How to Exclude Functions & Code from Running on Specific WordPress Admin Pages

Post written by David Wells
On Wednesday, November 20th, 2013

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!

About the Author:
David is Founder of Inbound Now and a Fanatical WordPress Designer & Developer. He believes that the internet is a magical place where wonderful things can happen. Say hi to him @DavidWells

Posted in: Code WordPress
Signup and get started with Inbound Now

Sign Up for Free

Download

[gravityform id="3" name="Popup Form" title="false" description="false" ajax="true"]