Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to insert Custom Template file through the plugin that handle a custom post type. Rest of the plugin working great except the template including part.

class ClassName{

    public function __construct() {
        add_action( 'template_redirect', array( $this, 'uniThemeRedirect') );
    }


    public function uniThemeRedirect(){

        global $wp_query;
        global $wp;

        // Specific Custom Post Type
        if( $wp->query_vars["post_type"] == "videos" ){
            $templateFilename = "single-videos.php";

            if( file_exists( TEMPLATEPATH . '/' . $templateFilename ) ){
                $return_template = TEMPLATEPATH . '/' . $templateFilename;
            }
            else {
                $return_template = UNISLIDER_TEMPLATES . $templateFilename;
            }

            $this->doThemeRedirect( $return_template );
        } else {
            echo 'not working';
        }

    }


    public function doThemeRedirect( $template_url ){
        global $post, $wp_query;
        if( have_posts() ){
            echo $template_url;
            require_once ( $template_url );
        }
        else{
            $wp_query->is_404 = true;
        }

    }

}

$handle = new ClassName();

I am following this http://stackoverflow.com/questions/4647604/wp-use-file-in-plugin-directory-as-custom-page-template#answer-4975004 example.

But when i visit on the http://www.domain.com/videos where videos is my custom post type.

i get the 'not working' means no template include.
Can anyone please help me what wrong i have done??

UPDATE: This is how i register the Custom Post Type

$labels = array(
             'name'=>__('UniVideos', 'unislider'), 
             'singular_name'=>__( 'Videos', 'unislider' ), 
             'add_new'=>__( 'Add New', 'unislider' ), 
             'add_new_item'=>__( 'Add New Video' ), 
             'edit_item'=>__('Edit Video'), 
             'new_item'=>__('New Video'), 
             'view_item'=>__('View Videos Images'), 
             'search_item'=>__('Search Slide'), 
             'not_found'=>__('Noting Found'), 
             'not_found_in_trash'=>__('Nothing found in Trash'), 
             'parent_item_colon'=>''
         );

         $args = array(
             'labels'=>$labels, 
             'public'=>true, 
             'has_archive'=>true, 
             'publicly_queryable'=>true, 
             'show_ui'=>true, 
             'query_var'=>true, 
             'menu_icon'=>'', 
             'rewrite'=>true, 
             'capability_type'=>'post', 
             'hierarchical'=>false, 
             'menu_position'=>NULL, 
             'supports'=>array( 'title' )
         );

         register_post_type( 'videos', $args );
share|improve this question
add comment

1 Answer

Short answer: I guess the problem of your code is the missing die(); as last line of doThemeRedirect method.


After that, your code can be simplified a lot, using the template_include filter hook instead of the template_redirect action hook.

Simplify means reduce the chances of bugs, or at least, let recognization of them simpler.

 public function __construct() {
    add_filter( 'template_include', array( $this, 'uniThemeRedirect') );
}


public function uniThemeRedirect( $template ){
    if(  get_query_var('post_type') == 'videos' || (is_single() && get_post_type() == 'videos') ) {

        // the 'single-videos.php' will be included for both
        // single and archive view of CPT 'videos'
        // to stop including for single view, remove
        // (is_single() && get_post_type() == 'videos') ) from if condition
        // to stop including for archive view, remove
        // get_query_var('post_type') == 'videos' from if condition

        $templateFilename = 'single-videos.php';
        $locate = locate_template( $templateFilename );
        if( $locate ) return $locate;
        if( file_exists( user_trailingslashit(UNISLIDER_TEMPLATES) . $templateFilename ) )
           return user_trailingslashit(UNISLIDER_TEMPLATES) . $templateFilename );
    }
    // die 'not working';
    return $template;

}

So you don't need the doThemeRedirect method at all.

This code is simpler than your, but more powerful (it supports child themes where the yours doesn't) and a bit more good-practised: there is no one global variable in it.

share|improve this answer
    
it's not working in this case, i tried to debug $template that returns the post.php page from the theme directory, and i want to include the single-videos.php from the plugin directory, it means is_post_type_archive('videos') return false. –  jogesh_pi Sep 13 '13 at 12:05
    
for your information, i am trying to write plugin for a theme, the theme hand there custom template with the post-{name}.php, I don't think if its matter because all the custom templates has Template Name: NameofTemplate –  jogesh_pi Sep 13 '13 at 12:09
    
i tried with your given code, but if( is_post_type_archive('videos') ) { always return false. –  jogesh_pi Sep 13 '13 at 12:21
    
I have updated my question and added how i created custom post type. –  jogesh_pi Sep 13 '13 at 12:31
    
I want to include single-videos.php on single page with the following URL: http://www.domain.com/videos. I tried with your updated code but still the same, not including the file :( –  jogesh_pi Sep 13 '13 at 12:43
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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