阅读像Wordpress主题引擎这样的文件中的注释详细信息

Var*_*ran 2 wordpress

WordPress如何从文件中读取评论的详细信息?

我试图通过读取WordPress核心文件来进行查找。我也尝试file_get_contents在php中使用函数和文件读取功能,但无法获得所需的确切值。

例如,我在css文件中添加了以下注释,并在WP-admin中打印出来,这怎么可能?

/*
Theme Name: My Theme 
Author: Team
Author URI: http://indiainternetready.com/
*/
Run Code Online (Sandbox Code Playgroud)

Var*_*ran 5

我找到了答案

实际上,wordpress具有文件头概念,可以从wordpress主题css或php文件中读取所有元数据

文件头规范 [事实上,每个文件头可以指定如下:]

  1. 标头写在PHP或CSS文件开头的块中。
  2. 可以在文件注释中放置一个块,例如PHP或CSS注释。
  3. 整个标头块必须放置在文件的前8 192个字节内。
  4. 标头互相跟随,一行一行。
  5. 标头由名称和值组成。
  6. 名称和值用':'字符分隔。
  7. 该名称最少包含一个,最多三个单词。
  8. 一个单词的最小长度为3,最大长度为12个字符。
  9. 一个单词由字符az和AZ组成。
  10. 单词之间用一个空格隔开(d32 / x20)
  11. 名称在行首或空格字符后开始。
  12. 名称以“:”字符结尾。值从“:”字符后开始。
  13. 有时':'字符后加空格。认为该空间不是值的一部分。
  14. 标头值可以包含任何字符,但不能包含换行符。
  15. 标头值可能在使用前被过滤。
  16. 标头值可以但不能包含某些XHTML元素或HTML标记形式的HTML代码。

注意:由于存在单独的标头,因此最大字数以及每个字的最小和最大字符数均基于默认标头。因为这是所有标头名称的子集,而不是所有标头名称的超集,所以这可能会因实现方式和所使用的插件而异。

以上信息来自http://codex.wordpress.org/File_Header


我刚刚提取了确切的代码,它将获取在wordpress中使用的文件元数据

步骤1:列出您要获取的所有元数据

$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'
);
Run Code Online (Sandbox Code Playgroud)

步骤2: 将此功能添加到文件中

/**
 * 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 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;
}
Run Code Online (Sandbox Code Playgroud)

步骤3:在文件中添加以下注释

/*
Theme Name: Stack Grace
Theme URI: http://stackoverflow.com
Author: Stack Over Flow
Author URI: http://stackoverflow.com
Description: Sample of Stack Overflow.
Version: 2.1
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: black, brown, orange, tan, white, yellow, light, one-column, two-columns, right-sidebar, fluid-layout, responsive-layout, custom-header, custom-menu, editor-style, featured-images, microformats, post-formats, rtl-language-support, sticky-post, translation-ready
Text Domain: This theme, like SS, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/
Run Code Online (Sandbox Code Playgroud)

步骤4:按如下所示调用函数[用test.php文件替换]

$data = get_file_data( 'test.php', $file_headers);
foreach($data as $key => $val){
    echo $key." --> ".$val."<br/>";
}
Run Code Online (Sandbox Code Playgroud)

最终:输出将如下所示

Name --> Stack Grace
ThemeURI --> http://stackoverflow.com
Description --> Sample of Stack Overflow.
Author --> Stack Over Flow
AuthorURI --> http://stackoverflow.com
Version --> 2.1
Template -->
Status -->
Tags --> black, brown, orange, tan, white, yellow, light, one-column, two-columns, right-sidebar, fluid-layout, responsive-layout, custom-header, custom-menu, editor-style, featured-images, microformats, post-formats, rtl-language-support, sticky-post, translation-ready
TextDomain --> This theme, like SS, is licensed under the GPL.
DomainPath --> 
Run Code Online (Sandbox Code Playgroud)