Check if the administrator has logged into WordPress

We have a complicated weird system where we need to check if the wordpress administrator has logged in and by logged in i dont just mean the session but initial second when the query is sent to validate the credentials entered. I was thinking doing traditional php where I do something like the following:

if(isset($_POST['submit']) { // do something }

But that would be the incorrect way. Where exactly would i look to see this peice , i believe its wp-login.php but wasnt able to find what i was looking for.

Read More

thanks

Related posts

Leave a Reply

3 comments

  1. You can use very neat WordPress function to determine the user capabilities, it is called current_user_can():

    <?php
    if ( current_user_can('update_core') ) {
    echo 'The current user is Administrator or Super administrator (in Multisite)';
    }

    Only Administrators of single site installations have the below list of capabilities. In Multi-site, only the Super Admin has these abilities:

    update_core 
    update_plugins 
    update_themes 
    install_plugins 
    install_themes 
    delete_themes 
    edit_plugins 
    edit_themes 
    edit_users 
    create_users 
    delete_users 
    unfiltered_html
    

    See here for more information.

  2. We can add some custom action inside the filter hook login_redirect. If the form was not submitted, the $user param is a WP_Error Object.

    add_filter( 'login_redirect', function( $redirect_to, $requested_redirect_to, $user )
    {
        if( !is_wp_error( $user ) && in_array( 'administrator', $user->roles ) )
        {
            # Do your thing; here just inspecting the WP_User Object
            wp_die( 
                sprintf( '<pre>%s</pre>', print_r( $user, true ) ),
                'Var dump',
                array( 
                    'response' => 500, 
                    'back_link' => true 
                )
            );
        }
    
        return $redirect_to;
    }, 10, 3 );