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
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♦ Nov 9 '17 at 0:54