0

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

  • Where is the custom login code happening? If anything has been output to the page already then this is happening far too late, it needs to happen early so that headers can get sent, let us know where and when the code runs – Tom J Nowell Nov 8 '17 at 20:37
  • @TomJNowell Hiya, thank you for your assistance. I have updated the OP with the code from the Login.php So this is a shortcode, as I do not use any of WP core login/registration stuff. – BadAddy Nov 8 '17 at 20:56
  • @TomJNowell Just, again, updated the code and it is clear the wp_authentication is not logging the user in. – BadAddy Nov 8 '17 at 21:15
  • I'm still not sure when this code runs, is that your 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
  • @TomJNowell I will be honest I am not sure when the code runs. This is a plugin and has it's own functions.php. Not used the Themes function.php and thought the install and activation of the plugin would handle the load sequence. Using Chrome Dev Screen I have been unable to see what loads first? – BadAddy Nov 9 '17 at 8:07
0

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.

  • What did you change? – Tom J Nowell Nov 9 '17 at 0:55
  • I added the following lines: ` wp_set_current_user($user->ID, $user->user_email); wp_set_auth_cookie($user->ID); do_action('wp_login', $user->user_email);`. I also added the nonce to the end of the redirect link. And I also added a filter for the menus. – BadAddy Nov 9 '17 at 8:05
0

wp_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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.