page-template-example icon indicating copy to clipboard operation
page-template-example copied to clipboard

Update template on theme switch and on plugin update

Open brasofilo opened this issue 11 years ago • 3 comments

My intention was to make a pull request, but I'm having issues with my fork triggering headers already sent on activation.

I gave up, deleted the fork, and will leave the tips here. Curiously, I used your technique in a plugin of mine without any glitches...

Here's how I used it:

Theme switch

public function switching_theme( $new_name, $new_theme )
{
    $old_theme = get_option( 'theme_switched' );

    $template_path_app = get_theme_root( $old_theme ) . "/$old_theme/template-example.php";    
    if( file_exists( $template_path_app ) )
        unlink( $template_path_app );

    $this->register_project_template();
}
add_action( 'switch_theme', array( $this, 'switching_theme' ), 10, 2 );

Plugin update In case our plugin is updatable via some repo.

// In the constructor
$this->plugin_slug   = plugin_basename( __FILE__ );

public function refresh_template(  $true, $hook_extra, $result ) {

    if( isset( $hook_extra['plugin'] ) &&  $hook_extra['plugin'] == $this->plugin_slug  ) {

        $this->register_project_template();
        $this->deregister_project_template();

    }

    return $true; 
} 
add_filter( 'upgrader_post_install', array( $this, 'refresh_template' ), 10, 3 );

brasofilo avatar Oct 11 '13 04:10 brasofilo

@brasofilo thanks for sharing this. when I have more time, I'll revisit this and take a look for integration into the plugin (as well as the headers already sent message).

In the mean time, I'm going to leave this issue open.

tommcfarlin avatar Oct 11 '13 14:10 tommcfarlin

Is this integrated?

klihelp avatar Mar 06 '14 21:03 klihelp

@klihelp No.

Once I finish the initial rewrite, I'll come back to this and see if it's not something that can be easily and quickly incorporated.

tommcfarlin avatar Dec 14 '16 05:12 tommcfarlin