0

As the title says, I am creating a Sage 10 theme (we are NOT using bedrock). This website requires a custom post type of "speaker" which will also come with a single-speaker.php page to display the information. All of this functionality was written within a plugin but I am having trouble getting the page template to populate within the theme.

The custom post type and metabox do work, and I can get the values as well. However, the single-speaker.php page will not work. I have tried:

add_filter('single_template', 'speaker_single_template');

function speaker_single_template($single) {

    global $post;

    /* Checks for single template by post type */
    if ( $post->post_type == 'speaker' ) {
        if ( file_exists( SPEAKER_PLUGIN_URL . '/templates/single-speaker.php' ) ) {
            return SPEAKER_PLUGIN_URL . '/templates/single-speaker.php';
        }
    }

    return $single;

}

I would have thought this filter would have pushed the template page into the theme, but it simply is not.

Is there a problem where Sage uses blade directives? I had assumed the default php pages would still work.

2 Answers2

0

You can use single_template filter hook.

add_filter('single_template', function ($single) {
global $post;
/* Checks for single template by post type */
if ( $post->post_type == 'POST TYPE NAME' ) {
    if ( file_exists( PLUGIN_PATH . '/Custom_File.php' ) ) {
        return PLUGIN_PATH . '/Custom_File.php';
    }
}
return $single;});
Abdul Hanan
  • 53
  • 1
  • 8
  • Thank you, but that is the code I have that I said does not work – Devon Regular May 31 '22 at 11:35
  • Sorry for that, try this filter under `template_redirect` hook or `init` hook. The reason for not working is that I think your plugin load after the `single_template` hook or filter – Abdul Hanan May 31 '22 at 11:41
  • Yeah, where I am using Sage 10 even when I put the the single-speaker.php file in the theme root it will not work because it forces me to use the content-signle-speaker.blade.php format. I will try the redirect hook thanks – Devon Regular May 31 '22 at 11:44
  • unfortunately neither of them worked. D: Thank you for your time – Devon Regular May 31 '22 at 11:46
0

Try this:

add_filter('template_include', function ($template) {
    global $post;

    if ( ! empty($post->post_type) && 'speaker' === $post->post_type) {
        if (file_exists(SPEAKER_PLUGIN_URL.'/templates/single-speaker.php')) {
            $template = SPEAKER_PLUGIN_URL.'/templates/single-speaker.php';
        }
    }

    return $template;
}, PHP_INT_MAX);

Tip: Confirm your constant SPEAKER_PLUGIN_URL is actually what you expect it to be.

filipecsweb
  • 171
  • 1
  • 10