WordPress – Determining When a Plugin or Theme is Updated

WordPress - Do Something When A Plugin or Theme Updates

Recently, I was faced with an unavoidable situation where I had to alter a third-party plugin’s code. The plugin is one I consider a “well-made” plugin, incorporating apply_filters or do_action all over the place. However, the hooks weren’t available in a specific spot where I needed them for a project.

Of course, the downside of altering third-party plugin or theme code is maintainability. Every time the plugin or theme releases an update, the custom code must be reintegrated manually once the upgrades are complete. So, tracking when a plugin or theme is updated becomes important.

A Nasty Choice

The plugin I had to alter was actually the ShipStation Integration1Full disclosure, this is a affiliate link, and I make money if you make any purchases on WooCommerce.com after visiting this URL., made available by WooCommerce.com. As part of a recent Warehouse Operations application I built, a key component was identifying inventory locations. ShipStation allows for a Location field for each order item, according to the documentation, but the WooCommerce plugin doesn’t provide this value in its XML export.

The challenge was to find a way to include the Location field in the export. However, as mentioned, there’s not an apply_filters available to alter the export XML. The only option currently available to remedy this is to alter the plugin code directly.2I’ve actually submitted a ticket to ask the developers if more hooks can be added to help this situation. I’ve yet to hear anything back, but will update this article if/when I do.

The Problem

Editing plugin or theme files directly is never a good idea. If you do, any time the item updates in the future, the altered code will be overwritten. This creates some debt by requiring a developer to reintegrate the customizations after the updates are complete.

Knowing when updates occur is usually an easy thing to keep up with because, prior to WordPress 5.5, plugin and theme updates were a manual process by default. However, 5.5 introduces automatic updates for plugins and themes in core, and there’s now a possibility for unattended updates.

With this possibility, we need a reliable way to determine when a specific plugin or theme is updated, even when we, as the developer, aren’t involved.

The Solution

WordPress core doesn’t provide a specific hooks for our purposes, like plugin_updated or theme_updated. Yet.3I’ve asked the Core Team to consider adding hooks such as these, given the increased use cases now that unattended, automatic updates are part of core. The ones that are available are more generic, and they are: upgrader_pre_install, pre_auto_update, upgrader_process_complete, and upgrader_post_install. The one of particular import in our use-case is upgrader_process_complete.

The upgrader_process_complete action fires when the entire upgrader process is complete for all items. However, with the second argument, $hook_extra, we can gather some information about what was updated. Printing out the entire $hook_extra array results in something like this:

Lucky for us, $hook_extra['plugins'], if available, will give us relative paths of plugins that were just successfully updated. We just need to filter through the values to determine whether a specific plugin was updated. Example:

add_action( 'upgrader_process_complete', 'de_upgrader_process_complete', 10, 2 );

function de_upgrader_process_complete( $upgrader_object, $options ) {
    $shipstation_updated = false;

    if ( isset( $options['plugins'] ) && is_array( $options['plugins'] ) ) {
        foreach ( $options['plugins'] as $index => $plugin ) {
            if ( 'woocommerce-shipstation-integration/woocommerce-shipstation.php' === $plugin ) {
                $shipstation_updated = true;
                break;
            }
        }
    }

    if ( ! $shipstation_updated ) {
        return;
    }

    // Do something when ShipStation plugin has been updated.
}

We’re checking for $hook_extra['plugins']‘s existence and looping through it to find our specific plugin. If we do, we update a flag. After a quick “bail” check on the flag, we can run whatever code we need to when the ShipStation plugin has been updated.

In my case, I’d like to have an email sent to me to alert me that the plugin has been updated. Since I’m running this code on three sites, I’d like that email to tell me which site as well. Here’s the final code:

add_action( 'upgrader_process_complete', 'de_upgrader_process_complete', 10, 2 );

function de_upgrader_process_complete( $upgrader_object, $options ) {
    $site = get_bloginfo( 'name' );
    $shipstation_updated = false;

    if ( isset( $options['plugins'] ) && is_array( $options['plugins'] ) ) {
        foreach ( $options['plugins'] as $index => $plugin ) {
            if ( 'woocommerce-shipstation-integration/woocommerce-shipstation.php' === $plugin ) {
                $shipstation_updated = true;
                break;
            }
        }
    }

    if ( ! $shipstation_updated ) {
        return;
    }

    $subject = sprintf(
        __( '%s ShipStation Plugin Updated' ),
        $site
    );

    $message = sprintf(
        __( 'The ShipStation plugin on %s has been updated.' ),
        $site
    );

    wp_mail( '[email protected][dash]encode[dot]com', $subject, $message );
}

Fell free to customize this code to fit your needs. You could set a transient that triggers an admin notice. You could disable the plugin automatically until you get a change to reapply your customizations. There’s a lot of possibilities here.

You can also use similar code to parse $hook_extra['themes'] to detect when a specific theme is updated. The output is similar in that it’s a list of paths relative to wp-content/themes/.

If you do find this code helpful, leave me a comment letting me know!

Creative Filtering in “WP WooCommerce Mailchimp” Plugin

Creative Filtering In WordPress Plugins

I’ve went on and on about “well-built” plugins for WordPress. As much as I’d like plugins to “filter all the things”, in reality, they never account for all use cases. One such challenge I had recently involved the WP WooCommerce Mailchimp plugin.

(more…)

Games Done Quick-Inspired Donation Total

SGDQ 2019 Donation Tracker Re-Creation

Twice a year, gamers from all over the world come together to “speedrun” both new and old games alike. Games from every platform and every era. Even better, it’s a week-long marathon to raise money for charity, and it’s streamed live on Twitch for millions to follow along. Although I’m not a big gamer, I’ve been watching both Awesome Games Done Quick(AGDQ) and Summer Games Done Quick(SGDQ) for about five years now.

Each marathon gets better. More games, larger sponsors, more money raised, and, finally, better production quality. The SGDQ that just finished raised over $3 Million for Doctors Without Borders, and the production and organization is top-notch.1If you can’t watch the whole SGDQ 2019, at least watch the last few hours. The grand finale run of Chrono Trigger ended up smashing the total-money-raised record for any GDQ and was just insane. Specifically, I was impressed with the donation tracker.

(more…)

Local WordPress Development – How To Sync Content From Staging or Production

Local WordPress Development - How To Sync Uploads And Database From A Staging or Production Site

Given that WordPress is still growing1WP is about a third of the web now, as of April 2019, there’s no shortage of work for proper WordPress developers. In my case, work comes from several sources, all of it freelance. The vast majority of work I do is retainer. Agencies/companies need me to come into a sprint, usually for the stuff that’s a bit too much for juniors.

Since every project is different, the workflow is usually unique as well. Unless I’m involved with the engineering at project kick-off, I usually have to adapt to what’s been put in place by other developers. Typically that means writing custom shell scripts and aliases to help sync both code and content.

(more…)

Contact Form 7: Salesforce Integration

WordPress Contact Form 7 Salesforce Integration

Here’s another forms use-case for WordPress. You have a form built with Contact Form 7, WP Forms, Gravity Forms, etc. However, when the form is submitted, you also want to send the form data to a third party, like Salesforce, SugarCRM, Marketo, etc. Luckily, the form plugins I mentioned allow us to “hook in” to the processing flow.

In this tutorial, I’ll show you a quick way to send Contact Form 7(CF7) submission data to Salesforce. We’ll be using some core WordPress functions to help with the integration. The code is pretty straightforward, so we’ll just put it in our child theme’s1You are using a child theme, right? functions.php file.

(more…)

Contact Form 7 – Custom Validation Messages

WordPress - Contact Form 7 Contextual Validation Error Messages

In this month’s edition of “well-built plugins”, we’re taking a look at Contact Form 7 for WordPress. Specifically, we’ll be customizing validation error messages on the front end. The popular contact form builder plugin is quite easy to extend, so let’s jump right in.

(more…)