Trying to add a page template for my custom post type from a plugin, but it will not display in the template dropdown unless file is in theme

I have a custom post type “publications” in my mu-plugin “publications-plugin”, and I want to include an optional page template to use with this CPT as well as with regular posts and pages.

My template has the required stuff:

<?php

/*
Template Name: Publication special
Template Post Type: publication, page, post
*/

I am using the “theme_page_templates” filter to add a path to my plugin’s optional page template:

function wp234234_theme_page_templates( $theme_templates ) {
    $theme_templates['/absolute/path/to/template-publication-special.php'] = 'Publication special'
    return $theme_templates;
}

if If var_dump the $theme_templates variable I’ll see something like

[archive-chart.search.php] => Search Page
[template-bootstrap.php] => Bootstrap
[template-donate.php] => Donation Page
[template-fullpage.php] => Full Page
[template-page-staff.php] => Staff Page
[template-search.php] => Search Page
[template-signup.php] => Signup Page
[template-publication-special.php] => Publication Special

But if I create a new a new “publication” I cannot see the template in “page attributes” it IS however, available for pages. If I move the template file from the plugin to the theme tho, it will now become available in both pages and “publications”

Is there another filter I need to run so that this thing is available for my CPTs as well as regular ol pages?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The filter hook you’re using is theme_<post type>_templates, which means that the <post type> part is dynamic and it’s the post type in which you want the custom template be added to the Template dropdown. So since the template is enabled for three post types, you would do so to add the template to the dropdown:

add_filter( 'theme_publication_templates', 'wp234234_theme_page_templates' ); // publication CPT
add_filter( 'theme_page_templates', 'wp234234_theme_page_templates' );        // regular Pages
add_filter( 'theme_post_templates', 'wp234234_theme_page_templates' );        // regular Posts

Or you can instead use the theme_templates hook: ( Note: This hook runs before the ones above. )

add_filter( 'theme_templates', 'wpse_387479_theme_templates', 10, 4 );
function wpse_387479_theme_templates( $post_templates, $theme, $post, $post_type ) {
    if ( in_array( $post_type, array( 'publication', 'page', 'post' ) ) ) {
        $post_templates['/absolute/path/to/template-publication-special.php'] = 'Publication special';
    }

    return $post_templates;
}

PS: Just change the “publication” to the correct post type, if what I used is wrong?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x