delete_theme( string $stylesheet, string $redirect = '' )

Remove a theme


Description Description


Parameters Parameters

$stylesheet

(string) (Required) Stylesheet of the theme to delete.

$redirect

(string) (Optional) Redirect to page when complete.

Default value: ''


Top ↑

Return Return

(bool|null|WP_Error) True on success, false if $stylesheet is empty, WP_Error on failure. Null if filesystem credentials are required to proceed.


Top ↑

Source Source

File: wp-admin/includes/theme.php

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function delete_theme( $stylesheet, $redirect = '' ) {
    global $wp_filesystem;
 
    if ( empty( $stylesheet ) ) {
        return false;
    }
 
    if ( empty( $redirect ) ) {
        $redirect = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet );
    }
 
    ob_start();
    $credentials = request_filesystem_credentials( $redirect );
    $data        = ob_get_clean();
 
    if ( false === $credentials ) {
        if ( ! empty( $data ) ) {
            include_once( ABSPATH . 'wp-admin/admin-header.php' );
            echo $data;
            include( ABSPATH . 'wp-admin/admin-footer.php' );
            exit;
        }
        return;
    }
 
    if ( ! WP_Filesystem( $credentials ) ) {
        ob_start();
        request_filesystem_credentials( $redirect, '', true ); // Failed to connect, Error and request again.
        $data = ob_get_clean();
 
        if ( ! empty( $data ) ) {
            include_once( ABSPATH . 'wp-admin/admin-header.php' );
            echo $data;
            include( ABSPATH . 'wp-admin/admin-footer.php' );
            exit;
        }
        return;
    }
 
    if ( ! is_object( $wp_filesystem ) ) {
        return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
    }
 
    if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
        return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
    }
 
    // Get the base plugin folder.
    $themes_dir = $wp_filesystem->wp_themes_dir();
    if ( empty( $themes_dir ) ) {
        return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) );
    }
 
    $themes_dir = trailingslashit( $themes_dir );
    $theme_dir  = trailingslashit( $themes_dir . $stylesheet );
    $deleted    = $wp_filesystem->delete( $theme_dir, true );
 
    if ( ! $deleted ) {
        return new WP_Error( 'could_not_remove_theme', sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet ) );
    }
 
    $theme_translations = wp_get_installed_translations( 'themes' );
 
    // Remove language files, silently.
    if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
        $translations = $theme_translations[ $stylesheet ];
 
        foreach ( $translations as $translation => $data ) {
            $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
            $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
 
            $json_translation_files = glob( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '-*.json' );
            if ( $json_translation_files ) {
                array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
            }
        }
    }
 
    // Remove the theme from allowed themes on the network.
    if ( is_multisite() ) {
        WP_Theme::network_disable_theme( $stylesheet );
    }
 
    // Force refresh of theme update information.
    delete_site_transient( 'update_themes' );
 
    return true;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.8.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.