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

Home » Snippets » How to Uninstall 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

If you’re a WordPress user, you know how easy it is to install a theme. All you have to do is find and install the theme you want, activate it, and you’re good to go. But what happens if you instal a theme that you don’t like? Sure, you can just activate a new theme, but the old one will still be sitting there, taking up space on your server. The solution? Uninstalling the theme altogether. In this article, we’ll show you how to uninstall a theme in WordPress, step by step.

					function wpturbo_uninstall_theme() {
    $stylesheet = 'your_theme_name'; // Replace this with your theme's stylesheet name
    $theme = wp_get_theme($stylesheet);
    $theme->delete();
}
add_action('switch_theme', 'wpturbo_uninstall_theme');
				

The given code snippet demonstrates how to uninstall a WordPress theme using code. The code consists of two parts: a function, wpturbo_uninstall_theme() and an action hook, add_action().

The purpose of the wpturbo_uninstall_theme() function is to delete the currently active theme on the website. Firstly, the $stylesheet variable is used to store the name of the theme’s stylesheet. This should be replaced with the name of the theme you want to uninstall.

Next, the wp_get_theme() function is used to retrieve the theme’s object based on the stylesheet name provided. This function gets the theme data for a given theme and returns an object of the WP_Theme class.

Finally, the delete() method is called on the theme object retrieved from wp_get_theme(). This method deletes the theme folder and all its contents from the server.

In order to execute the wpturbo_uninstall_theme() function, it needs to be hooked into a WordPress action. The add_action() function is used for this. In this case, we are using the switch_theme event, which is when a new theme is activated. When the switch_theme event is fired, the wpturbo_uninstall_theme() function is executed, deleting the previously active theme.

It’s important to note that this code should be used with caution. Uninstalling a theme using code can result in unintended consequences if used incorrectly. Ensure you have a full backup of your website before attempting to use this code.

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