Wordpress how to use get_current_screen and global pagenow

I do not understand the difference between the following two

get_current_screen

pagenow

Can you help me with that?

1

1 Answer

According to get_current_screenDocs:

it'll "return current screen object or null when screen not defined".

If you want to use it to detect admin pages, then use a different hook, for example you could use current_screen hook:

add_action('current_screen', 'detecting_current_screen');
function detecting_current_screen()
{ $current_screen = get_current_screen(); print_r($current_screen);
}

Or

add_action('current_screen', 'detecting_current_screen');
function detecting_current_screen()
{ global $current_screen; print_r($current_screen);
}

If you want to use it on the client-side and that returns null, then you could use a global variable called $pagenow.

Note: $pagenow will give you the template name.

global $pagenow;
echo $pagenow;

Or

echo $GLOBALS["pagenow"];

This should give you the name of the template you're on:

add_action( 'wp_footer', function () { global $pagenow; echo $pagenow;
}, 999 );

If none of the methods, mentioned above, work for you, then you could use another global variable called $post.

Or

You could check your page by using:

get_page_by_titleDocs
or
get_page_by_pathDocs

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like