WordPress-Plugin-Boilerplate icon indicating copy to clipboard operation
WordPress-Plugin-Boilerplate copied to clipboard

Custom template

Open Jeremaya45 opened this issue 8 years ago • 4 comments

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

Jeremaya45 avatar Dec 03 '17 14:12 Jeremaya45

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

slushman avatar Dec 03 '17 14:12 slushman

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/

dingo-d avatar Dec 03 '17 15:12 dingo-d

@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

Jeremaya45 avatar Dec 03 '17 17:12 Jeremaya45

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

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

NeaMitika avatar Nov 05 '20 17:11 NeaMitika