The WordPress Customizer does not have a built-in control for setting background images. The background feature simply consists of four different controls:
For this feature, the Customizer also loads javascript that reveals the radio fields once an image has been uploaded. And on the front-end, inline styles are used to apply the background image to the body tag.
If a developer wanted to build additional background image options, the simplest option would be to take the same route: register four settings, load four controls, add custom javascript to show/hide fields if an image is set. Repeat as needed.
However, I started experimenting with a single control and thinking through how that could be implemented.
Issues with Current Implementation
Trac is a great place to find feedback on a particular WordPress feature, and there were a number of suggestions for background enhancements:
A single control generates all the field markup (image, repeat, size, attach, position)
The control also saves an attachment ID if a setting is defined (making it easier to generate alternate sizes)
The control automatically loads the javascript to hide/show the select fields
At the moment, six settings need to be registered (with their defaults and sanitization) if all the fields are going to be displayed. This still feels like a lot of syntax and I’d like to simplify it further- perhaps by saving all the data into a single serialized array for the theme_mod or option setting that is registered.
It also doesn’t address cropping, alternate UI ideas, or retina- mainly just because I haven’t had a chance to explore it yet. But it does have support background-position-y (added to the position field) and background-size (new select field).
I’d love for other people to take a look at this control and give me any thoughts, feedback, or pull requests. You can find the Custom Background Control on GitHub along with all its documentation.
I am completely new to PHP unit testing, but I decided it was time to learn after discovering a critical bug in a small WooCommerce extension I had built for a client.
The extension I built added a feature that allowed administrators to limit specific coupons to new customers only. I had done some manual testing and made sure that new customers could use the coupon and existing customers could not. But there was a logic bug I missed that prevented existing customers from using any coupons, even ones that did not have the “new customer” restriction.
After finding the bug, I knew there were several use cases I would need to check every time an update was made to the plugin:
New customer should be able to apply a coupon
New customer should be able to apply a coupon with a “new customer” restriction
Existing customer should be able to apply a coupon without a “new customer” restriction
Existing customer should *not* be able to apply a coupon with the “new customer” restriction
Obviously, checking this manually each time would be rather tedious- which is why I turned to unit tests. Continue reading →
When the url of a published post is updated in WordPress, the previous url slug is saved into a meta key called _wp_old_slug and a 301 redirect is created automatically. In most cases this is exactly what you would want to happen, but there are rare cases where it can cause a problem.
I hit an odd edge case when upgrading to WordPress 4.4 on WP Engine. At some point the slugs for two WooCommerce products had been swapped, so the _wp_old_slug for ‘product-1’ was ‘product-2’, and ‘product-2’ was ‘product-1’. This should have caused an infinite redirect immediately, but for some reason those rules were ignored until the WordPress 4.4 upgrade. Continue reading →
The JetPack plugin makes it easy to add share buttons to posts in WordPress. With a little custom code it’s also possible to track how often the share buttons are clicked and which URLs are being shared.
You can then conditionally load that hidden field on the specific templates where where it is needed.
However, if you’re using get_search_form() and don’t want to completely replace all the search form markup, an alternative would be to do a string replace before outputting the form:
<?php
// Output a search for that only searches portfolio post types
If you have you have a lot of user accounts for subscribers (or customers if using WooCommerce or Easy Digital Downloads), you might find these functions useful for blocking access to the dashboard and simplifying logins. Continue reading →
WordPress has a button in the editor for adding blockquotes to a post. But what if an author wants to add a citation? Unfortunately, it’s not possible without a little HTML knowledge.
This is a problem I’ve thought about a bit when designing DevPress themes (what markup should I be styling for?), but it’s also come up on a few client projects recently. Citations are a pretty common use-case, and it would be great if they were easier to add.
My solution was to build a plugin that replaces the existing TinyMCE button for blockquotes with an enhanced one. Continue reading →