Function Testing with Action Hooks

Posted on

By Michael Fields

I found myself in a position where I needed to know how early I could use a set of custom conditional functions in my theme. All of the functions are defined during the after_setup_theme action, but were unavailable during init and wp_loaded. I came up with the following solution which will run the function during most public-facing actions and append a report to the html document.

I was able to determine that all of the conditional functions I tested were available at wp and all points after. This allowed me to plan my project better.

Unexpected Debuging Help

As soon as I ran this code I noticed that there were a ton of notices generated during all hooks before wp. This prompted me to re-factor the functions. This was a nice bonus.

The Code

Place in your theme’s functions.php file. You will probable want to change the value of $test.

/**
 * A list of WordPress actions.
 * From: http://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request
 */
$actions = array ( 'muplugins_loaded', 'plugins_loaded', 'sanitize_comment_cookies', 'setup_theme', 'load_textdomain', 'after_setup_theme', 'auth_cookie_malformed', 'set_current_user', 'init', 'widgets_init', 'register_sidebar', 'wp_register_sidebar_widget', 'wp_loaded', 'parse_request', 'send_headers', 'pre_get_posts', 'posts_selection', 'wp', 'template_redirect', 'get_header', 'wp_head', 'wp_enqueue_scripts', 'wp_print_styles', 'wp_print_scripts', 'get_template_part_loop', 'loop_start', 'the_post', 'loop_end', 'get_sidebar', 'dynamic_sidebar', 'get_search_form', 'parse_query', 'wp_meta', 'get_footer', 'twentyten_credits', 'wp_footer', 'wp_print_footer_scripts', 'shutdown' );

/**
 * Register test function as a callback for each action.
 */
foreach( $actions as $action ) {
	add_action( $action, 'mfields_conditional_function_test' );
}
/**
 * Test function.
 */
function mfields_conditional_function_test() {

	/* The name of the function to test. */
	$test = 'artpress_is_piece';

	/* All results will be stored here. */
	static $results = array();

	/* Find the current action. */
	$action = (string) current_filter();

	/* Run the function and store the result. */
	$results[$action] = ( call_user_func( $test ) ) ? 'T' : 'F';

	/* Print the results the last time through. */
	if( 'shutdown' === $action ) {
		print "\n" . '<h2>Test: <code>' . $test . '()</code></h2>';
		print "\n" . '<table>';
		foreach( $results as $a => $r ) {
			print "\n" . '<tr>';
			print "\n" . '<td>' . $r . '</td>';
			print "\n" . '<td>' . $a . '</td>';
			print "\n" . '</tr>';
		}
		print "\n" . '</table>';
	}
}

3 Comments Leave a comment

  1. Rarst October 19, 2010 at 6:53 pm

    Name of current hook is much easier to retrieve with current_filter() function.

  2. Michael Fields October 20, 2010 at 1:59 am

    Really? I had no idea that function existed. Thanks for pointing this out.

  3. Michael Fields October 20, 2010 at 4:36 am

    Sweet! current_filter() works perfectly. I updated the code to use this function.

Share your thoughts

*

Fork me on GitHub