函数文档

make_site_theme()

💡 云策文档标注

概述

make_site_theme() 函数用于创建一个基于博客名称的新站点主题,并自动将其激活。函数会检查主题目录是否可写,并根据是否存在旧版样式文件调用不同的子函数来创建主题。

关键要点

  • 函数返回新创建主题的模板名称(字符串)或失败时返回 false。
  • 主题名称基于博客名称(blogname 选项)生成,并使用 sanitize_title() 处理为安全的模板标识符。
  • 创建过程包括检查主题目录是否已存在、确保 themes 目录可写、创建主题目录,并根据 wp-layout.css 文件的存在调用 make_site_theme_from_oldschool() 或 make_site_theme_from_default()。
  • 如果当前激活的主题是默认主题(WP_DEFAULT_THEME),则自动更新 template 和 stylesheet 选项以激活新主题。

代码示例

function make_site_theme() {
    // Name the theme after the blog.
    $theme_name = __get_option( 'blogname' );
    $template   = sanitize_title( $theme_name );
    $site_dir   = WP_CONTENT_DIR . "/themes/$template";

    // If the theme already exists, nothing to do.
    if ( is_dir( $site_dir ) ) {
        return false;
    }

    // We must be able to write to the themes dir.
    if ( ! is_writable( WP_CONTENT_DIR . '/themes' ) ) {
        return false;
    }

    umask( 0 );
    if ( ! mkdir( $site_dir, 0777 ) ) {
        return false;
    }

    if ( file_exists( ABSPATH . 'wp-layout.css' ) ) {
        if ( ! make_site_theme_from_oldschool( $theme_name, $template ) ) {
            // TODO: rm -rf the site theme directory.
            return false;
        }
    } else {
        if ( ! make_site_theme_from_default( $theme_name, $template ) ) {
            // TODO: rm -rf the site theme directory.
            return false;
        }
    }

    // Make the new site theme active.
    $current_template = __get_option( 'template' );
    if ( WP_DEFAULT_THEME === $current_template ) {
        update_option( 'template', $template );
        update_option( 'stylesheet', $template );
    }
    return $template;
}

注意事项

  • 函数在创建主题目录时使用 umask(0) 和 mkdir( $site_dir, 0777 ),这可能会影响文件权限设置,需确保服务器环境安全。
  • 如果创建主题失败(例如子函数返回 false),函数会返回 false,但注释中提到需要清理已创建的主题目录(TODO: rm -rf),实际实现中可能未处理此清理,开发者需注意潜在的文件残留问题。
  • 此函数自 WordPress 1.5.0 版本引入,主要用于升级或初始化过程中的主题创建。

📄 原文内容

Creates a site theme.

Description

Return

string|false

Source

function make_site_theme() {
	// Name the theme after the blog.
	$theme_name = __get_option( 'blogname' );
	$template   = sanitize_title( $theme_name );
	$site_dir   = WP_CONTENT_DIR . "/themes/$template";

	// If the theme already exists, nothing to do.
	if ( is_dir( $site_dir ) ) {
		return false;
	}

	// We must be able to write to the themes dir.
	if ( ! is_writable( WP_CONTENT_DIR . '/themes' ) ) {
		return false;
	}

	umask( 0 );
	if ( ! mkdir( $site_dir, 0777 ) ) {
		return false;
	}

	if ( file_exists( ABSPATH . 'wp-layout.css' ) ) {
		if ( ! make_site_theme_from_oldschool( $theme_name, $template ) ) {
			// TODO: rm -rf the site theme directory.
			return false;
		}
	} else {
		if ( ! make_site_theme_from_default( $theme_name, $template ) ) {
			// TODO: rm -rf the site theme directory.
			return false;
		}
	}

	// Make the new site theme active.
	$current_template = __get_option( 'template' );
	if ( WP_DEFAULT_THEME === $current_template ) {
		update_option( 'template', $template );
		update_option( 'stylesheet', $template );
	}
	return $template;
}

Changelog

Version Description
1.5.0 Introduced.