Pass WordPress Admin Notices Across Page Redirects

I’ve been writing a lot of WordPress plugins recently, and one task keeps popping up, that doesn’t seem to have a definitive answer in the WordPress core.  I’ll need to show a message (confirmation, success, error, notice, warning, info, etc) to a user, after redirecting them through one of the scripts used to save changes, such as admin_post.php (for example, using the save_post action with a custom metabox).

Because I’m a strong proponent of DRY programming (Don’t Repeat Yourself), I wanted a universal tool to solve the problem, once and for all.  I’ve created a small class that I call WP_Persistent_Notices.  It’s a singleton, and is pluggable, so there should be no issues with simply including it in a theme or plugin, as is, and not worrying about another theme or plugin also including it.

It provides a simple filter interface for adding an admin notice, as well as a helper function to simplify the call, or allow it to more easily be used with variables.  The helper can be called as follows:

add_persistent_notice( array(
    'type' => 'error',
    'message' => "Houston, we have a problem!"
) );

By default, notices are rendered at the top of the page, by the ‘admin_notices’ action, but notices can alternatively be defined with an optional ‘location’ attribute, that will allow you to render them anywhere, with

<?php render_persistent_notices( $location ); ?>

Here’s the Gist I created for the class:


Why do you use a session variable to store the notices, instead of {insert method here}?

It seemed like the best compromise at the time.  POST/GET parameters cause issues with refreshing, COOKIE can only be set before rendering content, so it limits the user’s ability to add a notice at any point, and transients are slower and create potential concurrency issues when two users are working with the same account.

What if my server config can’t use sessions, or I need some special configuration?

There are three filters ( ‘wp_persistent_notices_replace_initialization’, ‘wp_persistent_notices_replace_save_notices’, and ‘wp_persistent_notices_replace_retrieve_notices’ ) that can be used to transparently replace the SESSION functionality with any other functionality of your choice, such as transients or memcache storage.

What’s with sorting the notices by type?

It seems to be the most logical way to waterfall the importance of multiple notices, but if you don’t like it, there is a filter to disable, a filter to replace, and a set of filters to change the weights of the various notice types (including custom types).

Why isn’t this class part of WordPress core?

I don’t know, that’s not my department.  Maybe Automattic will see this post and feel similarly, or maybe you can plus one my response to the ticket at:

Your class is bad, and you should feel bad!

Well, that’s not really a question, but ok… why not contact me about it, or just fork the gist? ..or best yet, do both!


The Facts of Life (politics in a coding blog??)

First things first, I am not a politician, and frankly, I am uninterested in changing anyone’s opinions on how to run this country.  In fact, I’d prefer to be staying out of this field entirely.  However, large numbers of otherwise intelligent people have been making statements recently that have no basis in logic, and I needed to weigh in on this intellectual dishonesty.  We need to talk about the two-party system.

Let me set the record straight for some of the voters out there, especially those considering who the “lesser of two evils” is. I am not going to talk about the qualities of various candidates, because that is shouting into a hurricane. Instead, I’m gonna give you some cold, hard, irrefutable facts about voting.

Do you live in Ohio, Colorado, Iowa, Nevada, New Hampshire, Virginia, or Florida? If not, your vote will not have any effect on the presidency. Your state is not contested, and you are not an elector. You are utterly and completely meaningless to the overall election.

I say this because many people are going to tell you that this is the one year when you need to hold your nose and stick to your party (just like it was 4 years ago, and 4 years before that…) so as to not be a “spoiler”. However, the hard truth is that your vote is not going to effect the election. This is a fact, not political rhetoric.

As such, the only thing you can have any effect on is the general election numbers. So, if you want change, don’t vote “against” anyone. Vote for someone you actually support, no matter what their political party is. Show an increase in support for their viewpoint, and show the politicians in power that their are other viewpoints than theirs.

The second fact is that there are NOT only 2 candidates for the presidency. There are hundreds of registered candidates for the 2016 presidential election, as of this moment. Here’s a list of a few of them ( You may be thinking “sure, but there are only two that matter…”

That brings us to fact 3: Bernie Sanders is NOT a Democrat. Despite running in the Democratic Primary, Bernie has been an Independent socialist since 1979. Consider that for a moment: by calling himself a Democrat for a couple of months, without changing any of his policy points, an independent socialist from Vermont came within a single percent of gaining the Democratic Nomination. This is cold, hard proof that third party candidates can have the same level of support as the two de-facto parties. The only thing holding them back is people, just like you, who say “I can’t vote with my beliefs this year” or “It doesn’t matter that s/he’s right, s/he can’t win”.

This leads to a single conclusion: for most of you, voting democrat or republican will be meaningless to the election.  It serves only as a crutch that lets you say “I was on the team that voted against X”, without causing any change.

To finish with a single personal stance, I believe that this sort of tribalism should have no place in modern politics, and I believe that most young people are ready to finally end it.  Personally, I’ll be voting with whichever third party candidate is polling highest on the day of the election, but that’s just my opinion.


Introducing WP SmartCrop – Intelligent, Responsive Image Cropping for WordPress

I’ve been taking another look at old code recently, and I dusted off a couple on-the-fly smart cropping demos that I wrote, years ago. At the time, they relied on questionable hacks and ran extremely slowly, making them impractical for real-world use.

However, with the rise and widespread adoption of CSS3, and the recent incorporation of Responsive image srcsets into WordPress 4.4, The timing seemed right to finally complete the toolkit, and offer WordPress users truly responsive images.

WP SmartCrop is a free WordPress plugin that allows you to specify a ‘focal point’ for the images you upload.  Then, when the image is used in a post, or as a featured image, it will automatically crop the image to any dimensions set by the site’s stylesheet.

With this new feature, you can (for the most part) decouple your media from your design, without sacrificing the impact it has for viewers.


Use category interface with non-hierarchical custom taxonomies

When creating a custom taxonomy in WordPress, you get a different default interface for hierarchical taxonomies than for non-hierarchical taxonomies.  Hierarchical taxonomies get a checkbox-based interface, like Categories, while non-hierarchical taxonomies get an auto-complete interface, like Tags.

taxonomy callbacks

You can select the metabox interface used with the ‘meta_box_cb’ attribute of the ‘register_taxonomy’ call.  By default, hierarchical taxonomies use the callback ‘post_categories_meta_box’ and non-hierarchical taxonomies use the callback ‘post_tags_metabox’.  Unfortunately, the two options aren’t compatible with each other.

For some reason, when a post is saved or updated, non-hierarchical taxonomies expect to receive an array or comma-separated list of term names (as strings), while hierarchical taxonomies expect to receive an array of term ids (as ints).  Luckily, it is fairly trivial to translate between the two formats.

Here’s the code to make a non-hierarchical taxonomy that uses the category-style metabox:


Fix Broken images after WordPress 4.4 update

WordPress 4.4 brought an awesome new feature to the CMS system: Responsive images. As of 4.4, WordPress automatically adds the HTML5 srcset attribute to the image tags that it auto-generates. These srcset attributes define other sizes of the image, to be downloaded on devices with other screen resolutions and orientations.  Using responsive image tags allows sites to load faster on mobile, use less data, and be optimized better for the viewer’s screen.

Unfortunately, because of the way WordPress implemented the feature, these new srcset attributes can cause trouble on some sites.

If you are using Cloudflare, with Universal SSL, or if you are serving your site over both HTTP and HTTPS, you may see broken image tags in Chrome, or receive security warnings in other browsers.

The problem is pretty simple.  Wordpress expects your site to be served at the address stored in the ‘siteurl’ option, including the protocol stored there.  The new srcset image urls are built from that assumption, without running through the usual filters.  So, HTTPS sites can end up serving HTTP images in the srcset tag, causing all kinds of trouble.

Lockily, I’ve come up with a simple fix for the problem.  Just add the following code to your functions.php file, and your srcset images will all be served as protocol-independent urls:

This same approach can also be used to fix CDN urls in srcset tags, by using a different replace function.