Quick code test in WordPress

A quick snippet to run custom code in WordPress.

The problem

Not any project I work on is a TDD-ed one and hence a dry-running framework is not always in place.
Sometimes I need to run a quick snippet of code just to know what’s happening under the hood or if a certain idea is worth any further investigation.
Setting up a whole plugin structure or test suite on mere code leads might be a waste of time.

Code runner plugin

A recent epiphany for me has been the use of a basic must use plugin to wrap and run the interested code.
Simply putting this code in the /wp-content/mu-plugins (or wherever the plugins folder is located) will allow for quick Xdebug sessions with very little overhead time investment.

<?php
/**
 * Code Runner
 */

add_action( 'wp_loaded', 'run_code', 100, 10 );
function run_code(){
    $args = func_get_args();
    extract($args);

    // run code here
}

WordPress will not complain about the hook or filter not passing 10 arguments at all but that leaves space for long signature calls and argument investigation.

Live Template and Snippet

To cope with my over-engineering of anything here are a Sublime Text snippet

<snippet>
    <content><![CDATA[
/**
 * Code Runner
 */

add_action( '${1:wp_loaded}', 'run_code', 100, 10 );
function run_code(){
    $args = func_get_args();
    extract($args);

    ${2:// run code here}
}
]]></content>
    <tabTrigger>coderunner</tabTrigger>
    <scope>source.php</scope>
</snippet>

and a PhpStorm live template

/**
 * Code Runner
 */

add_action( '$hook$', 'run_code', 100, 10 );
function run_code(){
    $args = func_get_args();
    extract($args);

    $code$
}

to output the code in one swoop.