无法扩展所使用的默认标头WP_Theme
:
private static $file_headers = array(
\'Name\' => \'Theme Name\',
\'ThemeURI\' => \'Theme URI\',
\'Description\' => \'Description\',
\'Author\' => \'Author\',
\'AuthorURI\' => \'Author URI\',
\'Version\' => \'Version\',
\'Template\' => \'Template\',
\'Status\' => \'Status\',
\'Tags\' => \'Tags\',
\'TextDomain\' => \'Text Domain\',
\'DomainPath\' => \'Domain Path\',
);
Here\'s another post 有人试图做同样的事情。
我一直在挖,发现this post over-on-Stack溢出提供了一种解决方法,可以硬编码和读取自定义文件头。
我意识到您希望能够动态地读写这些标题,并根据信息显示消息,这个答案并没有涵盖所有这些,但这是一个良好的开端。
假设主题中有此标题:
/*
Theme Name: Dave\'s Good Stuff 80\'s Theme
Theme URI: https://www.youtube.com/playlist?list=PL3k1BzkuZjd7F0EE6mp5LPQWxigJ-8_td
Author: Dave Romsey
Author URI: http://daveromsey.com
Description: A cool theme.
Version: 0.1.0
Tags:
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: goto10
Key: 867-5309
*/
此代码将能够提取自定义文件头密钥:
/**
* Retrieve metadata from a file.
*
* Searches for metadata in the first 8kiB of a file, such as a plugin or theme.
* Each piece of metadata must be on its own line. Fields can not span multiple
* lines, the value will get cut at the end of the first line.
*
* If the file data is not within that first 8kiB, then the author should correct
* their plugin file and move the data headers to the top.
*
* @param string $file Path to the file
* @param array $default_headers List of headers, in the format array(\'HeaderKey\' => \'Header Name\')
*/
function wpse238644_get_file_data( $file, $default_headers) {
$fp = fopen( $file, \'r\' );
$file_data = fread( $fp, 8192 );
fclose( $fp );
$file_data = str_replace( "\\r", "\\n", $file_data );
$all_headers = $default_headers;
foreach ( $all_headers as $field => $regex ) {
if (preg_match( \'/^[ \\t\\/*#@]*\' . preg_quote( $regex, \'/\' ) . \':(.*)$/mi\', $file_data, $match )
&& $match[1])
$all_headers[ $field ] = trim(preg_replace("/\\s*(?:\\*\\/|\\?>).*/", \'\', $match[1]));
else
$all_headers[ $field ] = \'\';
}
return $all_headers;
}
function wpse238644_get_theme_data() {
// Customize these to fit your needs
$file_headers = array(
\'Name\' => \'Theme Name\',
\'ThemeURI\' => \'Theme URI\',
\'Description\' => \'Description\',
\'Author\' => \'Author\',
\'AuthorURI\' => \'Author URI\',
\'Version\' => \'Version\',
\'Template\' => \'Template\',
\'Status\' => \'Status\',
\'Tags\' => \'Tags\',
\'TextDomain\' => \'Text Domain\',
\'DomainPath\' => \'Domain Path\',
\'Key\' => \'Key\',
);
$theme_headers = wpse238644_get_file_data( get_template_directory() . \'/style.css\', $file_headers );
$theme_key = isset( $theme_headers[\'Key\'] ) ? $theme_headers[\'Key\'] : false;
// Do something with the key
//var_dump( $theme_key );
}
add_action( \'init\', \'wpse238644_get_theme_data\' );