If you’re hosting your WordPress website on DreamHost, and use their cron system to offload your WordPress faux-cron for better reliability, be careful of what version of PHP you have in your code.
I recently had an issue where my cron events weren’t firing, and after enabling email output, I ended up with something like this PHP error message:
Parse error: syntax error, unexpected '?' in /path/to/file.php on line 123
It turns out that WP-CLI was running PHP 5.x via the DreamHost cron system. I had PHP 7.x specific code in my theme. To fix this, I had to set the WP_CLI_PHP environment variable in my cron job:
export WP_CLI_PHP=/usr/local/php72/bin/php
wp cron event run --due-now --path=/home/path/to/wp/ --url=https://example.com/
WordPress has a built-in function called wp_safe_redirect(). This allows you to create redirects in code, but only to whitelisted domains (via the allowed_redirect_hosts filter).
The downside to this is that you have to remember to whitelist the domains. It’s easy to forget if you’re doing a lot of redirects, for instance with the WPCOM Legacy Redirector plugin.
When this happens, all un-whitelisted redirects will be redirected by default to /wp-admin/ instead, and can cause a headache trying to figure out what’s going wrong.
I had an idea to solve this problem. A simple logging plugin that logs failed redirects and adds a dashboard widget to show the domains and number of times the redirect has failed:
So if I am doing some development and want to purge one or more caches, I need to go dig around three different places to purge these, and that’s not fun. To help combat this, I made myself a simple Admin Dashboard widget with quick access to purge each of these:
Here’s the code:
<?php
class Emrikol_Cache_Dashboard {
public static function instance() {
static $instance = false;
if ( ! $instance ) {
$instance = new Emrikol_Cache_Dashboard();
}
return $instance;
}
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
if ( $this->is_admin() && isset( $_GET['ead_purge_object_cache'] ) && check_admin_referer( 'manual_purge' ) ) {
$did_flush = wp_cache_flush();
if ( $did_flush ) {
add_action( 'admin_notices', array( $this, 'message_object_cache_purge_success' ) );
} else {
add_action( 'admin_notices', array( $this, 'message_object_cache_purge_failure' ) );
}
} elseif ( $this->is_admin() && isset( $_GET['ead_purge_wp_super_cache'] ) && check_admin_referer( 'manual_purge' ) ) {
global $file_prefix;
wp_cache_clean_cache( $file_prefix, true );
add_action( 'admin_notices', array( $this, 'message_wp_super_cache_purge_success' ) );
} elseif ( $this->is_admin() && isset( $_GET['ead_purge_OPcache'] ) && check_admin_referer( 'manual_purge' ) ) {
// Taken from: https://wordpress.org/plugins/flush-opcache/
// Check if file cache is enabled and delete it if enabled.
// phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
if ( ini_get( 'OPcache.file_cache' ) && is_writable( ini_get( 'OPcache.file_cache' ) ) ) {
$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( ini_get( 'OPcache.file_cache' ), RecursiveDirectoryIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST );
foreach ( $files as $fileinfo ) {
$todo = ( $fileinfo->isDir() ? 'rmdir' : 'unlink' );
$todo( $fileinfo->getRealPath() );
}
}
// Flush OPcache.
$did_flush = OPcache_reset();
if ( $did_flush ) {
add_action( 'admin_notices', array( $this, 'message_OPcache_purge_success' ) );
} else {
add_action( 'admin_notices', array( $this, 'message_OPcache_purge_failure' ) );
}
}
add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widgets' ) );
}
public function add_dashboard_widgets() {
if ( $this->is_admin() ) {
wp_add_dashboard_widget( 'emrikol_admin_dashboard', 'Cache Control', array( $this, 'show_admin_dashboard' ) );
}
}
public function show_admin_dashboard() {
if ( false === get_parent_class( $GLOBALS['wp_object_cache'] ) ) {
// Persistent Object Cache detected.
?>
<div class="activity-block">
<span class="button"><a href="<?php echo esc_url( wp_nonce_url( admin_url( '?ead_purge_object_cache' ), 'manual_purge' ) ); ?>"><strong>Purge Object Cache</strong></a></span>
<p>Force a purge of your entire site's object cache.</p>
</div>
<?php
} else {
// Transients!
?>
<div class="activity-block">
<h3>Transients</h3>
<p>Transients cannot currently be removed manually.</p>
</div>
<?php
}
if ( function_exists( 'wp_cache_clean_cache' ) ) {
// WP Super Cache!
?>
<div class="activity-block">
<span class="button"><a href="<?php echo esc_url( wp_nonce_url( admin_url( '?ead_purge_wp_super_cache' ), 'manual_purge' ) ); ?>"><strong>Purge Page Cache</strong></a></span>
<p>Force a purge of your entire site's page cache.</p>
</div>
<?php
}
if ( function_exists( 'OPcache_reset' ) ) {
// PHP OPcache.
?>
<div class="activity-block">
<span class="button"><a href="<?php echo esc_url( wp_nonce_url( admin_url( '?ead_purge_OPcache' ), 'manual_purge' ) ); ?>"><strong>Purge PHP OPcache</strong></a></span>
<p>Force a purge of your entire site's PHP OPcache.</p>
</div>
<?php
}
}
public function message_wp_super_cache_purge_success() {
echo '<div id="message" class="notice notice-success is-dismissible"><p>Page Cache purged!</p></div>';
}
public function message_object_cache_purge_success() {
echo '<div id="message" class="notice notice-success is-dismissible"><p>Object Cache purged!</p></div>';
}
public function message_object_cache_purge_failure() {
echo '<div id="message" class="notice notice-error is-dismissible"><p>Object Cache purge failed!</p></div>';
}
public function message_OPcache_purge_success() {
echo '<div id="message" class="notice notice-success is-dismissible"><p>PHP OPcache purged!</p></div>';
}
public function message_OPcache_purge_failure() {
echo '<div id="message" class="notice notice-error is-dismissible"><p>PHP OPcache purge failed!</p></div>';
}
private function is_admin() {
if ( current_user_can( 'manage_options' ) ) {
return true;
}
return false;
}
}
Emrikol_Cache_Dashboard::instance();
One of the most important things to do when working on new themes, plugins, or debugging issues in WordPress is to turn on WP_DEBUG. According to the Codex:
WP_DEBUG is a PHP constant (a permanent global variable) that can be used to trigger the “debug” mode throughout WordPress. It is assumed to be false by default and is usually set to true in the wp-config.php file on development copies of WordPress.
It’s common to edit the wp-config.php file every time you want to turn this off and on, but another way to do it is via a secret cookie:
At WordPress.com VIP one of the features we have on our platform is automated concatenation of Javascript and CSS files when registered through the core WordPress wp_enqueue__*() functions.
This plugin was written to work with nginx, but the server running derrick.blog is Apache. I’ve worked around this and have nginx-http-concat running fully in WordPress, with added caching.
The bulk of the plugin is this file, which does all of the work of caching and calling the nignx-http-concat plugin:
The problem came up recently about how to make sure plugins activated in the WordPress plugin UI don’t get deactivated if they are necessary for a site to function. I thought that was an interesting thought puzzle worth spending 15 minutes on, so I came up with this function as a solution:
I made a small site recently where I wanted all newly registered users from a specific email domain to automatically be administrators (this is a terrible idea, don’t do it). The user registration was restricted by Single-Sign-On and 2-Factor Authentication, so I felt relatively safe doing this, especially since it was only a “for fun” project.
The interesting bit of code that upgraded users to admins is as follows:
Last week an interesting issue came up for a client. Somehow a few posts were moved from Published to Draft. Unfortunately, post status isn’t stored in revisions so it’s unlikely we’ll ever know who or how it happened. Luckily there’s a simple solution I found to log all post status transitions in post meta:
I recently worked on migrating a site to a different server and for one reason or another, some of the images did not come over properly. While I could have just re-downloaded and re-imported all of the media, it would have taken quite a while since the media library was well over 100Gb. Instead, I opted to use WP-CLI to help find what images were missing: