函数文档

delete_theme()

💡 云策文档标注

概述

delete_theme() 函数用于从 WordPress 系统中删除指定主题,包括主题文件和关联的翻译文件。它处理文件系统操作、权限验证,并触发相关 Hook。

关键要点

  • 函数接受两个参数:$stylesheet(必需,主题的样式表标识符)和 $redirect(可选,删除完成后的重定向页面)。
  • 返回值类型多样:成功时返回 true,$stylesheet 为空时返回 false,失败时返回 WP_Error,需要文件系统凭证时返回 null。
  • 内部流程包括验证参数、请求文件系统凭证、连接 WP_Filesystem、删除主题目录和翻译文件,并清理缓存。
  • 触发两个 Hook:delete_theme(删除前)和 deleted_theme(删除后),便于开发者扩展操作。
  • 支持多站点环境,会自动从网络允许的主题中禁用该主题。

代码示例

if(delete_theme('twentyseventeen')) {
     echo 'Twenty Seventeen has been deleted.';
}

注意事项

  • 使用前需确保有足够的文件系统权限,否则可能返回 null 或 WP_Error。
  • 在多站点中,删除主题会同时禁用其在网络中的所有站点。
  • 函数会清除主题缓存和更新信息,确保系统状态同步。

📄 原文内容

Removes a theme.

Parameters

$stylesheetstringrequired
Stylesheet of the theme to delete.
$redirectstringrequired
Redirect to page when complete.

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.

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 theme 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.' ) );
	}

	/**
	 * Fires immediately before a theme deletion attempt.
	 *
	 * @since 5.8.0
	 *
	 * @param string $stylesheet Stylesheet of the theme to delete.
	 */
	do_action( 'delete_theme', $stylesheet );

	$theme = wp_get_theme( $stylesheet );

	$themes_dir = trailingslashit( $themes_dir );
	$theme_dir  = trailingslashit( $themes_dir . $stylesheet );
	$deleted    = $wp_filesystem->delete( $theme_dir, true );

	/**
	 * Fires immediately after a theme deletion attempt.
	 *
	 * @since 5.8.0
	 *
	 * @param string $stylesheet Stylesheet of the theme to delete.
	 * @param bool   $deleted    Whether the theme deletion was successful.
	 */
	do_action( 'deleted_theme', $stylesheet, $deleted );

	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' );
			$wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.l10n.php' );

			$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 );
	}

	// Clear theme caches.
	$theme->cache_delete();

	// Force refresh of theme update information.
	delete_site_transient( 'update_themes' );

	return true;
}

Hooks

do_action( ‘deleted_theme’, string $stylesheet, bool $deleted )

Fires immediately after a theme deletion attempt.

do_action( ‘delete_theme’, string $stylesheet )

Fires immediately before a theme deletion attempt.

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes