Skip to content

Instantly share code, notes, and snippets.

@byjml
Last active August 9, 2021 21:30
Show Gist options
  • Save byjml/3683367 to your computer and use it in GitHub Desktop.
Save byjml/3683367 to your computer and use it in GitHub Desktop.
WordPress Hook Inspector: Inspects which functions are attached into what hook.
<?php
/**
* Plugin Name: WordPress Hook Inspector
* Description: Provides a shortcode that displays information table about which function is hooked into what hook. The information displayed is only for debugging and the shortcode will not display anything if the post included is not password protected. <strong>Usage:</strong> add [inspect hook="hook_name"] for single hooks or [inspect] for all hooks. The shortcode requires the post to be password protected.
* Author: JML
* Author URI: http://jml.so
* Version: 1.0
* License: GNU General Public License v2.0
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/*
CREDITS:
Rarst <http://rarst.net/>
WPRecipes <http://wprecipes.com/>
USAGE:
To dislay information about one hook use: [inspect hook="hook_name"] (e.g. [inspect hook="init"])
To display information about all hooks, use: [inspect]
Note: The post/page should be password protected or this will not work, this is done, so nobody
but you will see the hooks in action.
*/
/* Create the [inspect] shortcode. */
add_shortcode( 'inspect', 'hook_inspector_shortcode' );
/**
* hook_inspector_shortcode()
*
* Creates a shortcode that displays information table about which function is attached to what hook.
*
*
* @uses shortcode_atts() to extract shortcode attributes.
* @param array $atts Shortcode attributes
* @return string HTML output.
*/
function hook_inspector_shortcode( $atts ) {
extract( shortcode_atts( array( 'hook' => false ), $atts ) );
return hook_inspector( $hook );
}
/**
* hook_inspector()
*
* Finds out which function hooks into what.
*
* @credit WP Recipes
* @see http://www.wprecipes.com/list-all-hooked-wordpress-functions/
*
* @global $wp_filter Global WordPress filter object.
*
* @param string|bool $hook The hook we want to get its information.
*/
function hook_inspector( $hook = false ) {
global $wp_filter, $wpdb, $post;
$post_password = $wpdb->get_var( $wpdb->prepare( "SELECT post_password FROM $wpdb->posts WHERE ID = %s LIMIT 1", $post->ID ) );
if ( empty( $post_password ) )
return "<p>Please make sure that this post is password protected.</p>";
if ( $hook ) {
$hooks[$hook] = $wp_filter[$hook];
if ( ! is_array( $hooks[$hook] ) ) {
return "<p><strong>Error:</strong> nothing found for <code>$hook</code> hook.</p>";
}
}
else {
$hooks = $wp_filter;
ksort( $hooks );
}
echo "<table style='width: 100%'>\n";
/* Start table head. */
echo "\t\t\t<thead>\n";
echo "\t\t\t\t<tr>\n";
echo "\t\t\t\t\t<th scope='col'>Hook</th>\n";
echo "\t\t\t\t\t<th scope='col'>Priority &amp; Function list</th>\n";
echo "\t\t\t\t</tr>\n";
echo "\t\t\t</thead>\n";
/* Start table body. */
echo "\t\t\t<tbody>\n";
foreach( $hooks as $hook => $priorities ) {
echo "\t\t\t\t<tr>\n";
echo "\t\t\t\t\t<td style='vertical-align: middle'><code>$hook</code></td>\n";
echo "\t\t\t\t\t<td>\n";
echo "\t\t\t\t\t\t<dl style='margin: 0;'>\n";
ksort( $priorities );
foreach( $priorities as $priority => $functions ) {
echo "\t\t\t\t\t\t\t<dt style='background-color: #eee; padding: 0 10px;'><code>$priority</code></dt>\n";
foreach( $functions as $name => $properties )
echo "\t\t\t\t\t\t\t<dd style='margin: 0;'><code>$name</code></dd>\n";
}
echo "\t\t\t\t\t\t</dl>\n";
echo "\t\t\t\t\t</td>\n";
echo "\t\t\t\t</tr>\n";
}
echo "\t\t\t</tbody>\n";
echo '</table>';
}
@MikeLoucas
Copy link

Noob Question, How do I use this pleaseNthanks? I read the USAGE... I dont get it.
Do I put the short code in a page, in a html block?
Im trying to see the Hooks of the ADD USER page, so Im not sure what to do, the plug in sounds intriguing.

:-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment