Updates using Transient Data

,

Here some useful activation and update hooks.

<?php

 add_action( 'upgrader_process_complete', 'some_plugin_upgrade_completed', 10, 2 );
 function some_plugin_upgrade_completed( $upgrader_object, $options ) {
 	// The path to our plugin's main file
 	$our_plugin = SOME_PLUGIN_BASENAME;
 	// If an update has taken place and the updated type is plugins and the plugins element exists
 	if( $options['action'] == 'update' && $options['type'] == 'plugin' && isset( $options['plugins'] ) ) {
 		// Iterate through the plugins being updated and check if ours is there
 		foreach( $options['plugins'] as $plugin ) {
 			if( $plugin == $our_plugin ) {
 				// Set a transient to record that our plugin has just been updated
 				set_transient( 'some_plugin_updated', 1 );
 			}
 		}
 	}
 }
 
 add_action( 'admin_notices', 'some_plugin_display_update_notice' );
 function some_plugin_display_update_notice() {
     // Check the transient to see if we've just updated the plugin
     if( get_transient( 'some_plugin_updated' ) ) {
         echo '<div class="notice notice-success">' . __( 'Thanks for updating', 'some_plugin' ) . '</div>';
         some_plugin_db_sync(); //I like to do a db sync to ensure any updates to DB are done. This is usually the dbDelta function of the plugin.
         delete_transient( 'some_plugin_updated' );
    }
 }

 add_action( 'admin_notices', 'some_plugin_display_install_notice' );
 function some_plugin_display_install_notice() {
    // Check the transient to see if we've just activated the plugin
    if( get_transient( 'some_plugin_activated' ) ) {
        echo '<div class="notice notice-success">' . __( 'Thanks for installing', 'some_plugin' ) . '</div>';
        // Delete the transient so we don't keep displaying the activation message
        delete_transient( 'some_plugin_activated' );
    }
 }

 add_action( 'admin_notices', 'some_plugin_display_deactivate_notice' );
 function some_plugin_display_deactivate_notice() {
 	if (get_transient( 'some_plugin_deactivated' ) ) {
 		echo '<div class="notice notice-success">' . __( 'Thanks for deactivating Some Plugin', 'some_plugin' ) . '</div>';
 	}
 	delete_transient( 'some_plugin_deactivated' );
 }

 

Skills

Posted on

April 8, 2022

Submit a Comment

Your email address will not be published.