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. read more »
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.
Sometimes a script needs to call a function that either uses a large amount of system resources, or that performs external calls to servers or APIs that place limits on requests. Often, however, the data returned by these functions isn’t highly volatile, and can be expected to remain the same for subsequent calls.
In these cases, it is necessary to implement a form of cache, to store the results of running the function once, and use the same results on subsequent calls (clearing the cache regularly, to prevent buildup).
Caching to disk is fairly easy to implement in PHP, but it is nice to have a prebuilt, general purpose solution that can be dropped in to any project.
read more »
This is less of a post, and more of a useful snippet. I’ve seen many posts online about sorting an array of names by surname, and they’ve all failed to be complete. Here is my addition to the pile. Keep in mind that it has known issues with compound last names that contain a space, like “Von Zinger”, “Mac Innis”, or “Di Vece”. However, it is a more robust solution than many others online today.
I was looking back over my old code from 2005, and I found some of the Duplicate Image Detection (DID) code that I wrote for Gallery 2, back in 2005. I decided to clean it up and post a simplified version for people working in PHP with the GD extension. read more »