0

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?

0
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?

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.