I want to put JavaScript code on certain pages in frontend, But the output returns null.
add_action( 'wp_footer', function () {
$screen = get_current_screen();
var_dump( $screen );
}, 999 );
CodePudding user response:
According to get_current_screen
Docs:
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: