Page Templates from plugin not working after upgrading WP to 4.7 or upper version

The Problem:

As Mark suggested already, your template loading by manipulating cache is far from standard practice. With these sort of cache alteration, even if you modify your CODE to work with WordPress 4.7+, there is no guarantee that you’ll not find similar problems in future updates. So better use any of the solutions mentioned below:

Theme Solution:

Instead of assigning templates form a plugin, you can have actual page templates in the active theme. Your active theme is the recommended place to have these page templates.

Plugin Solution

However, if you have to assign these templates with your plugin for some reason, then use the theme_page_templates hook to do so. It’ll work for WordPress 4.4+.

Following is the rewrite of your CODE using theme_page_templates filter hook:

function get_my_template( $template ) {
    $post = get_post();
    $page_template = get_post_meta( $post->ID, '_wp_page_template', true );
    if( $page_template == 'templates/study_home.php' ){
        return plugin_dir_path(__FILE__) . "templates/study_home.php";
    }
    if( $page_template == 'templates/study_job_home.php' ){
        return plugin_dir_path(__FILE__) . "templates/study_job_home.php";
    }
    if( $page_template == 'templates/exams.php' ){
        return plugin_dir_path(__FILE__) . "templates/exams.php";
    }
    if( $page_template == 'templates/colleges.php' ){
        return plugin_dir_path(__FILE__) . "templates/colleges.php";
    }
    return $template;
}

function filter_admin_page_templates( $templates ) {
    $templates['templates/exams.php'] = __('Exams');
    $templates['templates/colleges.php'] = __('Colleges');
    $templates['templates/study_home.php'] = __('Study Home');
    $templates['templates/study_job_home.php'] = __('Study Job Home');
    return $templates;
}

function add_my_templates() {
    if( is_admin() ) {
        add_filter( 'theme_page_templates', 'filter_admin_page_templates' );
    }
    else {
        add_filter( 'page_template', 'get_my_template', 1 );
    }
}

add_action( 'wp_loaded', 'add_my_templates' );

Use the above CODE instead of the CODE you’ve provided. It’ll work for any WordPress version 4.4 and later. I’ve tested it for WordPress 4.7.2 & it works fine.

Leave a Comment