wpGrafie

<?php /* Präsentiert auch coole Codeschnipsel */ ?>

Feed abonnieren

WordPress Filter debuggen

WordPress Filter debuggen

WordPress hat viele Filter, wo sich Entwickler einklinken können. Durch einen Filter ist es möglich, bestimmte Daten vor der Ausgabe zu filtern.

Manchmal ist es dann interessant zu wissen, welche Funktion welchen Ausgabe filtert.

Dafür habe ich mir folgende Funktion geschrieben, welche jeder gerne benutzen und weiter entwickeln darf.

<?php
/**
* Debug WordPress filters.
*
* Use add_action( 'shutdown', 'ds_debug_filters' ) to display all used
* filters with the functions hooked into the filter.
*
* @author Dominik Schilling
* @license GPLv2
* @link http://wpgrafie.de/262/
*
* @version 0.1.1
*
* Changelog:
* Version 0.1.1 - Fixed string for more then 1 accepted arguments.
*
*/
function ds_debug_filters( $custom_tags = array() ) {
// $wp_filter Stores all of the filters
global $wp_filter;

if ( empty( $wp_filter ) )
return false;

// Check if custom tags are defined
if ( ! empty( $custom_tags ) ) {
// Check if custom tags are available
$tags = array_intersect( array_keys( $wp_filter), (array) $custom_tags );

if ( empty( $tags ) )
return false;

// Fill custom tags
foreach ( $tags as $tag )
$_wp_filter[$tag] = $wp_filter[$tag];
} else {
// Use default tags
$_wp_filter = $wp_filter;
}

echo '<pre id="wp-debug-filters">';

// Uncomment, if you want to sort by name of the filter hooks
// ksort( $_wp_filter );

foreach ( $_wp_filter as $tag => $data ) {
// Print tag name
printf(
'<br /><strong>%s</strong><br />',
esc_html( $tag )
);

// Sort by priority
ksort( $data );

foreach ( $data as $priority => $functions ) {
// Print priority once
printf(
'%s',
$priority
);

// Go through each function
foreach ( $functions as $function ) {
$_function = $function['function'];
$_args = $function['accepted_args'];

// Check function type
if ( is_array( $_function ) ) {
// Object class calling
if ( is_object( $_function[0] ) )
$class = get_class( $_function[0] );
else
$class = $_function[0];

$name = $class . '::' . $_function[1];
} else {
// Static calling
$name = $_function;
}

// Print function name and number of accepted arguments
printf(
"t%s() (%s)<br />",
esc_html( $name ),
sprintf(
_n(
'1 accepted argument',
'%s accepted arguments',
$_args
),
$_args
)
);
}
}
}

echo '</pre>';
}

WordPress Filter Debuggen

Mögliche Ausgabe der Funktion

Ein Dank geht an Andrey Savchenko für die Idee.

3 Kommentare zu WordPress Filter debuggen

  1. Dominik (Autor)

    @Ralf und Rene,
    danke für eure Kommentare. Das von Frank bzw Mike (wer hats nun entwickelt?) kannte ich.

    Allerdings gibt es einen Unterschied. Meine Version listet nur die Filter auf, auf welche auch zugegriffen wird. Die Version von Frank/Mike listet alle Filter auf, die zur Verfügung stehen.

    Rene, deine Version ist aber böse.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit einem * markiert.
Bitte bleibe beim Thema, für Supportanfragen stehe ich gerne per Mail zur Verfügung.

*

*