4

I am trying to figure out how to use another single.php for a custom plugin I'm making. It's for a custom post type.

Because if people install this plugin, they won't have the custom single-product.php in their theme folder. That's why I want it to be in the plugin folder.

Is there a way to change the path of this custom post type's single.php or a way to automatically generate the file in the theme folder after installing this plugin?

Thanks in advance

7

I think a hook into template_include like described here could be a proper way to do this.

Code could be like this:

add_filter('template_include', 'my_plugin_templates');
function my_plugin_templates( $template ) {
    $post_types = array('post');

    if (is_singular($post_types)) {
        $template = 'path/to/singular/template/in/plugin/folder.php';
    }

    return $template;
}
  • Thanks a lot. That works! Is there also a way to specify a custom stylesheet for this? The stylesheet I imported in my plugin.php doesn't load up there. – user3428971 Nov 30 '15 at 11:12
  • Puh, never done this, but you should be able to load stylesheets just for your plugin, see here. – flomei Nov 30 '15 at 11:31
1

Plugins should never* have any visual effect which is not a widget or a shortcode. If you feel the need to manipulate the theme files then you are most likely doing it wrong.

*there are always exceptions as targeting a specific theme or doing something which is so theme agnostic (like popup/lightbox) that there is very little chance of breaking the theme or getting an ugly result.

EDIT from COMMENTS

This is a border line issue without any white/black type of answer. From what you describe you should have done a theme and not a plugin, but I get you were given the assignment. The right way is like with woocommerce - shortcodes and widgets that display the content managed as CPT otherwise you are more likely not to play nice with all the themes

  • I am making a project for school. In this project I am creating a "products" plugin. For this plugin I"m using custom post types. The way this post should look like has to be styled right? What would be a better way to style it then? I also added a gallery to this custom post type. Now need to figure out how to style this gallery and make it nice with jquery – user3428971 Nov 30 '15 at 11:15
  • This is a border line issue without any white/black type of answer. From what you describe you should have done a theme and not a plugin, but I get you were given the assignment. The right way is like with woocommerce - shortcodes and widgets that display the content managed as CPT otherwise you are more likely not to play nice with all the themes. – Mark Kaplun Nov 30 '15 at 11:21
1

Where get_custom_post_type_template is the function WordPress should call when the content is being retrieved. Note that the filter function the plugin defines must return the a full path to a template file or the resulting page will be blank. The template file should have the same entries as the single.php file has in the theme. For more information visit the site.

 <?php
    function get_custom_post_type_template($single_template) {
    global $post;

       if ($post->post_type == 'my_post_type') {
          $single_template = dirname( __FILE__ ) . '/post-type-template.php';
       }
    return $single_template;
    }
    add_filter( 'single_template', 'get_custom_post_type_template' );
?>
  • Can you explain a little bit in addtion to posting the code, thanks – bravokeyl Sep 7 '16 at 12:02

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.