最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I use a plugin to swap out the template file for a custom post type?

programmeradmin1浏览0评论

I'm trying to apply a filter for a custom post type and give it a different template. All from within a plugin. However, despite confirming that add_hooks() is called (everything else works in that area), the filter is flatly ignored.

I can only guess that I got something wrong as it looks like the filter is never applied. I know it is not because I put an else/die in there. It should either give me my template or die (right?) but it ignores me. How do I do this correctly?

class some_cool_new_stuff{

   public function __construct(){
       $this->add_hooks();
   }

   public function page_template( $page_template ) {
       if ( is_page( 'my_lovely_post_type' ) ) {
           $page_template = MY_CUSTOM_CONST_FOR_PLUGIN_DIR . 'templates/business-page.php'; // this does not happen but the path is 100% correct
       }else{
          die("Help me, I'm going to die!!"); // this never happens
       }
       return $page_template; 
   }

   public function add_hooks(){
       // ... 
       add_action( 'init', array($this,'awesome_function') );
       add_filter( 'page_template', array($this,'page_template') );
       // ... 
   }

   // ...

}

Here is some other stuff that doesn't work for me:

    add_filter( 'my_lovely_post_type_template', array($this,'page_template') );
    add_filter( 'template_include', array($this,'page_template') );

I'm trying to apply a filter for a custom post type and give it a different template. All from within a plugin. However, despite confirming that add_hooks() is called (everything else works in that area), the filter is flatly ignored.

I can only guess that I got something wrong as it looks like the filter is never applied. I know it is not because I put an else/die in there. It should either give me my template or die (right?) but it ignores me. How do I do this correctly?

class some_cool_new_stuff{

   public function __construct(){
       $this->add_hooks();
   }

   public function page_template( $page_template ) {
       if ( is_page( 'my_lovely_post_type' ) ) {
           $page_template = MY_CUSTOM_CONST_FOR_PLUGIN_DIR . 'templates/business-page.php'; // this does not happen but the path is 100% correct
       }else{
          die("Help me, I'm going to die!!"); // this never happens
       }
       return $page_template; 
   }

   public function add_hooks(){
       // ... 
       add_action( 'init', array($this,'awesome_function') );
       add_filter( 'page_template', array($this,'page_template') );
       // ... 
   }

   // ...

}

Here is some other stuff that doesn't work for me:

    add_filter( 'my_lovely_post_type_template', array($this,'page_template') );
    add_filter( 'template_include', array($this,'page_template') );
Share Improve this question edited Jul 7, 2019 at 15:41 Matthew Brown aka Lord Matt asked Jul 7, 2019 at 15:35 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

After trying a whole bunch of filter possibilities, I found one that worked.

add_filter( 'single_template', array($this,'page_template') );

Which got my function to be called. However, I also needed to make some more changes (not least of which taking out the die line). This was, finally, what worked:

public function page_template( $page_template ) {
    global $post;
    if ( 'my_lovely_post_type' === $post->post_type ) {
        $page_template = BUSINESS_PAGE_PLUGIN_DIR . 'templates/business-page.php';
    }
    return $page_template;
}
发布评论

评论列表(0)

  1. 暂无评论