函数文档

wp_is_theme_directory_ignored()

💡 云策文档标注

概述

wp_is_theme_directory_ignored() 函数用于在导出过程中判断主题目录是否应被忽略。它检查给定路径是否匹配预设的忽略目录列表,以优化导出操作。

关键要点

  • 函数接受一个必需的字符串参数 $path,表示主题中的文件路径。
  • 返回布尔值,指示该文件是否位于应忽略的目录中。
  • 预设的忽略目录包括 .DS_Store、.svn、.git、.hg、.bzr、node_modules 和 vendor。
  • 通过 str_starts_with() 检查路径是否以这些目录名开头,以确定是否忽略。

代码示例

function wp_is_theme_directory_ignored( $path ) {
    $directories_to_ignore = array( '.DS_Store', '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' );

    foreach ( $directories_to_ignore as $directory ) {
        if ( str_starts_with( $path, $directory ) ) {
            return true;
        }
    }

    return false;
}

注意事项

  • 此函数在 WordPress 6.0.0 版本中引入。
  • 主要用于 wp_generate_block_templates_export_file() 函数,在创建站点编辑器模板导出 ZIP 文件时过滤无关目录。

📄 原文内容

Determines whether a theme directory should be ignored during export.

Parameters

$pathstringrequired
The path of the file in the theme.

Return

bool Whether this file is in an ignored directory.

Source

function wp_is_theme_directory_ignored( $path ) {
	$directories_to_ignore = array( '.DS_Store', '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' );

	foreach ( $directories_to_ignore as $directory ) {
		if ( str_starts_with( $path, $directory ) ) {
			return true;
		}
	}

	return false;
}

Changelog

Version Description
6.0.0 Introduced.