Trying to add a custom post type page template from plugin into Sage 10 theme

870 views Asked by At

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

There are 2 answers

4
Abdul Hanan On

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;});
0
filipecsweb On

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.