1. 首页
  2. WordPress 函数手册

delete_theme()

delete_theme( string $stylesheet, string $redirect =  )

删除主题
Remove a theme

目录锚点:#参数#返回#源码#笔记


参数(Parameters)

参数 类型 必填 说明
$stylesheet (string) 必需 要删除的主题的样式表。
$redirect (string) 可选 完成后重定向到页面。

返回(Return)

(bool|null|WP_Error)成功时为True,如果$stylesheet为空则为false,失败时为WP|Error。如果需要文件系统凭据才能继续,则为空。


源码(Source)

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 ) ) {
            require_once ABSPATH . 'wp-admin/admin-header.php';
            echo $data;
            require_once ABSPATH . 'wp-admin/admin-footer.php';
            exit;
        }
        return;
    }
 
    if ( ! WP_Filesystem( $credentials ) ) {
        ob_start();
        // Failed to connect. Error and request again.
        request_filesystem_credentials( $redirect, '', true );
        $data = ob_get_clean();
 
        if ( ! empty( $data ) ) {
            require_once ABSPATH . 'wp-admin/admin-header.php';
            echo $data;
            require_once 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',
            /* translators: %s: Theme name. */
            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;
}
更新版本 源码位置 使用 被使用
2.8.0 wp-admin/includes/theme.php:21 1 function 11

笔记(Notes)

删除主题“2117”:

delete_theme() 为WP2原创文章,链接:https://www.wp2.cn/functions/delete_theme/

发表评论

您的电子邮箱地址不会被公开。