I am pretty new to all things Wordpress and seem to be really confused by what I have read.
What I am doing is creating a plugin for a specific job and I need to simply change the menu once logged in. Now I am using DIVI 2 as a theme and I have this in the functions.php of the plugin:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'UserMenu';
} else {
$args['menu'] = 'MainMenu';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
Which seems to make sense, but does not work and I am not sure if this is Divi2 or, as tested several times, the user is not logged in. It keeps coming back as false is_user_logged_in()
.
Here is the code I use for the custom login:
<?php
/**
* Created by PhpStorm.
* User: Andrew
* Date: 08/11/2017
* Time: 11:21
*/
global $wpdb;
$userOK = -1;
print_r(is_user_logged_in());
if(is_user_logged_in()) { //This does not action, ever!
?>
<script>
window.location.href = "/";
</script>
<?php
}
if($_POST['wd_resendActivationButton'] === 'resend') {
sendActivation($_POST['userid'], $_POST['useremail'],"2");
}
if($_POST['loginEmail'] && $_POST['loginPassword'] && !$_POST['wd_resendActivationButton']){
$userOK = 0;
$user = wp_authenticate( $_POST['loginEmail'] , $_POST['loginPassword'] );
if(is_wp_error($user)) {
echo $user->get_error_message();
} else {
if($user->user_status === "0") {
wp_logout();
} else {
?>
<script>
window.location.href = "/";
</script>
<?php
}
}
}
As I understood it, wp_authenticate
also logged the user in, but it seems to not do that. So not sure where the issue lays. Could someone please point me into the correct direction?
UPDATE
Using the below code, it is clear the wp_authentication is failing, although it does give me the user object, it does not log the user in:
if($_POST['loginEmail'] && $_POST['loginPassword'] && !$_POST['wd_resendActivationButton']){
$userOK = 0;
$user = wp_authenticate( $_POST['loginEmail'] , $_POST['loginPassword'] );
if(is_wp_error($user)) {
echo $user->get_error_message();
} else {
if($user->user_status === "0") {
wp_logout();
} else {
if(is_user_logged_in()) {
?>
<script>
console.log('logged in');
window.location.href = "/";
</script>
<?php
} else {
?>
<script>
console.log('Not logged in');
window.location.href = "/";
</script>
<?php
}
}
}
}
Thanks
Addy
I am pretty new to all things Wordpress and seem to be really confused by what I have read.
What I am doing is creating a plugin for a specific job and I need to simply change the menu once logged in. Now I am using DIVI 2 as a theme and I have this in the functions.php of the plugin:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'UserMenu';
} else {
$args['menu'] = 'MainMenu';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
Which seems to make sense, but does not work and I am not sure if this is Divi2 or, as tested several times, the user is not logged in. It keeps coming back as false is_user_logged_in()
.
Here is the code I use for the custom login:
<?php
/**
* Created by PhpStorm.
* User: Andrew
* Date: 08/11/2017
* Time: 11:21
*/
global $wpdb;
$userOK = -1;
print_r(is_user_logged_in());
if(is_user_logged_in()) { //This does not action, ever!
?>
<script>
window.location.href = "https://URL.uk/userhome/";
</script>
<?php
}
if($_POST['wd_resendActivationButton'] === 'resend') {
sendActivation($_POST['userid'], $_POST['useremail'],"2");
}
if($_POST['loginEmail'] && $_POST['loginPassword'] && !$_POST['wd_resendActivationButton']){
$userOK = 0;
$user = wp_authenticate( $_POST['loginEmail'] , $_POST['loginPassword'] );
if(is_wp_error($user)) {
echo $user->get_error_message();
} else {
if($user->user_status === "0") {
wp_logout();
} else {
?>
<script>
window.location.href = "https://url.uk/userhome/";
</script>
<?php
}
}
}
As I understood it, wp_authenticate
also logged the user in, but it seems to not do that. So not sure where the issue lays. Could someone please point me into the correct direction?
UPDATE
Using the below code, it is clear the wp_authentication is failing, although it does give me the user object, it does not log the user in:
if($_POST['loginEmail'] && $_POST['loginPassword'] && !$_POST['wd_resendActivationButton']){
$userOK = 0;
$user = wp_authenticate( $_POST['loginEmail'] , $_POST['loginPassword'] );
if(is_wp_error($user)) {
echo $user->get_error_message();
} else {
if($user->user_status === "0") {
wp_logout();
} else {
if(is_user_logged_in()) {
?>
<script>
console.log('logged in');
window.location.href = "https://URL.uk/userhome/";
</script>
<?php
} else {
?>
<script>
console.log('Not logged in');
window.location.href = "https://URL.uk/";
</script>
<?php
}
}
}
}
Thanks
Addy
Share Improve this question edited Nov 8, 2017 at 21:14 BadAddy asked Nov 8, 2017 at 20:08 BadAddyBadAddy 1211 silver badge4 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 2wp_authenticate()
just checks user data, but not actually authenticating - docs.
You can use wp_signon
, which uses and wp_authenticate
: source, like this:
$credentials = [
'user_login' => $name,
'user_password' => $password,
'rememberme' => true,
];
$signon = wp_signon($credentials, true); // true - use HTTP only cookie
if(is_wp_error($signon)){
return false;
}
// The user is logged in redirect, return true, etc.
I managed to figure out what was causing it. But I find it odd that there is such little information on it. Anyway i hope this may help others in the future:
if($_POST['loginEmail'] && $_POST['loginPassword'] && !$_POST['wd_resendActivationButton']){
$userOK = 0;
$user = wp_authenticate_username_password(NULL, $_POST['loginEmail'] , $_POST['loginPassword'] );
wp_set_current_user($user->ID, $user->user_email);
wp_set_auth_cookie($user->ID);
do_action('wp_login', $user->user_email);
if(is_wp_error($user)) {
echo $user->get_error_message();
} else {
if($user->user_status === "0") {
wp_logout();
$userOK =1;
} else {
if(is_user_logged_in()) {
$nonce = wp_create_nonce();
function my_wp_nav_menu_args( $args = '' ) {
if ( is_user_logged_in() ) {
$args['menu'] = 'UserMenu';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
?>
<script>
console.log('logged in');
window.location.href = "https://URL.uk/userhome?loggedin=true&_wpnonce=<?php echo $nonce; ?>";
</script>
<?php
} else {
?>
<script>
console.log('Not logged in');
window.location.href = "https://URL.uk/";
</script>
<?php
}
}
}
}
I would only add that I am not sure if the filter function for the menu needs to be here, or if I can place this back in Functions.php and use use the:
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
Not sure what is standard practice. But it works.
Thanks.
functions.php
? Where is the file included? Or is it a template? I cannot emphasise how important the when is, and where the code is ran, it's not enough to just see it without context. – Tom J Nowell ♦ Commented Nov 9, 2017 at 0:54