php - 在 WordPress 中过滤模板文件的 PHP 输出

标签 php regex wordpress filter wordpress-theming

基于 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 DzCyber​​DeV 成功了

if(!strpos($path, '/wp-content/themes/') === false) {
    echo $key." = ". $path."</br>";
}

最佳答案

因此,正如评论中所述,您可以执行以下操作:

if(!strpos($path, '/wp-content/themes/') === false) {
    echo $key." = ". $path."</br>";
}

出于学习目的,使用正则表达式:

if(!preg_match('#/wp-content/themes/#i', $path) === false) {
    echo $key." = ". $path."</br>";
}

i 修饰符:不区分大小写。

<强> Online demo

<强> Learn regex hard way

关于php - 在 WordPress 中过滤模板文件的 PHP 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16723569/

相关文章:

php - 计算 MySQL 中的行数(高分)

php - 如何使用 phpstorm 和 Xdebug 设置远程调试

php - 如何使用 preg_replace() 将 hilight_string() 应用于类似 BBCode 标签之间的内容?

python - 使用 grep 或 sed 合并两行

python - 如何删除数据帧列中字符后的字符串的其余部分?

css - 媒体查询在 Iphone 上不起作用,但在浏览器调整大小时正常

php - Laravel 5.3 Homestead php7 重新加载 php.ini 文件

php - 我提供的图片不正确吗,它们都显示为旋转了 90 度

wordpress - 元描述

php - 如何隐藏空元素?