Filtering PHP output of template files in WordPress

Based on a basic knowledge of PHP and some smart snips around the internet, I’ve put together a debugging routine for WP child theme development. The idea is that it’s often difficult to figure out what goes into the soup that makes up a rendered WP page.

So I added this function by Rarst:

Read More
/**
 * 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>";
    }
}

Useful, yes, definitely. I wrapped it in a function and called it in the footer, just before the WP footer (in order to avoid hundreds of plugin file references). Put all that in a CSS sandwich then wrap the display…

<?php if (current_user_can('manage_options')) { ?>
    <div class="debug">
        <?php thelist(); ?>
    </div>
<?php } ?>

And now we’re getting somewhere. Unfortunately that somewhere is me going “dude, the files I need are always on the end about 3 miles down the page” – but filtering the function output is something I would get intensely frustrated with. So I come here both to learn how it’s done, and why, and take that bit of wisdom forward.

A bit of sample output from a random client site:

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

So, basically, I need the last 5 of 235 lines of output. More specifically, I need to filter out anything with /wp-content/themes/ in the path and display only that.

The only problem is that I’m still learning about syntax and class files. I haven’t really gotten into Regex and filtering yet… but I’d sure love for this list to be a little more functional. Eventually, I’d like to stick filtered paths in a config file but I’m not going to burden SO with figuring that out for me (that’s why I’m learning PHP to go with jQuery and CSS).

I do, however, need your help with a basic theme filter!

UPDATE: HamZa DzCyberDeV nailed it

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

Related posts

Leave a Reply

1 comment

  1. So as stated in the comments you could do the following:

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

    For learning purposes using regex:

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

    i modifier: case insensitive.

    Online demo.

    Learn regex and the hard way.