Single_template for a custom post type created from a plugin is returning an empty page

Blank content

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 to 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 (is there no ‘hello world’ text at all) but I can see the echo ‘EXISTS! ‘ . $plugin_path . ‘views/single-press_release.php’:

What am I doing wrong?

Thanks a lot!

Read more here: Single_template for a custom post type created from a plugin is returning an empty page

Leave a Reply

Your email address will not be published. Required fields are marked *