I'm stumbling into a little problem as I'm currently trying to programmatically log into two wordpress blogs at once. I have a custom login page and the sessions for both wordpress blogs have to be created. One blog works just fine, but as I have to include another wp-load.php
it doesn't work for the second one... My code so far:
...
public static function blogLogin($mail, $password) {
require __DIR__ . '/../blog/wp-load.php';
$wpuser = get_user_by('email', $mail);
if (!$wpuser) $wpuser = get_user_by('login', $mail);
if (!empty($wpuser) && !empty($wpuser->ID)) {
wp_set_auth_cookie($wpuser->ID, true);
return true;
} else {
return false;
}
}
public static function secondBlogLogin($mail, $password) {
require __DIR__ . '/../secondblog/wp-load.php';
$wpuser = get_user_by('email', $mail);
if (!$wpuser) $wpuser = get_user_by('login', $mail);
if (!empty($wpuser) && !empty($wpuser->ID)) {
wp_set_auth_cookie($wpuser->ID, true);
return true;
} else {
return false;
}
}
...
And is being called by:
Class::blogLogin(...); Class::secondBlogLogin(...);
This seems not to work as the include is not only "sandboxed" inside the function but also available in the outside. Is there a ways to either kind off an inclusion or otherwise to achieve this login at both wordpress blogs in one script?
Below answer is for enable login of two word press during one execution from single custom login page.
//Your form which contain custom login (email,password).
<?php
//afer submit form post login credential value into PHP code of first file.
if(isset($_POST) && isset($_POST['submit'])){
require("path of first wordpress root directory \wp-load.php");
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
} else {
$creds = array();
// Use the submited information to populate the user_login & user_password
$creds['user_login'] = $_POST['username or email'];
$creds['user_password'] = $_POST['password'];
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
echo $user->get_error_message();
} else {
wp_set_auth_cookie( $user->ID, true );
}
?>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
var url = 'url/first-blog-login.php';
var user = '<?php echo $_POST['username or email'];?>';
var password = '<?php echo $_POST['password'];?>';
$.ajax({
type: "POST",
url: url,
data: {
'user':user,
'password':password
},
success: function(){
alert("detail passed successfully.");
}
});
</script>
<?php
}
}
if(isset($_POST) && !empty($_POST)){
$user = $_POST['user'];
$password = $_POST['password'];
require("path of secound wordpress root directory\wp-load.php");
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
} else {
$creds= array();
// Use the submited information to populate the user_login & user_password
$creds['user_login'] = $user;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
echo $user->get_error_message();
} else {
wp_set_auth_cookie( $user->ID, true );
}
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With