Load Template with Plugin

A little to work with the current WordPress version. 



/**
 * Load Template with Plugin
 */
function yourname_add_page_template ($templates) {
    $templates['page-one.php'] = 'title here One';
    $templates['page-two.php'] = 'title here Two';
    $templates['page-three.php'] = 'title here Three';
    return $templates;
}
add_filter ('theme_page_templates', 'yourname_add_page_template');

function yourname_redirect_page_template ($template) {
    $post = get_post();
    $page_template = get_post_meta( $post->ID, '_wp_page_template', true );
    if ('page-one.php' == basename ($page_template)) {
        $template = WP_PLUGIN_DIR . '/pluginname/templates/page-one.php';
        return $template;
    }
    elseif ('page-two.php' == basename ($page_template)) {
        $template = WP_PLUGIN_DIR . '/pluginname/templates/page-two.php';
        return $template;
    }
    elseif ('page-three.php' == basename ($page_template)) {
        $template = WP_PLUGIN_DIR . '/pluginname/templates/page-three.php';
        return $template;
    }
}
add_filter ('page_template', 'yourname_redirect_page_template');


Load Template with Plugin Load Template with Plugin Reviewed by Md Shahinur Islam on July 17, 2022 Rating: 5

No comments:

Powered by Blogger.