Wordpress - How can you check if you are in a particular page in the WP Admin section? For example how can I check if I am in the Users > Your Profile page?
There is a global variable in wp-admin called $pagenow which holds name of the current page, ie edit.php, post.php, etc.
You can also check the $_GET request to narrow your location down further, for example:
global $pagenow;
if (( $pagenow == 'post.php' ) && ($_GET['post_type'] == 'page')) {
// editing a page
}
if ($pagenow == 'users.php') {
// user listing page
}
if ($pagenow == 'profile.php') {
// editing user profile page
}
The way to do this is to use the 'admin_enqueue_scripts' hook to en-queue the files you need. This hook will get passed a $hook_suffix that relates to the current page that is loaded:
function my_admin_enqueue($hook_suffix) {
if($hook_suffix == 'appearance_page_theme-options') {
wp_enqueue_script('my-theme-settings', get_template_directory_uri() . '/js/theme-settings.js', array('jquery'));
wp_enqueue_style('my-theme-settings', get_template_directory_uri() . '/styles/theme-settings.css');
?>
<script type="text/javascript">
//<![CDATA[
var template_directory = '<?php echo get_template_directory_uri() ?>';
//]]>
</script>
<?php
}
}
add_action('admin_enqueue_scripts', 'my_admin_enqueue');
The most comprehensive method is get_current_screen
added in WordPress 3.1
$screen = get_current_screen();
returns
WP_Screen Object (
[action] =>
[base] => post
[id] => post
[is_network] =>
[is_user] =>
[parent_base] => edit
[parent_file] => edit.php
[post_type] => post
[taxonomy] =>
)