How to Exclude Functions & Code from Running on Specific WordPress Admin Pages
This is a handy snippet for ensuring your functions only run on a specific WordPress admin screen. It checks the current page that the user is viewing and checks the ID of the page and if the IDs don’t match the function doesn’t run.
This utilizes the wordpress get_current_screen() function
$screen = get_current_screen(); //echo $screen->id; // show current screen id if ( $screen->id != 'landing-page_page_lp_global_settings') return; // exit if incorrect screen id
So in the above example, if the $screen id is not the correct page the function exits itself gracefully.
The screen id ‘landing-page_page_lp_global_settings‘ broken down is actually the post type ‘landing-page‘ and then the settings page ‘lp_global_settings‘ or http://local.dev/wp-admin/edit.php?post_type=landing-page&page=lp_global_settings.
Echo out the $screen->id variable to match your specific screen id.
The full function looks like:
add_action('admin_footer', 'landing_pages_load_sys_info'); function landing_pages_load_sys_info($hook) { global $wpdb; $screen = get_current_screen(); //echo $screen->id; if ( $screen->id != 'landing-page_page_lp_global_settings') return; // exit if incorrect screen id // do stuff if the page id is correct }
There are probably other methods of excluding admin scripts from specific pages as well. Leave them in the comments below if you have a better way!
Get help with difficult WordPress projects
Codeable.io provides over 500 senior WordPress Experts that can help you get past dead ends.