Debugging WordPress HTTP API Remote Requests

If you’re writing WordPress code that makes remote requests and need to easily debug the requests, here’s some helper code to do it. This requires that you’re using WordPress’s HTTP API which you should already be doing — directly using cURL or other methods is wrong and a great way to make your code not cross-server compatible.

add_action( 'http_api_debug', 'viper_http_api_debug', 10, 5 );

function viper_http_api_debug( $response, $type, $class, $args, $url ) {
	// You can change this from error_log() to var_dump() but it can break AJAX requests
	error_log( 'Request URL: ' . var_export( $url, true ) );
	error_log( 'Request Args: ' . var_export( $args, true ) );
	error_log( 'Request Response : ' . var_export( $response, true ) );
}

That will log the request URL, the request arguments, and the whole response HTTP_API object to your error log file. The last message one will be pretty long/spammy but it’ll give you headers, the body, and everything else. Since it’s an instance of HTTP_API, you can also do things like this if you want to:

error_log( 'Response Code: ' . wp_remote_retrieve_response_code( $response ) );

Installing PHPUnit On Windows

READ THIS: This guide no longer works due to PHPUnit dropping support for the PEAR installation method. If you’re at this page in order to use PHPUnit for WordPress-related things, then these days I recommend setting up an instance of Varying Vagrant Vagrants which is a pre-built virtual machine configuration for developing using WordPress and includes PHPUnit pre-configured. Otherwise check out the official PHPUnit installation documentation on how to install PHPUnit globally.

I wanted to start contributing to the WordPress unit tests so I needed to install PHPUnit. Turned out it was harder than it might seem (I had a tough time getting it all working) so I thought I’d blog what finally ended up working for me to help save some people some time.

Assuming you already have PHP and MySQL installed, here’s the steps you need to take:

  1. Install PEAR, a dependency for PHPUnit:
    1. Visit http://pear.php.net/go-pear.phar in your browser and save the file into your PHP directory. This is the folder where you can find php.exe.
    2. Open an administrator command prompt. Hit your Windows key, type “cmd”, right-click the resulting “cmd.exe” search result, and select “Run as administrator”. Navigate to the folder where you have PHP installed, the same folder where you saved the file in the previous step.
    3. Type the following command to execute the file you just downloaded: php go-pear.phar
    4. After a moment, you should start being prompted for some things. The installer is pretty self-explanatory and I think you want a system installation rather than a local one.
    5. Open the folder where PHP is installed and double-click the PEAR_ENV.reg file that has been created. This allows you to run the pear command from any folder.
    6. Verify PEAR is working by running the command pear version
  2. Install PHPUnit:
    1. Turn on auto_discover in PEAR by typing the following command at the command line: pear config-set auto_discover 1
    2. Download and install PHPUnit by running the following command: pear install pear.phpunit.de/PHPUnit
    3. In order to be able to run the phpunit command from any folder, you need to add it to your Windows Path value. Right-click My Computer → Properties → Advanced system settings → Environmental Variables → select “Path” under “System Variables” → Edit → Add a semi-colon (;) and then the full path to your PHP folder onto the end of the value, for example like this: ;D:\Webserver\php
    4. Verify PHPUnit is working by running the command phpunit --version
  3. Set up the WordPress unit tests by following the rest of the steps on the WordPress Core Contributor Handbook now that you have PHPUnit installed.

Done!

jQuery Lightbox For Native Galleries Plugin Discontinued

With the release of Jetpack 1.5, Jetpack now supports the awesome carousel feature that you may have seen running on WordPress.com. It’s superior to my jQuery Lightbox For Native Galleries plugin in my opinion so I am opting to discontinue development on my plugin.

My plugin should continue to work for the indefinite future but I will no longer be maintaining it.

WordPress License Plates

I moved my “IM STIG” license plates over to my Dodge Viper so I had to get new plates for my Ford Mustang. What to get on my plates this time around seemed like an obvious choice! 🙂

WordPress License Plates

UPDATE: It looks like I’m the fifth sixth person to get WordPress license plates! Ryan Duff (Pennsylvania), Jonathan Dingman (California), Michael Torbert (North Carolina), AJ Morris (Michigan), and Jesse Friedman (Rhode Island) also have them. Awesome. 😎

JW FLV Player Removed From My “Viper’s Video Quicktags” Plugin

JW FLV Player has been removed from the latest version of my Viper’s Video Quicktags plugin. The player is not compatible with the GPL license and as a result cannot be included in plugins that are hosted in the WordPress.org plugin repository. I shouldn’t have added the player to my plugin in the first place but it was the only decent player at the time oh so many years ago. Today I have rectified that mistake by removing it from my plugin’s download.

However do not fear — I have not actually removed the functionality, I just no longer bundle it with my plugin. If you wish to continue to use the player to embed .flv and .mp4 files after upgrading to version 6.4.0 of my plugin, you will need to download this ZIP file and extract it to your wp-content folder, resulting in /wp-content/jw-flv-player/player.swf. I’m hosting the ZIP file since my plugin uses an old version of the player that is no longer available for download from the official site.

This is a temporary band-aid fix. The eventual plan is to switch to the GPL-licensed Flowplayer but that won’t happen until I complete the latest recode of my plugin which introduces many much-needed features. Whether I’ll actually ever finish version 7.0 of my plugin is another matter though.

If you have any questions or need any help, please post over on the Viper’s Video Quicktags page.

Did You Know GitHub Supports SVN?

GitHub is an awesome code repository and code distribution website. It’s used by countless people and organizations to develop and release code.

I’ve wanted to made use of GitHub for a very long time now but the site is built around the Git revision control software. However I instead extensively use Subversion (SVN) in my daily life and have no desire or need to learn Git — everything I currently do requires that I use SVN. Not to mention that the Git experience on Windows is absolutely horrendous.

So I was incredibly pleased to learn that GitHub supports SVN! They added full support way back in October 2011 but I somehow missed the memo.

This is super good news because it means I can now start to distribute my code and projects via my my GitHub page. Nothing is there quite yet but I hope to package up and commit some code to there soon. 🙂

wp_list_pluck() Is An Awesome WordPress Function

If you’re a WordPress developer and you don’t know about wp_list_pluck(), then listen up!

Say you have an array called $posts that contains this:

[0] => stdClass Object
	[ID] => 675
	[post_author] => 5
	[post_date] => 2010-07-25 19:40:01
	[post_date_gmt] => 2010-07-25 19:40:01
	[post_content] => This site is using the standard ...
	[post_title] => About The Tests

[1] => stdClass Object
	[ID] => 501
	[post_author] => 6
	[post_date] => 2010-08-01 09:42:26
	[post_date_gmt] => 2010-08-01 16:42:26
	[post_content] => The last item in this page's content ...
	[post_title] => Clearing Floats

[2] => stdClass Object
	[ID] => 174
	[post_author] => 5
	[post_date] => 2007-12-11 16:25:40
	[post_date_gmt] => 2007-12-11 06:25:40
	[post_content] => Level 1 of the reverse hierarchy test ...
	[post_title] => Level 1
	)

[3] => stdClass Object
	[ID] => 173
	[post_author] => 5
	[post_date] => 2007-12-11 16:23:33
	[post_date_gmt] => 2007-12-11 06:23:33
	[post_content] => Level 2 of the reverse hierarchy test.
	[post_title] => Level 2

How would you go about getting all of the post_title values? Well obviously you could do something like this:

$post_titles = array();
foreach ( $posts as $key => $post ) {
	$post_titles[$key] = $post->post_title;
}

Sure, it’s not that complicated but that can get repetitive in your code. So how about this instead?

$post_titles = wp_list_pluck( $posts, 'post_title' );

Much easier, right? 🙂

Thanks Michael Fields for reminding me about this great function!