Integrating AMP into WordPress: A Comprehensive Guide

AMP is an emerging technology that’s gaining the attention of online marketers in all fields.  It formats content in a way that loads blazingly fast for mobile visitors, whose page load time expectations can be as little as three seconds. I recently worked with a client who wanted to improve their page load times by … Continue reading Integrating AMP into WordPress: A Comprehensive Guide

The Definitive Guide To PHP’s isset() And empty()

Check out The Definitive Guide To PHP’s isset() And empty() for a deep dive on these faux-function language constructs.

TL;DR

  • always develop with error reporting turned to 11 (and fix all real errors)
  • always use empty or isset for variables that may legitimately not exist
  • never use empty or isset for variables that should exist

Note: To deal with PHP notices in 3rd-party code, if you can’t contribute fixes upstream you may want to consider using a plugin like Zack Tollman’s Ostrichcize.

it [is] impossible to distinguish between a variable that does not exist and a variable that has the value null. […] Specifically for arrays, there’s an alternative to isset($array['key']) called array_key_exists. This function specifically does what it says: it returns true if an array key exists, regardless of the value of that key. That makes it possible to detect null values in arrays

Note that array_key_exists() is used in WP_Customize_Manager::post_value() for this reason. Since the Customizer’s dirty setting values are encoded as JSON it is possible for a post value to be null (although wp.customize.Value doesn’t directly facilitate seting this value).

/via @Josh412

Use meta_input and tax_input with wp_insert_post() & wp_update_post()

We were recently in a situation in a project where we wanted to rebuild/invalidate a cache when a particular post was updated. Some of the key data for the post is stored in postmeta. So we have code that looks like: The problem here is that we can’t reliably use the save_post action to flush the cache because … Continue reading Use meta_input and tax_input with wp_insert_post() & wp_update_post()

Isolating PHPUnit tests to run

PHPUnit tests can be really slow, especially for large plugins that integrate with external APIs. It can be tempting thus to bypass the pre-commit hook via git commit –no-verify, but then Travis CI (or Bamboo) will eventually complain and the issue will come back to you, or worse the issue will get ignored. So we … Continue reading Isolating PHPUnit tests to run

PHP now upgraded to 5.6 in Quickstart

VIP just merged a PR which upgraded PHP to 5.6 in Quickstart. This is an upgrade from 5.4. With Quickstart being upgraded to 5.6, this should be a signal that we’re now in the clear to use any PHP 5.6 features (and 5.5 features) in our plugins. The first useful feature that comes to mind for us … Continue reading PHP now upgraded to 5.6 in Quickstart