Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress: Hook for when a theme update was installed

Tags:

wordpress

My theme creates a css file when the theme settings are being update by the user. This works without an issue.

Now I also need to create the css file, when the theme is being updated from within the dashboard or when an automatic update occurs.

I have looked into upgrader_process_complete and upgrader_post_install based on these examples: https://codex.wordpress.org/Plugin_API/Action_Reference/upgrader_process_complete and https://wordpress.stackexchange.com/questions/215063/limit-filter-upgrader-post-install-to-a-single-plugin but for some reason, my function is not being fired after I update the theme.

The following code is in my functions.php of my theme.

function create_css_file() {
   //code to create css file
}

function my_upgrade_function( $options ) {
    create_css_file();
}
add_action( 'upgrader_process_complete', my_upgrade_function, 10, 2);

//I also tried this. No luck
add_filter( 'upgrader_post_install', my_upgrade_function, 100, 0 );

Am I missing something fundamental?

Update #1: I have lowered the priority by using add_action( 'upgrader_process_complete', my_upgrade_function, 150); Now it seems to work, but when the create_css_file function is called, it uses the previous version, and not the newly installed version. When I check the CSS file that was created, it's being newly generated, but based on the old version. Not sure why this happens.

I also change it to add_action( 'upgrader_process_complete', my_upgrade_function, 1); - Same issue. The old code is being used, rather than the newly installed one.

Update #2: Okay, I came to the conclusion, that both, upgrader_process_complete and upgrader_post_install, are being fired while the old theme is still installed, which kind of defeats the entire purpose.

I quickly ran a test, where I removed the entire upgrader_process_complete section, and uploaded it to my private repository. I then went to the theme and updated it in the dashboard, but the upgrader_process_complete was still fired, again creating a css file based on the theme that was installed, rather than the newly installed one. But as I said, I actually removed the entire upgrader_process_complete code section, so it should not have fired in the first place.

This leads me to believe, that upgrader_process_complete is being fired while the old theme is still installed.

Which brings me back to square one. How do I call a function, after a theme update has been successfully installed?

like image 628
Kevin M Avatar asked Feb 28 '26 20:02

Kevin M


1 Answers

Having a similar issue I have decided to store the version of the theme in an option and check it against the current theme version:

add_action('after_setup_theme', 'PREFIX_check_theme_version');
function PREFIX_check_theme_version() {

  $current_version = wp_get_theme()->get('Version');
  $old_version = get_option( 'PREFIX_theme_version' );

  if ($old_version !== $current_version) {
    // do some cool stuff
    create_css_file();

    // update not to run twice
    update_option('PREFIX_theme_version', $current_version);
  }
}

Of course you should remove the option on theme deactivation:

add_action('switch_theme', 'PREFIX_options_removal');

function PREFIX_options_removal () {
  delete_option('PREFIX_theme_version');
}

It's hacky, but gets the job done

like image 136
Łukasz Mazan Avatar answered Mar 04 '26 15:03

Łukasz Mazan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!