Site icon WP Smith

Your Own WordPress Code Sandbox

When I was learning WordPress, the one file that I created that was the most helpful was my test.php file. This file proved extremely valuable. Even now, everyone once in a while, I want to write a vanilla function for a particular filter or action and see what the parameters contain. It was a way that I could load the WordPress environment (and optionally the admin environment) and test various default functions, filters, and actions. However, I don't want to navigate to a specific page or anything. I want something quick, easy and fast. So, I have this file that I keep in my development environment called test.php.

NEVER use this file in a production environment.

This file contains the following:

  1. Loads the WordPress Environment
  2. Contains debugging functions wps_printr() and wps_die()
  3. Optionally Loads the Admin Environment
  4. Optionally loads user functions
  5. Optionally loads the Plugins API

To use this file, create a file called test.php at the root of your local site (this is the same place where your wp-config.php [unless you have moved this file] and wp-load.php files are located. The file should be located and reachable by http://domain.dev/test.php.

Debugging Functions

The debugging functions allow you to pretty print variable values to the screen.

//** Debugging **//
if ( ! function_exists( 'wps_printr' ) ) {
/**
* Pretty prints variable (array) value.
* @author Travis Smith <t@wpsmith.net>
*
* @param $args Variable to be outputted.
* @param string $name Title to output before the variable output.
*/
function wps_printr( $args, $name = '' ) {
if ( apply_filters( 'wps_debug_off', false ) ) {
return;
}
if ( '' !== $name && false !== $name ) {
printf( '<strong>%s</strong><br/>', $name );
}
if ( is_array( $args ) ) {
printf( '<pre>%s</pre>', htmlspecialchars( print_r( $args, true ) ) );
} else {
var_dump( $args );
}
}
}
if ( function_exists( 'wps_printr' ) && ! function_exists( 'wps_die' ) ) {
/**
* Pretty prints variable (array) value & dies.
* @author Travis Smith <t@wpsmith.net>
*
* @param $args Variable to be outputted.
* @param string $name Title to output before the variable output.
*/
function wps_die( $args, $name = '' ) {
if ( apply_filters( 'wps_debug_off', false ) ) {
return;
}
wp_die( wps_printr( $args, $name ) );
}
}

Optionally Load the Admin Environment

All you need to do is uncomment the line with wps_load_admin().

//** Admin **//
/**
* Pluggable function, prevent redirect to admin pages
* @author Travis Smith <t@wpsmith.net>
*/
function auth_redirect() {
$loginusername = 'admin';
$user = get_user_by( 'login', $loginusername );
wp_set_current_user( $user->ID, $loginusername );
wp_set_auth_cookie( $user->ID );
}
/**
* Load the WordPress Admin Environment
* @author Travis Smith <t@wpsmith.net>
*/
function wps_load_admin() {
// Select either Network or User Admin
// define( 'WP_NETWORK_ADMIN', TRUE ); // Used in is_network_admin()
define( 'WP_USER_ADMIN', TRUE ); // Used in is_user_admin()
define( 'WP_BLOG_ADMIN', TRUE ); // Used in is_blog_admin()
define( 'WP_ADMIN', TRUE ); // Used in is_admin()
// Required for admin.php & prevent errors
global $wp_db_version, $_wp_submenu_nopriv;
require( './wp-admin/admin.php' );
echo '<h1>Admin Loaded</h1>';
}
// Load Admin
//wps_load_admin();

Optionally Load the User Functions

// User functions
//require( './wp-admin/includes/user.php' );

All you need to do is uncomment the line with require( './wp-admin/includes/user.php' );.

Optionally Load the Plugins API

// WordPress Plugins API
//require( './wp-admin/includes/plugin_install.php' );
// Endpoints
//https://api.wordpress.org/plugins/info/1.0/{slug}
//https://api.wordpress.org/plugins/info/1.0/{slug}.json
//https://api.wordpress.org/plugins/update-check/1.0/
//https://api.wordpress.org/plugins/update-check/1.1/
// Importers
//https://api.wordpress.org/core/importers/1.0/ (serialized)
//https://api.wordpress.org/core/importers/1.1/ (JSON)
//Checksum
//https://api.wordpress.org/core/checksums/1.0/?version={wp-version}&locale=en_US (JSON)

All you need to do is uncomment the line with require( './wp-admin/includes/plugin_install.php' );.

Full Code

<?php
// Set date, just in case
date_default_timezone_set( 'America/New_York' );
// Load the WordPress Environment
define( 'WP_DEBUG', TRUE ); // Set to FALSE to remove DEBUG MODE
require( './wp-load.php' );
// User functions
//require( './wp-admin/includes/user.php' );
//** Admin **//
/**
* Pluggable function, prevent redirect to admin pages
* @author Travis Smith <t@wpsmith.net>
*/
function auth_redirect() {
$loginusername = 'admin';
$user = get_user_by( 'login', $loginusername );
wp_set_current_user( $user->ID, $loginusername );
wp_set_auth_cookie( $user->ID );
}
/**
* Load the WordPress Admin Environment
* @author Travis Smith <t@wpsmith.net>
*/
function wps_load_admin() {
// Select either Network or User Admin
// define( 'WP_NETWORK_ADMIN', TRUE ); // Used in is_network_admin()
define( 'WP_USER_ADMIN', TRUE ); // Used in is_user_admin()
define( 'WP_BLOG_ADMIN', TRUE ); // Used in is_blog_admin()
define( 'WP_ADMIN', TRUE ); // Used in is_admin()
// Required for admin.php & prevent errors
global $wp_db_version, $_wp_submenu_nopriv;
require( './wp-admin/admin.php' );
echo '<h1>Admin Loaded</h1>';
}
// Load Admin
//wps_load_admin();
// WordPress Plugins API
//require( './wp-admin/includes/plugin_install.php' );
// Endpoints
//https://api.wordpress.org/plugins/info/1.0/{slug}
//https://api.wordpress.org/plugins/info/1.0/{slug}.json
//https://api.wordpress.org/plugins/update-check/1.0/
//https://api.wordpress.org/plugins/update-check/1.1/
// Importers
//https://api.wordpress.org/core/importers/1.0/ (serialized)
//https://api.wordpress.org/core/importers/1.1/ (JSON)
//Checksum
//https://api.wordpress.org/core/checksums/1.0/?version={wp-version}&locale=en_US (JSON)
//** Debugging **//
if ( ! function_exists( 'wps_printr' ) ) {
/**
* Pretty prints variable (array) value.
* @author Travis Smith <t@wpsmith.net>
*
* @param $args Variable to be outputted.
* @param string $name Title to output before the variable output.
*/
function wps_printr( $args, $name = '' ) {
if ( apply_filters( 'wps_debug_off', false ) ) {
return;
}
if ( '' !== $name && false !== $name ) {
printf( '<strong>%s</strong><br/>', $name );
}
if ( is_array( $args ) ) {
printf( '<pre>%s</pre>', htmlspecialchars( print_r( $args, true ) ) );
} else {
var_dump( $args );
}
}
}
if ( function_exists( 'wps_printr' ) && ! function_exists( 'wps_die' ) ) {
/**
* Pretty prints variable (array) value & dies.
* @author Travis Smith <t@wpsmith.net>
*
* @param $args Variable to be outputted.
* @param string $name Title to output before the variable output.
*/
function wps_die( $args, $name = '' ) {
if ( apply_filters( 'wps_debug_off', false ) ) {
return;
}
wp_die( wps_printr( $args, $name ) );
}
}
/**
* Plugins API Remote Post Example
*
* @author Travis Smith <t@wpsmith.net>
*/
function wps_plugins_api_remote_post() {
$args = (object) array( 'slug' => 'custom-favicon' );
$request = array( 'action' => 'plugin_information', 'timeout' => 15, 'request' => serialize( $args ) );
$url = 'http://api.wordpress.org/plugins/info/1.0/';
$response = wp_remote_post( $url, array( 'body' => $request ) );
$plugin_info = unserialize( $response['body'] );
wps_printr( $plugin_info, 'plugin_info' );
}
/**
* Helper function to output plugins_api()
*
* @author Travis Smith <t@wpsmith.net>
* @param $result array Array of plugins_api() result.
*/
function wps_output_result( $result ) {
/** Check for Errors & Display the results */
if ( is_wp_error( $result ) ) {
wps_printr( $result->get_error_message() );
} else {
wps_printr( $result );
}
}
/**
* Plugins API using plugins_api(): Get specific plugin information
*
* @author Travis Smith <t@wpsmith.net>
*/
function wps_plugins_api_plugin_information() {
$call_api = plugins_api( 'plugin_information', array( 'slug' => 'custom-favicon' ) );
wps_output_result( $call_api );
}
/**
* Plugins API using plugins_api(): Get plugins with tag
*
* @author Travis Smith <t@wpsmith.net>
*/
function wps_plugins_api_hot_tags() {
/** Prepare our query */
$call_api = plugins_api( 'hot_tags',
array(
'number' => '50',
)
);
wps_output_result( $call_api );
}
/**
* Plugins API using plugins_api(): Search plugins
*
* @author Travis Smith <t@wpsmith.net>
*/
function wps_plugins_api_query_plugins() {
$call_api = plugins_api( 'query_plugins',
array(
'browse' => 'top-rated',
'page' => '1',
'per_page' => '5',
'fields' => array(
'downloaded' => false,
'rating' => false,
'description' => false,
'short_description' => false,
'donate_link' => false,
'tags' => false,
'sections' => false,
'homepage' => false,
'added' => false,
'last_updated' => false,
'compatibility' => false,
'tested' => false,
'requires' => false,
'downloadlink' => true,
)
)
);
wps_output_result( $call_api );
}
// Execute plugins_api() functions
//wps_plugins_api_remote_post();
//wps_plugins_api_hot_tags();
//wps_plugins_api_query_plugins();
view raw test.php hosted with ❤ by GitHub