函数文档

make_site_theme_from_default()

💡 云策文档标注

概述

make_site_theme_from_default() 函数用于基于默认主题创建新的站点主题,通过复制默认主题文件并修改主题头部信息来实现。

关键要点

  • 函数接受两个必需参数:$theme_name(主题名称)和 $template(主题目录名)。
  • 从 WP_DEFAULT_THEME 目录复制文件到新主题目录,包括核心 PHP 文件和 style.css。
  • 重写 style.css 文件中的主题头部信息,如 Theme Name、Theme URI、Description、Version 和 Author。
  • 创建 images 目录并复制默认主题中的图像文件。
  • 函数返回 void 或 false,在操作失败时可能返回 false。

代码示例

function make_site_theme_from_default( $theme_name, $template ) {
    $site_dir    = WP_CONTENT_DIR . "/themes/$template";
    $default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;

    $theme_dir = @opendir( $default_dir );
    if ( $theme_dir ) {
        while ( ( $theme_file = readdir( $theme_dir ) ) !== false ) {
            if ( is_dir( "$default_dir/$theme_file" ) ) {
                continue;
            }

            if ( ! copy( "$default_dir/$theme_file", "$site_dir/$theme_file" ) ) {
                return;
            }

            chmod( "$site_dir/$theme_file", 0777 );
        }

        closedir( $theme_dir );
    }

    $stylelines = explode( "n", implode( '', file( "$site_dir/style.css" ) ) );
    if ( $stylelines ) {
        $f = fopen( "$site_dir/style.css", 'w' );

        $headers = array(
            'Theme Name:'  => $theme_name,
            'Theme URI:'   => __get_option( 'url' ),
            'Description:' => 'Your theme.',
            'Version:'     => '1',
            'Author:'      => 'You',
        );

        foreach ( $stylelines as $line ) {
            foreach ( $headers as $header => $value ) {
                if ( str_contains( $line, $header ) ) {
                    $line = $header . ' ' . $value;
                    break;
                }
            }

            fwrite( $f, $line . "n" );
        }

        fclose( $f );
    }

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

    $images_dir = @opendir( "$default_dir/images" );
    if ( $images_dir ) {
        while ( ( $image = readdir( $images_dir ) ) !== false ) {
            if ( is_dir( "$default_dir/images/$image" ) ) {
                continue;
            }

            if ( ! copy( "$default_dir/images/$image", "$site_dir/images/$image" ) ) {
                return;
            }

            chmod( "$site_dir/images/$image", 0777 );
        }

        closedir( $images_dir );
    }
}

注意事项

  • 函数使用 @opendir 抑制错误,建议在实际使用中处理可能的目录打开失败情况。
  • 复制文件时设置权限为 0777,可能需要在安全敏感环境中调整。
  • 函数依赖于 WP_DEFAULT_THEME 常量,确保其已正确定义。
  • 在 WordPress 1.5.0 版本中引入,兼容性需考虑。

📄 原文内容

Creates a site theme from the default theme.

Description

Parameters

$theme_namestringrequired
The name of the theme.
$templatestringrequired
The directory name of the theme.

Return

void|false

Source

function make_site_theme_from_default( $theme_name, $template ) {
	$site_dir    = WP_CONTENT_DIR . "/themes/$template";
	$default_dir = WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME;

	/*
	 * Copy files from the default theme to the site theme.
	 * $files = array( 'index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css' );
	 */

	$theme_dir = @opendir( $default_dir );
	if ( $theme_dir ) {
		while ( ( $theme_file = readdir( $theme_dir ) ) !== false ) {
			if ( is_dir( "$default_dir/$theme_file" ) ) {
				continue;
			}

			if ( ! copy( "$default_dir/$theme_file", "$site_dir/$theme_file" ) ) {
				return;
			}

			chmod( "$site_dir/$theme_file", 0777 );
		}

		closedir( $theme_dir );
	}

	// Rewrite the theme header.
	$stylelines = explode( "n", implode( '', file( "$site_dir/style.css" ) ) );
	if ( $stylelines ) {
		$f = fopen( "$site_dir/style.css", 'w' );

		$headers = array(
			'Theme Name:'  => $theme_name,
			'Theme URI:'   => __get_option( 'url' ),
			'Description:' => 'Your theme.',
			'Version:'     => '1',
			'Author:'      => 'You',
		);

		foreach ( $stylelines as $line ) {
			foreach ( $headers as $header => $value ) {
				if ( str_contains( $line, $header ) ) {
					$line = $header . ' ' . $value;
					break;
				}
			}

			fwrite( $f, $line . "n" );
		}

		fclose( $f );
	}

	// Copy the images.
	umask( 0 );
	if ( ! mkdir( "$site_dir/images", 0777 ) ) {
		return false;
	}

	$images_dir = @opendir( "$default_dir/images" );
	if ( $images_dir ) {
		while ( ( $image = readdir( $images_dir ) ) !== false ) {
			if ( is_dir( "$default_dir/images/$image" ) ) {
				continue;
			}

			if ( ! copy( "$default_dir/images/$image", "$site_dir/images/$image" ) ) {
				return;
			}

			chmod( "$site_dir/images/$image", 0777 );
		}

		closedir( $images_dir );
	}
}

Changelog

Version Description
1.5.0 Introduced.