Friday, July 22, 2022
HomeWordPress DevelopmentPlugin customized publish template; with out overriding all posts

Plugin customized publish template; with out overriding all posts


Undecided if there’s a greater solution to do it, however you should use the theme_<publish sort>_templates filter so as to add your customized template to the “Template” dropdown, after which use the template_include filter to make sure the proper file is loaded.

So one thing like this could work:

add_filter( 'template_include', 'my_events_template_include' );
perform my_events_template_include( $template ) {
    if ( 'occasions' === get_post_type() ) {
        $file = plugin_dir_path( __FILE__ ) . 'templates/single-events.php';
        return ( $file === get_page_template_slug() ) ? $file : $template;
    }

    return $template;
}

add_filter( 'theme_events_templates', 'my_plugin_theme_events_templates' );
perform my_plugin_theme_events_templates( $post_templates ) {
    $file = plugin_dir_path( __FILE__ ) . 'templates/single-events.php';
    $post_templates[ $file ] = __( 'Single Occasions', 'your-text-domain' );
    return $post_templates;
}

Notice: The array key of $post_templates must be a file title/path relative to the energetic theme listing, however since we’re utilizing a full filesystem path, then we had to make use of the template_include filter to load the proper file from the plugin listing.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments