How to load php file for specific page in admin?

Based on this comment:

@ChipBennett, it’s the site’s front page, i.e. the home page.

(Side note: in WordPress the home page is not the Site Front Page, but rather the Blog Posts Index. is_home() returns true on the blog posts index, and not necessarily on the site front page. This is important to understand, as it can make certain function names and terminology confusing.)

This is actually quite easy. If your site front page is a static page, then you can find out the ID of that page like so:

get_option( 'page_on_front' );

Next step: hook your metabox code into the correct hook: 'add_meta_boxes', or, more precisely: 'add_meta_boxes_page':

add_action( 'add_meta_boxes_page','load_home_meta' );
function load_home_meta() {}

Next, I’ll assume that all of your meta box definition code is in the functional file /metaboxes/home-main-home.php? We just want to load it only conditionally, which we’ll do as follows:

global $post;
if ( $post->ID == get_option( 'page_on_front' ) ) {
    // Load meta boxes file
}

Next, you need to reference that file properly. I’ll assume we’re working with a Theme? In that case, you need to reference get_template_directory_uri():

include( get_template_directory() . '/metaboxes/home-main-home.php' );

Putting it all together:

add_action( 'add_meta_boxes_page','load_home_meta' );
function load_home_meta() {
    global $post;
    if ( $post->ID == get_option( 'page_on_front' ) ) {
        include( get_template_directory() . '/metaboxes/home-main-home.php' );
    }
}

This all assumes, of course, that the functional code in /metaboxes/home-main-home.php is correct…

Edit

Use get_template_directory() (filepath) rather than get_template_directory_uri() (URL path) for include() calls.

Leave a Comment