How to Properly Remove a WordPress Theme from Your Website

Home » Snippets » How to Properly Remove a WordPress Theme from Your Website
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Whether you’re redesigning your website or just looking to clean up your WordPress themes, removing unused themes can help streamline your site. But sometimes uninstalling a theme can be confusing or even lead to problems with your site if not done correctly. In this article, we’ll guide you through the process of removing a WordPress theme safely and efficiently.

					function wpturbo_remove_theme() {
    $theme = wp_get_theme();
    $theme_name = $theme->get( 'Name' );
    $response = array(
        'success' => false,
        'message' => ''
    );

    // Check if theme is active
    if ( $theme->get( 'Status' ) == 'active' ) {
        $response['message'] = __( 'You cannot remove an active theme. Please switch to another theme first.', 'wpturbo' );
    } else {
        // Check if theme is still installed
        if ( wp_get_theme( $theme_name )->exists() ) {
            $is_deleted = delete_theme( $theme_name );
            if ( $is_deleted ) {
                $response['success'] = true;
                $response['message'] = __( 'Theme successfully deleted.', 'wpturbo' );
            } else {
                $response['message'] = __( 'Error deleting theme. Please try again.', 'wpturbo' );
            }
        } else {
            $response['message'] = __( 'Theme is not installed.', 'wpturbo' );
        }
    }

    wp_send_json( $response );
}
add_action( 'wp_ajax_wpturbo_remove_theme', 'wpturbo_remove_theme' );
				

The code snippet above demonstrates how to remove a WordPress theme using PHP code. This particular code is designed to work as an AJAX action in WordPress, hence the presence of wp_ajax_wpturbo_remove_theme action hook.

The first step in this code is to define the wpturbo_remove_theme() function. This function accepts no parameters. Inside the function, we create an empty response object and set the success key to false. We also get the current theme using the wp_get_theme() function and store it in the $theme variable. We then retrieve the name of the theme using the get() method of the $theme object and store it in the $theme_name variable.

Next, we check if the current theme is active. We do this by checking if the Status property of the $theme object is equal to active. If the theme is active, we update the message of the response to inform the user that an active theme cannot be deleted.

If the theme is not active, we check if it still exists. We do this by calling the exists() method of the wp_get_theme() function and passing the theme name to it. If the theme exists, we attempt to delete it using the delete_theme() function, which returns a boolean value indicating whether the delete was successful or not.

If the theme was successfully deleted, we set the success key of response object to true and update the message key to indicate that the theme was successfully deleted. If we were unable to delete the theme, we set the message key to indicate that there was an error. If the theme does not exist, we set the message key to indicate that the theme is not installed.

Finally, we call the wp_send_json() function to send the response object back to the client-side AJAX script as a JSON object. This function allows us to return the response to the client-side script in an organized way. After defining the wpturbo_remove_theme() function, we attach it to the wp_ajax_wpturbo_remove_theme action hook using the add_action() function. This ensures that the function executes when the specified AJAX action is triggered.

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