7

I want to add page templates to a theme directly from the plugin. The idea is that the template would show up in the dropdown under Page Attributes, and all the code would need to be in the plugin.

Any tips on how to achieve that ?

10

You can use the theme_page_templates filter to add templates to the dropdown list of page templates like this:

function wpse255804_add_page_template ($templates) {
    $templates['my-custom-template.php'] = 'My Template';
    return $templates;
    }
add_filter ('theme_page_templates', 'wpse255804_add_page_template');

Now WP will be searching for my-custom-template.php in the theme directory, so you will have to redirect that to your plugin directory by using the page_template filter like this:

function wpse255804_redirect_page_template ($template) {
    if ('my-custom-template.php' == basename ($template))
        $template = WP_PLUGIN_DIR . '/mypluginname/my-custom-template.php';
    return $template;
    }
add_filter ('page_template', 'wpse255804_redirect_page_template');

Read more about this here: Add custom template page programmatically

| improve this answer | |
  • perfect ! that does it exactly. – smartcat Feb 9 '17 at 15:53
  • 3
    since 4.7 version of wordpress, above code will not work. change this line from 'if ('my-custom-template.php' == basename ($template))' to $post = get_post(); $page_template = get_post_meta( $post->ID, '_wp_page_template', true ); if ('my-custom-template.php' == basename ($page_template )) – Nirmal Goswami Dec 9 '17 at 9:47
1

This is a combination of the above answer and the above comments which ended up working for me.

The function to add the plugin to the list of available templates:

function wpse255804_add_page_template ($templates) {
    $templates['my-custom-template.php'] = 'My Template';
    return $templates;
    }
add_filter ('theme_page_templates', 'wpse255804_add_page_template');


The function to point the template to the appropriate directory within the plugin:

function wpse255804_redirect_page_template ($template) {
    $post = get_post();
    $page_template = get_post_meta( $post->ID, '_wp_page_template', true );
    if ('my-custom-template.php' == basename ($page_template))
        $template = WP_PLUGIN_DIR . '/mypluginname/my-custom-template.php';
    return $template;
    }
add_filter ('page_template', 'wpse255804_redirect_page_template');
| improve this answer | |
0

From the codex:

<?php 
   $templates = get_page_templates();
   foreach ( $templates as $template_name => $template_filename ) {
       echo "$template_name ($template_filename)<br />";
   }
?>

You get then use the current templates and programmatically add them to whatever you wish.

| improve this answer | |
0

Thanks for the help. I changed the code a little to work with the current WordPress version. Also changed it to support more than one custom template.

I bet there is a better way of doing this but it worked for me.

/**
 * 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');

| improve this answer | |
0

The below code snippet is very well thought out, it will try to look for a template in the plugin and if it can't find it there it will try to get it from the theme.

define( 'MY_PLUGIN_DIR', plugin_dir_path( __FILE __ ) );
define( 'MY_PLUGIN_TEMPLATE_DIR', MY_PLUGIN_DIR . '/templates/' );

add_filter( 'template_include', 'ibenic_include_from_plugin', 99 );

function ibenic_include_from_plugin( $template ) {

    $new_template = '';

    $provided_template_array = explode( '/', $template );

    // This will give us archive.php
    $new_template = end( $provided_template_array );

    // Define single and archive template for custom post type 'portfolio'
    if( is_singular('portfolio') ) {
        $new_template = 'single-portfolio.php';
    }

    if( is_post_type_archive( 'portfolio' ) ) {
        $new_template = 'archive-portfolio.php';
    }

    $plugin_template = MY_PLUGIN_TEMPLATE_DIR . $new_template;

    if( file_exists( $plugin_template ) ) {
    return $plugin_template;
    }

    return $template;
}

Source: https://www.ibenic.com/include-or-override-wordpress-templates/

| improve this answer | |
-3
<?php load_template( $_template_file, $require_once ) ?>

That is from the very 1st google search results for "load template from plugin". Please make an effort to find the answers yourself before asking here.

| improve this answer | |
  • If you don't want the theme or child theme to be able to override the plugin template then just use require($template) – mrben522 Feb 9 '17 at 13:41
  • 2
    Ofcourse I've seen that on google. I don't think you understood my question. I want the template to show up in the Page Attributes dropdown, so the user can choose it when creating/editing a page. – smartcat Feb 9 '17 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.