I am working on a plugin which we need for education website. I have added 3-4 Page templates within my plugin so that we can call when plugin is activated.
Till WordPress 4.7
, it was working perfectly; but as I upgraded WordPress to the latest (from 4.6.3
), page templates don’t even show in page attribute section.
Here is the code which was working fine with older versions (before 4.7
):
add_action( 'wp_loaded', 'add_my_templates' );
function add_my_templates() {
if( is_admin() ){
global $wp_object_cache;
$current_theme = wp_get_theme();
$templates = $current_theme->get_page_templates();
$hash = md5( $current_theme->theme_root . "https://wordpress.stackexchange.com/". $current_theme->stylesheet );
$templates = $wp_object_cache->get( 'page_templates-'. $hash, 'themes' );
$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');
wp_cache_replace( 'page_templates-'. $hash, $templates, 'themes' );
}
else {
add_filter( 'page_template', 'get_my_template', 1 );
}
}
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' ) {
$template = plugin_dir_path(__FILE__) . "templates/study_home.php";
}
if( $page_template == 'templates/study_job_home.php' ) {
$template = plugin_dir_path(__FILE__) . "templates/study_job_home.php";
}
if( $page_template == 'templates/exams.php' ) {
$template = plugin_dir_path(__FILE__) . "templates/exams.php";
}
if( $page_template == 'templates/colleges.php' ) {
$template = plugin_dir_path(__FILE__) . "templates/colleges.php";
}
return $template;
}
I am searching for the solution from last 2 days, but no luck!