0

I would like to display a page and all the hooks, classes, or php custom function inline with page content without var_dump.

The purpose of this would be to audit a seven year old site that has 18k pages and many developers coding and writing functions over the years (without documentation) to discern which functions are being used on which pages and if there are functions that are not being used on the site at all, deprecate them.

Thank you!

5
  • @jacobpeattie, reworded question to conform to rules. please remove on hold. May 6, 2019 at 15:15
  • @jackjohansson, reworded question to conform to rules. please remove on hold. May 6, 2019 at 15:16
  • Are you looking to enumerate all of the hooks, classes and PHP functions used, or just the ones that are used on a particular page? If it's on a page, are you asking about the post type page or on a particular... template, core page (like archive or post), admin page, or... otherwise? May 6, 2019 at 16:42
  • @StephanSamuel, Yes I am looking to enumerate all of the hooks, classes and php functions that used on each front-end page (post, page, cpt, etc), excluding admin. The functions should display inline with the content, not just a list. Thank you May 6, 2019 at 17:45
  • There are options in PHP, see this Stack Overflow thread: »Print PHP Call Stack«. May 6, 2019 at 17:54

1 Answer 1

1

You're asking for more than one thing:

  • Enumerate hooks: this can be found in the $wp_filter global variable. If you're looking for just the names of the hooks, use array_keys(). For some code examples, look here or there are plenty of other examples.
  • Enumerate classes: use the PHP function get_declared_classes().
  • Enumerate functions: similarly, use the PHP function get_defined_functions(). This should give you an associative array-of-arrays and it sounds like you probably just want the ones under the user key.
  • Filter these lists: this is the tricker part. Probably you'd need to build a reference list of things you want to filter out, like all the WP custom functions (a long list). All of the above-referenced sources give you arrays, so array_filter() is probably your best friend here.
  • Display your content: probably you will end up with one or more arrays containing the content that you want to display. Build your foreach loops to dump the contents on the screen where and how you'd like. I'm not exactly sure what, "inline with page content," means but a few quick runs should help you sort things out and make it look as you prefer. Keep in mind that if you have an array of strings to dump to the screen in a delimited list, implode(', ', $array_of_strings) gives good results.

If you're looking for what gets called on a particular page as opposed to what's been defined, the classes and functions are hard without breaking into the PHP parser (or re-parsing the PHP file that was called). Hooks are relatively easy in this case, just use the all filter. This will give you what's being called, from end to end, in the rendering of the page.

If you're looking for what's defined in a PHP file, that's just an exercise in parsing or hooking into the PHP parser. Your bigger problem here is that a particular page load in WP has a very loose correlation to the number of PHP files that are loaded. There is no one-to-one correlation between the URL visited and a single PHP file loaded. For example, each of the support classes, like WP_Post and wpdb, are defined in a single PHP file, none of which may be responsible directly for any output to any page load, but all of which are essential to just about every page load.

Without more information on exactly what you're trying to accomplish (specifically, why), it's hard to say more. So... what are you trying to accomplish? Is this enough background information to help you accomplish your task?

4
  • 1
    I think I will start here and then update this thread as I go along. The purpose of this would be to audit a seven year old site that has 18k pages and many developers coding and writing functions over the years (without documentation) to discern which functions are being used on which pages and if there are functions that are not being used on the site at all, deprecate them. May 7, 2019 at 1:14
  • You may want to look into some static code analysis. At least part of this (deprecating unused functions) is a solved problem. Not sure what dev environment you're using but I'm sure that I saw something in Eclipse that does this. May 7, 2019 at 20:12
  • Thanks Stephan. I am using PHPStorm by Jetbrains. I will look to see if there something similar to this. May 8, 2019 at 22:08
  • PHPStorm claims code analysis in its feature set. This may work for you. Keep in mind that in order for it to detect function use problems, you may need to import WP core into your project. Or you can create a separate analysis project that includes WP, from the root, and your plugins/themes in wp-content. May 9, 2019 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.