Skip to Main Content

Wordpress

Improving WordPress Part 2 – Better Auto Excerpts and Feeds

WordPress allows most HTML in the post_content field, allowing a fair amount of flexibility for formatting post content for browsers. However, when that content needs to be presented in feeds or excerpts, it is run through a set of sanitizing filters that strips this HTML, leaving bare text.

For the most part this works ok, but when WordPress strips block level tags like <div>, <blockquote>, or even <br>, the remaining text can be jammed together, causing words to run together.

It would normally be a fairly simple task to add a regular expression which would add whitespace around such tags, before stripping them. However, WordPress considers the functions in question so integral to the core functionality, that they do not include any filters to hook to at all.

To address this shortcoming, I needed to step back a few levels, and rewrite several WordPress core functions from the last place there is a usable hook. To save you all the trouble i went through, here is a single-file plugin that will insert the relevant missing formatting:

Improving WordPress Part 1 – 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. read more »

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. read more »

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. read more »

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.