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)
我找到了答案
实际上,wordpress具有文件头概念,可以从wordpress主题css或php文件中读取所有元数据
文件头规范 [事实上,每个文件头可以指定如下:]
注意:由于存在单独的标头,因此最大字数以及每个字的最小和最大字符数均基于默认标头。因为这是所有标头名称的子集,而不是所有标头名称的超集,所以这可能会因实现方式和所使用的插件而异。
以上信息来自http://codex.wordpress.org/File_Header
步骤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)
| 归档时间: |
|
| 查看次数: |
278 次 |
| 最近记录: |