How to Update a WordPress Theme: A Step-by-Step Guide

Home » Snippets » How to Update a WordPress Theme: A Step-by-Step Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Themes are an essential part of any WordPress website’s design and functionality. They often provide the necessary tools to change the look and feel of the site and can add new features. However, themes require updates over time to ensure that they stay secure and up-to-date with the latest WordPress version. In this article, we’ll show you how to update your WordPress theme safely and efficiently. Whether you have a custom-made theme or a third-party theme, updating it will be a breeze with these steps.

					function wpturbo_update_theme() {

    $theme = wp_get_theme(); // Gets the current theme

    // Updater
    require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
    $upgrader = new WP_Upgrader();
    $upgraded = $upgrader->upgrade($theme->stylesheet);

    // Output
    if ($upgraded === true) {
        echo '<div id="message" class="updated notice is-dismissible"><p>Your theme has been updated.</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
    } else {
        echo '<div class="error"><p>There was an error updating your theme.</p></div>';
    }
}
add_action( 'admin_init', 'wpturbo_update_theme' );
				

Updating a theme in WordPress is a crucial task to keep your website secure and up-to-date. In this tutorial, I will show you how to use a code snippet to update your WordPress theme programmatically.

The first line of the code snippet is used to get the current theme object of the website. We use the built-in WordPress function wp_get_theme() and assign it to a variable called $theme.

$theme = wp_get_theme(); // Gets the current theme

Next, we need to require the WordPress Theme Upgrader class to use its upgrade() method which will do the actual updating of the theme. We use require_once() to make sure that this class only gets loaded one time in case multiple plugins or themes are trying to load it.

require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');

Then we create an instance of the WP_Upgrader() class and use its upgrade() method to update our theme. We pass the name of the stylesheet of the current theme as a parameter to this method.

$upgrader = new WP_Upgrader();
$upgraded = $upgrader->upgrade($theme->stylesheet);

In the above code snippet, we use a simple conditional statement to check for success or failure of the update process. If $upgraded is true, then the update was successful so we display an admin notice with a success message using HTML markup.

if ($upgraded === true) {
    echo '<div id="message" class="updated notice is-dismissible"><p>Your theme has been updated.</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
}

If $upgraded returns false, then we display an error message on the admin dashboard using HTML markup.

else {
    echo '<div class="error"><p>There was an error updating your theme.</p></div>';
}

Finally, we hook our function to the admin_init action using the add_action() function. This ensures that our code is run when the WordPress admin dashboard loads.

add_action( 'admin_init', 'wpturbo_update_theme' );

With these simple steps, you can now update your WordPress theme programmatically whenever you make changes to your theme files.

Register an account to save your snippets or go Pro to get more features.