Skip to content
Advertisement

Load custom page templates in WordPress admin dashboard

I have several custom page templates in my theme. But I want to hide a few with a plugin, and only show the custom home page template on the page that has been set as the front page “is_front_page” or “is_home”. This is what I am using in my plugin to stop some of the page templates from showing up.

This is for a large multisite where there are two tiers of sites, one gets the full set of features, and the other a stunted set. I have everything working as I need except for this.

add_filter( 'theme_page_templates', 'je_remove_page_template' );
  function je_remove_page_template( $pages_templates ) {
  unset( $pages_templates['page-topimage.php'] );
  unset( $pages_templates['page-home.php'] );
  return $pages_templates;
}

The code above works totally fine, but I need a conditional in there to show the page-home.php when the page has been set to the home page. I have tried this code, but it doesn’t work.

if ( is_front_page() ) :

    add_filter( 'theme_page_templates', 'je_remove_page_template' );
    function je_remove_page_template( $pages_templates ) {
    unset( $pages_templates['page-topimage.php'] );
    return $pages_templates;
    }

    else :

    function je_remove_page_template( $pages_templates ) {
    unset( $pages_templates['page-topimage.php'] );
    unset( $pages_templates['page-home.php'] );
    return $pages_templates;
    }

endif;

Any ideas on how I can get this to work?

Advertisement

Answer

A few things:

One, it appears you’ve only added the add_filter call to the is_front_page()‘s true condition, that’s probably part of it.

Second, you should not be defining and/or redefining functions inside if statements!

Third, for WordPress specifically, with it’s Action Hook and Filter system, it’s generally considered a best practice to add the filters and actions at all times, and run if/else or abort type statements inside the function to allow for easier extensibility.

Edit:

Based on some clarification, I understand a bit better. You need to get the ID of the page that’s set to the front page, which is stored as an option named page_on_front in the options table. The is_front_page() function will only work in the scope of the current $wp_query loop.

I think this would solve it for you. Run the je_remove_page_template function on the theme_page_templates filter, and check the current page’s ID against the one stored in the page_on_front option:

add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ){
    unset( $pages_templates['page-topimage.php'] );

    $home_page_id = absint( get_option( 'page_on_front' ) );
    $this_page_id = get_the_ID();

    // If this isn't the home page, remove `page-home`
    if( $this_page_id != $home_page_id ){
        unset( $pages_templates['page-home.php'] );
    }

    return $pages_templates;
}

Also of note would be that is_front_page() is different than is_home() – make sure you’re using the correct one! (I believe you are, as generally IFP is what people want)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement