Add Snippet To Project
Sometimes, as a WordPress user, you may find yourself wanting to remove or uninstall a theme that you no longer need or use. Whether you installed it temporarily to test it out or just decided it wasn’t the right fit, removing a theme can keep your website organized and running smoothly. But how do you do it? In this article, we will show you how to remove a WordPress theme the right way, including backups, precautions, and step-by-step instructions.
function wpturbo_remove_theme() {
$theme = wp_get_theme();
if ( 'Theme Name' === $theme->get( 'Name' ) ) {
wp_delete_theme( $theme->get_stylesheet() );
}
}
add_action( 'after_switch_theme', 'wpturbo_remove_theme' );
The code snippet above shows you how to remove a WordPress theme programmatically. This can be useful if you need to uninstall a theme but are unable to do so via the WordPress dashboard.
To use this code, you will need to create a new function called wpturbo_remove_theme()
. Inside this function, we first get the current active theme using the wp_get_theme()
function.
$theme = wp_get_theme();
Next, we check if the name of the active theme matches the name of the theme that needs to be removed. If it is a match, we use the wp_delete_theme()
function to delete the theme.
if ( 'Theme Name' === $theme->get( 'Name' ) ) {
wp_delete_theme( $theme->get_stylesheet() );
}
Remember to replace Theme Name
with the name of the theme that you want to remove from your WordPress installation. Additionally, the wp_delete_theme()
function takes the stylesheet name of the theme as its argument. To get the current active theme’s stylesheet name, we use the get_stylesheet()
method of the $theme
object.
Finally, we hook the wpturbo_remove_theme()
function into the after_switch_theme
action using the add_action()
function. This action is fired whenever a new theme is switched to, and we use it to execute the code that removes the old theme.
Overall, this is a useful code snippet that allows you to programmatically remove a WordPress theme without having to go through the dashboard. Just remember to use it wisely and with caution!