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

Create page template from inside plugin #552

Closed NeaMitika closed 3 years ago

NeaMitika commented 3 years ago

I was wondering, is there any way to create a page/post template from inside the plugin? I know its possibile to create it from inside a wordpress theme folder, but i cant find any working code online to create it from a plugin.. Maby this could be a feature for a next boilerplate update?

NeaMitika commented 3 years ago

After a lot of GUGLING i have found a solution... this one is not perfect... but is a start

Ofc you have to make a template file like Wordpress is asking... i have saved mine here (templates/single-pianta.php) inside plugin's folder

    public function template_custom_post_type( $template ) {
        global $post;

        if (is_singular( 'pianta' ) ) {
            $template = plugin_dir_path(__DIR__).'templates/single-pianta.php';
        }

        elseif (is_archive( 'pianta' ) ){
            $template = plugin_dir_path( __DIR__ ).'templates/archive-pianta.php';
        }

        return $template;
    }
    $this->loader->add_filter( 'template_include', $plugin_admin, 'template_post_type_template' ) ;