0

I'm trying to create a single template for the custom post type posts called 'press release' from a plugin.

This is the code I added to my plugin:

function single_press_release_template($single_template) {
     global $post;
     $plugin_path = plugin_dir_path( __FILE__ );
     if ($post->post_type == 'press_release') {
        if(file_exists($plugin_path . 'views/single-press_release.php')) {
            $single_template = $plugin_path . 'views/single-press_release.php';
            echo 'EXISTS! ' . $plugin_path . 'views/single-press_release.php';
        }
     }
     return $single_template;
}
add_filter( 'single_template', 'single_press_release_template');

The file single-press_release.php (located in the 'views' plugins directory) contains this code:

/**
 * Single Press Release
 *
 * @package WordPress
 */

<?php

echo 'Hello World';

?>

The result I get is a page without any main content (there is no 'hello world' text at all) but I can see the echo 'EXISTS! ' . $plugin_path . 'views/single-press_release.php':

blank main content

What am I doing wrong?

0

So, the template file is there, but it doesn't get called, even though your function looks good (to me) as it does exactly what the codex says. The problem is therefore most probably caused by interference by your theme or another plugin that uses the same filter, overruling yours. You could solve that by giving your filter a higher priority:

add_filter ('single_template', 'single_press_release_template', 20 ,1);

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.