admin管理员组

文章数量:1135119

I am following a rather outdated tutorial on custom post types and the last step is to create a custom page template, where this is intended to show all posts of the custom type in archive listing format. As per the comments, the methods used no longer work 6 years later (go figure) and my custom page template isn't an option when creating a new page.

If I place my custom template in the theme folder it works but the option to load it from within the plugin directory has broken. How can I fix this so I can assign the template to a new page?

Edit

As I better understand now, this code is only loading the template when viewing my custom post types. The confusion was due to it showing as an available template on the "Page Attributes" section when it was located in the Themes directory. I am wanting to have it available as an option when making a new page, but have the template file located in my plugin's directory.

The original code from the tutorial:

add_filter( 'template_include', 'include_template_function', 1 );

function include_template_function( $template_path ) {
    if ( get_post_type() == 'movie_reviews' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'single-movie_reviews.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . '/single-movie_reviews.php';
            }
        }
    }
    return $template_path;
}

I am following a rather outdated tutorial on custom post types and the last step is to create a custom page template, where this is intended to show all posts of the custom type in archive listing format. As per the comments, the methods used no longer work 6 years later (go figure) and my custom page template isn't an option when creating a new page.

If I place my custom template in the theme folder it works but the option to load it from within the plugin directory has broken. How can I fix this so I can assign the template to a new page?

Edit

As I better understand now, this code is only loading the template when viewing my custom post types. The confusion was due to it showing as an available template on the "Page Attributes" section when it was located in the Themes directory. I am wanting to have it available as an option when making a new page, but have the template file located in my plugin's directory.

The original code from the tutorial:

add_filter( 'template_include', 'include_template_function', 1 );

function include_template_function( $template_path ) {
    if ( get_post_type() == 'movie_reviews' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'single-movie_reviews.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . '/single-movie_reviews.php';
            }
        }
    }
    return $template_path;
}
Share Improve this question edited Dec 14, 2017 at 8:49 Aidan Knight asked Dec 14, 2017 at 8:13 Aidan KnightAidan Knight 3831 gold badge3 silver badges14 bronze badges 8
  • Is your single-movie_reviews.php is in plugin root directory ? Is the pasted code in the plugin too ? (I think that's not the problem but plugin_dir_path return the path with a / in the you can remove the / from $template_path) – Elex Commented Dec 14, 2017 at 8:22
  • The pasted code is in the plugin, it works if the template file is in my theme folder but not if it is located in the plugin folder. I do currently have it in the root of the plugin. – Aidan Knight Commented Dec 14, 2017 at 8:30
  • How this code is working for listing archive, for templates in theme directory, when you executed it only on single post page is_single? – kierzniak Commented Dec 14, 2017 at 8:31
  • I was curious about that as well. The code I listed is directly from the tutorial on tutsplus.com but I tried removing the if checks and it still doesn't seem to load it. – Aidan Knight Commented Dec 14, 2017 at 8:34
  • May you try add_filter( 'template_include', 'include_template_function', 1, PHP_INT_MAX); – Elex Commented Dec 14, 2017 at 8:36
 |  Show 3 more comments

1 Answer 1

Reset to default 12

To add custom template in page attributes template section you have to first add your template to dropdown and load it in template_include hook when current page has selected it as current template.

/**
 * Add "Custom" template to page attirbute template section.
 */
function wpse_288589_add_template_to_select( $post_templates, $wp_theme, $post, $post_type ) {

    // Add custom template named template-custom.php to select dropdown 
    $post_templates['template-custom.php'] = __('Custom');

    return $post_templates;
}

add_filter( 'theme_page_templates', 'wpse_288589_add_template_to_select', 10, 4 );


/**
 * Check if current page has our custom template. Try to load
 * template from theme directory and if not exist load it 
 * from root plugin directory.
 */
function wpse_288589_load_plugin_template( $template ) {

    if(  get_page_template_slug() === 'template-custom.php' ) {

        if ( $theme_file = locate_template( array( 'template-custom.php' ) ) ) {
            $template = $theme_file;
        } else {
            $template = plugin_dir_path( __FILE__ ) . 'template-custom.php';
        }
    }

    if($template == '') {
        throw new \Exception('No template found');
    }

    return $template;
}

add_filter( 'template_include', 'wpse_288589_load_plugin_template' );

theme_page_templates hook is available for page post type. If you want to add custom template to other post type you must replace page with your custom post type name e.g. event post type hook will have a name theme_event_templates.

本文标签: Loading custom page template via plugin