tommcfarlin/page-template-example

Help: Inactivates Template Pages of Theme itself

patrickf0 opened this issue · 13 comments

Hi there,

your class works fine, but I´ve a slight problem. It integrates my template Page of my Plugin I created, but it seems taht the page templates within the theme itself are overwritten, or no longer visible.

Can you give me a hint?

P.S.: I´m using your plugin boilerplate and I want to integrate this class in the plugin class.

Right now, this is a bug that will need to be fixed.

Ultimately, I'd like to release a more refined plugin than the example in this repository; however, this is all I have right now.

Try changing:
$templates = wp_cache_get( $cache_key, ‘themes’ );
to:
$templates = wp_get_theme()->get_page_templates();

Thanks, @CoryTrevor.

Assigning this to the next milestone.

Works nicely!

Hi
Thanks @tommcfarlin for this great plugin example, helped me a lot!
I just added these lines to bottom of the constructor (see pull):

$templates = wp_get_theme()->get_page_templates();
$templates = array_merge( $templates, $this->templates ); // This wasn't really necessary to add I noticed later..

This worked as well, don't know what is best though. @CoryTrevor solution works as well. I wanted to leave as much as possible as it is and just add some functionality where needed. These templates already exist and wp knows they do so I think they just need to be added in the end.

Hello,
I just wanted to say thanks to @tommcfarlin as well! This is incredibly useful - thank you.
(Of course to @CoryTrevor too for the great fix ;) )
Harri

@MBing

Thanks for the update. As soon as I have a chance to get back to the working on this plugin, I'll be sure to handle this (unless a pull request is issued!)

@HarriBellThomas - sure thing! Definitely couldn't be doing this without the contributors :).

I used @CoryTrevor 's solution to good effect. Thanks!

Awesome! Thanks @tommcfarlin and @MBing !!!

To remove the templates, I added:

public function deactivate( $network_wide ) {
    foreach($this as $value) {
        page-template-example::delete_template( $value );
    }

} // end deactivate

/*--------------------------------------------*
 * Delete Templates from Theme
*---------------------------------------------*/
public function delete_template( $filename ){               
    $theme_path = get_template_directory();
    $template_path = $theme_path . '/' . $filename;  
    if( file_exists( $template_path ) ) {
        unlink( $template_path );
    }

    // we should probably delete the old cache
    wp_cache_delete( $cache_key , 'themes');
}


//and this in the constructor
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );

The de/register activation hooks need to to refer to static functions rather than defining them in the constructor.

Other than that, the above code looks good!

If you create a pull request, I'd be happy to merge it.

I did a PR a couple of months ago as you suggested.. ;)

@MBing merged! Thought I had already done that. My bad 👎, but all fixed now 👍.