基于PHP的基本知识和互联网上的一些智能片段,我整理了一个用于WP儿童主题开发的调试程序。这个想法是,通常很难弄清楚构成呈现的 WP 页面的汤中有什么。
所以我通过 Rarst 添加了这个功能:
/**
* Function to list all templates used in a page
* @author Rarst
* @uri http://wordpress.stackexchange.com/a/89005
*/
function thelist() {
$included_files = get_included_files();
$stylesheet_dir = str_replace( '\\', '/', get_stylesheet_directory() );
$template_dir = str_replace( '\\', '/', get_template_directory() );
foreach ( $included_files as $key => $path ) {
$path = str_replace( '\\', '/', $path );
if ( false === strpos( $path, $stylesheet_dir ) && false === strpos( $path, $template_dir ) )
unset( $included_files[$key] );
echo $key." = ". $path."</br>";
}
}
有用,是的,绝对有用。我将它包装在一个函数中并在页脚中调用它,就在 WP 页脚之前(为了避免数百个插件文件引用)。将所有内容放入 CSS 三明治中,然后将显示屏包裹起来……
<?php if (current_user_can('manage_options')) { ?>
<div class="debug">
<?php thelist(); ?>
</div>
<?php } ?>
现在我们正在取得进展。不幸的是,我要去某个地方“老兄,我需要的文件总是在页面下方约 3 英里处”——但过滤函数输出是我会非常沮丧的事情。所以我来这里是为了了解它是如何完成的,以及为什么,并把那一点智慧向前推进。
来自随机客户端站点的一些示例输出:
0 = /path/to/file/index.php
1 = /path/to/file/wp-blog-header.php
2 = /path/to/file/wp-load.php
3 = /path/to/file/wp-config.php
4 = /path/to/file/wp-settings.php
5 = /path/to/file/wp-includes/load.php
6 = /path/to/file/wp-includes/default-constants.php
7 = /path/to/file/wp-includes/version.php
8 = /path/to/file/wp-includes/compat.php
9 = /path/to/file/wp-includes/functions.php
10 = /path/to/file/wp-includes/option.php
[...]
219 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/page_link.php
220 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/post_object.php
221 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/relationship.php
222 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/taxonomy.php
223 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/user.php
224 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/date_picker/date_picker.php
225 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/color_picker.php
226 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/message.php
227 = /path/to/file/wp-content/plugins/advanced-custom-fields/core/fields/tab.php
228 = /path/to/file/wp-content/plugins/add-to-any/add-to-any-wp-widget.php
229 = /path/to/file/wp-includes/class-wp-admin-bar.php
230 = /path/to/file/wp-includes/template-loader.php
231 = /path/to/file/wp-content/themes/themename/category.php
232 = /path/to/file/wp-content/themes/themename/header.php
233 = /path/to/file/wp-content/themes/themename/content.php
234 = /path/to/file/wp-content/themes/themename/sidebar.php
235 = /path/to/file/wp-content/themes/themename/footer.php
所以,基本上,我需要 235 行输出中的最后 5 行。更具体地说,我需要过滤掉路径中带有 /wp-content/themes/ 的任何内容并仅显示该内容。
唯一的问题是我仍在学习语法和类文件。我还没有真正进入正则表达式和过滤......但我肯定喜欢这个列表更实用一些。最终,我想将过滤后的路径粘贴在配置文件中,但我不会为我解决这个问题(这就是我学习 PHP 以配合 jQuery 和 CSS 的原因)。
但是,我确实需要您对基本主题过滤器的帮助!
更新:HamZa DzCyberDeV 做到了
if(!strpos($path, '/wp-content/themes/') === false) {
echo $key." = ". $path."</br>";
}