Step-by-Step Guide: How to Uninstall a Theme in WordPress

Home » Snippets » Step-by-Step Guide: How to Uninstall a Theme in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever installed a WordPress theme only to realize it doesn’t suit your needs or expectations? If so, you’re not alone. It’s common to change themes until you find the perfect one for your website. Knowing how to properly uninstall a WordPress theme is crucial in this process. In this article, we’ll guide you through the steps to safely and effectively uninstall a WordPress theme without losing your data.

					function wpturbo_theme_uninstaller() {
    // Switch theme to a default one
    switch_theme('twentysixteen');
    // Get the current theme after the switch above
    $current_theme = wp_get_theme();
    if ('twentysixteen' !== $current_theme->get('Name')) {
        // If it didn't switch, we require manual intervention
        wp_die('Manually need to switch theme as automatic switching failed.');
    }
    // Deletes the theme
    delete_theme('your-theme-directory-name');
}
register_deactivation_hook( __FILE__, 'wpturbo_theme_uninstaller' );
				

The function wpturbo_theme_uninstaller() is declared at the beginning of the provided PHP code snippet. This function is named in a way that implies that it would be responsible for uninstalling a particular theme whenever it is called during the code execution.

First, we are calling switch_theme('twentysixteen') function to switch the current theme to a default WordPress theme (Twenty Sixteen), in case the theme your user wanted to uninstall was active. It’s crucial to perform this step before you delete a theme to avoid undesirable consequences. The switch_theme() is a built-in WordPress function that is used to change the Active Theme.

Following that, the function wp_get_theme() is used to obtain a WP_Theme object for the current theme (after switching). wp_get_theme() retrieves the current WordPress theme or a certain theme’s WP_Theme object, which can then allow you to fetch theme information using various methods.

$current_theme = wp_get_theme();

A conditional statement is nested in the wpturbo_theme_uninstaller() function. If the switch wasn’t successful (the current theme isn’t "twentysixteen"), a failed operation message is displayed using wp_die(). This function shows an error message and then stops script execution, ensuring the user knows that manual intervention is needed to switch the theme.

if ('twentysixteen' !== $current_theme->get('Name')) {
    wp_die('Manually need to switch theme as automatic switching failed.');
}

Once the theme switch has been done successfully, the delete_theme('your-theme-directory-name') function is used to delete the theme from the WordPress themes directory. You need to replace ‘your-theme-directory-name’ with the exact folder name of the theme you want to delete.

delete_theme('your-theme-directory-name');

Finally, the register_deactivation_hook() function is used to set a function that will be triggered when the theme is deactivated. This means that every time the user deactivates your theme, the theme will also be deleted entirely. It basically ties the uninstall function we created to the theme deactivation process.

register_deactivation_hook( __FILE__, 'wpturbo_theme_uninstaller' );

Replace __FILE__ with the absolute path to the file that contains the plugin headers. In this case, it’s usually the theme’s main file.

This code won’t work as expected if put in a theme because ‘register_deactivation_hook’ is meant for plugins. If you need to accomplish this with a theme, you’d have to prompt the user to switch theme before you delete it.

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