I would like to access author id when a visitor come to author page? when I am coding in author.php file, I could print author id like below

$q_obj = get_queried_object();
$user_id = (int) $q_obj->data->ID;

But, if I would like to get user id functions.php, How should I print?

function zb_user_analyze() {
    // I would like to access user id here
add_action('wp_loaded', 'zb_user_analyze');

Note: I only want to access author page user id for nonlogin user.


wp_loaded hook runs before Wordpress has setup postdata so you won't be able to get there the information you need.

The earliest hook you can use to have that information should be wp.

You can use is_author() to check if you're on an author page and is_user_logged_in() to check if the user is logged in:

function zb_user_analyze() {
    if( is_author() && !is_user_logged_in() ) {
        $author_id = get_query_var( 'author' );
        // do something
add_action('wp', 'zb_user_analyze');
  • Did this helped? – dbeja Jul 21 '17 at 23:04

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.