3

Is there any way to test if a function running only on plugin update is successfully running? Right now I am calling function if WP version present in configuration file is not same as plugin version's value in database and so testing by changing value of constant. But is there any other better approach?

0

Is there any way to test if a function running only on plugin update is successfully running?

Yes.

Background

When you update a plugin, WordPress does several actions behind the scenes.

  1. Download the new version of the plugin
  2. Deactivate the old plugin version
  3. Delete the old plugin version
  4. Extract the new version
  5. Activate the new version

In addition to the hooks that WordPress fires during those actions, it fires upgrader_process_complete. This hook passes two parameters to the callback: $this, which is obviously the object instance, and $options['hook_extra'], which are extra arguments passed to the filter hooks called by WP_Upgrader::install_package(). The Plugin_Upgrader class passes the following array:

array( 'plugin' => $plugin,
       'type' => 'plugin',
       'action' => 'update',
)

Upgrader Hook

Therefore, if we want to run a function on a plugin upgrade, we can use this hook.

add_action( 'upgrader_process_complete', 'wpse_262412_upgrader_process_complete' );
function wpse_262412_upgrader_process_complete( $instance, $extras ) {
  //* Only interesting when 'our' plugin updates
  if(
    'plugin' !== $extras[ 'type' ] &&
    'update' !== $extras[ 'action' ] &&
    'my-plugin-name' !== $extras[ 'plugin' ]
  ) {
    return;
  }
  //* Do something useful after your plugin updates
  wpse_262412_something_useful();
}

Checking

If you are developing a plugin and want to makes that a particular function runs on plugin update, then you can add an option to the database:

function wpse_262412_something_useful() {
  include_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  $data = get_plugin_data( 'my-plugin-name' );
  update_option( 'my-plugin-update-option', $data[ 'version' ] );
}

Then we can add an action to init that checks that option value. If the option value is equal to the current plugin version, then we know that the function has run. Then we can make sure we can see that the function has been run, like add an admin notice.

add_action( 'init', 'wpse_262412_init' );
function wpse_262412_init() {
  $data = get_plugin_data( 'my-plugin-name' );
  $option = get_option( 'my-plugin-update-option' );
  if( $data[ 'version' ] === $option ) {
    //* The plugin update function ran successfully
    add_action( 'admin_notices', 'wpse_262412_update_notice' );
  }
}

function wpse_262412_update_notice() {
?>
  <div class="updated notice">
    <p><?php _e( 'The function has run correctly.', 'wpse-262412' ); ?></p>
  </div>
  <?php
}

But is there any other better approach?

Is this better than your approach? I don't know. Depends on your application. It doesn't involve opening and reading a configuration file, so it will likely be faster. But probably not noticeably much. I wouldn't do either method on a production site. Make sure your code works on a development server, then use it. If you're talking about comparing plugin version numbers, that seems like as good as any way to do it to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.