0

I am creating a plugin which requires use of custom templates so using the following code to use the templates:

add_filter('template_include', 'hhavideo_set_template');
 function hhavideo_set_template( $template ){
  if(is_archive('hhavideo') && 'archive-hhavideo.php' != $template ){
   $template = include( plugin_dir_path( __FILE__ ) . 'templates/archive-hhavideo.php');
  }
  if(is_singular('hhavideo') && 'single-hhavideo.php' != $template ){
   $template = include( plugin_dir_path( __FILE__ ) . 'templates/single-hhavideo.php');
  }
  return $template;
 }

When that code is used, both templates crash my browser (Firefox), I sometimes get the error message that I have used the maximum execution time of 60 seconds.

The templates only have get_header and get_footer in them, the site runs perfectly fine on other pages.

What am I doing wrong, is there a better way to call the template files from the plugin?

Edit: Error I am now getting:

Warning: include(1) [function.include]: failed to open stream: No such file or directory in /wp-includes/template-loader.php on line 74
3
  • If you change the contents of the two template files you are trying to call to a simple echo statement, do you still get the errors? If not, then it's probably an issue with those files, and you would need to post the code for anyone to help.
    – SkyShab
    Commented Nov 20, 2014 at 13:17
  • is_archive() does not accept any parameters Commented Nov 20, 2014 at 13:23
  • Changed to is_post_type_archive and getting an include error (in top post), there is nothing in the templates apart from the header and footer calls. Commented Nov 20, 2014 at 13:59

1 Answer 1

2

You have a couple mistakes in your code:

is_archive() does not accept any parameters. If you want to check if this is the archive of a custom post type, use is_post_type_archive( $post_type )

  • Instead of using include( plugin_dir_path( __FILE__ ) . 'my-template.php');, use dirname( __FILE__ ) . 'my-template.php';

  • Single templates has its own filter, single_template, so split your function so that you have your template_include for the archive page separately

Try something like this

add_filter('template_include', function ( $template ) {

    if( is_post_type_archive( 'hhavideo' ) ){
        $template = dirname( __FILE__ ) . '/templates/archive-hhavideo.php';
    }
    return $template;

}, PHP_INT_MAX, 2 );

add_filter( 'single_template', function ($single_template) {

    if ( is_singular( 'hhavideo' ) ) {
        $single_template = dirname( __FILE__ ) . '/templates/single-hhavideo.php';
    }
    return $single_template;

}, PHP_INT_MAX, 2 );
1
  • 1
    My pleasure, enjoy :-) Commented Nov 20, 2014 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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