DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.66k stars 2.25k forks source link

Custom template #455

Open Jeremaya45 opened 6 years ago

Jeremaya45 commented 6 years ago

Hello,

I tried to implement a functionality of adding custom template to wordpress. This is the function I added to file class-myplugin-public.php

function my_custom_template($single) {

        global $wp_query, $post;
        /* Checks for single template by post type */
        if ( $post->post_type == 'post' ) {
            if ( file_exists( PLUGIN_PATH . '/public/partials/bn-single-post.php' ) ) {
                return PLUGIN_PATH . '/public/partials/bn-single-post.php';
            }
        }

        return $single;

    }

The template file itself is located at public/partials/bn-single-post.php

I also added to define_public_hooks function the following filter $this->loader->add_filter( 'single_template', $plugin_public, 'my_custom_template' );

However it does't work. It seems like PLUGIN_PATH is not defined. This is the source of where I grabbed the code: https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder

slushman commented 6 years ago

You would need to define PLUGIN_PATH in the index.php file as a constant to use it that way.

dingo-d commented 6 years ago

This seems like a better way (if you're already using OOP in your plugin) to add page templates to your plugin: http://www.wpexplorer.com/wordpress-page-templates-plugin/

Jeremaya45 commented 6 years ago

@slushman I added to the top level index.php the following lines

if ( !defined( 'PLUGIN_PATH' ) ) {
    define( 'PLUGIN_PATH', plugin_dir_path( __FILE__ ) );

}

but it still doesn't work

NeaMitika commented 3 years ago

https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/552

check this out i made this for my CPT template maby it can help you