Disable Object Cache Addition/Invalidation in WordPress

You can temporarily disable object caching in WordPress‬ by calling:

wp_suspend_cache_addition( true );

Ref. You can turn addition back on by passing false as the only argument. There’s a similar approach for object cache invalidation. However, there’s a big chance persistent object caching plugins won’t implement these functions like WordPress built-in object caching does, so they might be unreliable if working in an unknown environment.

Default Post Thumbnails in WordPress

Justin Tadlock wrote about the right ways to add default post thumbnails to your WordPress themes. One method is to filter on post_thumbnail_html, and the second method is to simply use else with has_post_thumbnail.

The second method is easier to understand, straightforward and future-proof. The filter in the first method would only work well when we need to change the markup of the thumbnail images output, like print a div with a background-image instead of an img element.

Order By Post Meta DATE in WP_Query

Sorting by post meta in WP_Query is quite easy with the orderby argument set to meta_value. If your meta value is a number, you can use meta_value_num, however, if you run into a situation where you have a date string, like 2012-06-15, both meta_value and meta_value_num won’t give you the desired results.

Luckily, you can hook into the posts_orderby filter and do some custom SQL, like this:

add_filter( 'posts_orderby', 'my_posts_orderby_date', 10, 2 );
function my_posts_orderby_date( $orderby, $query ) {
    global $wpdb;
    return " CAST( $wpdb->postmeta.meta_value AS DATE ) " . $query->get( 'order' );

Then do your regular WP_Query with both orderby and meta_key arguments, and no matter to what orderby is set to, it’ll use the SQL generated in the filter.

However, keep in mind that CASTs aren’t the fastest and most efficient queries in MySQL, so think about using timestamp (integers) for meta values and using meta_value_num for sorting. You have PHP functions such as strtotime that can help you format a date and time string, into a numeric timestamp.

Cheers, and stay updated!

How to Remove the Publish Box from a Post Type

I’ve been working plugin, where a post type was meant to only be read from the admin panel, but never edited. Removing the capabilities to edit the post type is one piece of the puzzle, and actually hiding the publish box is another. Have you ever heard people say WordPress is written using its own APIs? Well this is a perfect example:

add_action( 'add_meta_boxes', 'my_remove_publish_metabox' );
function my_remove_publish_metabox() {
    remove_meta_box( 'submitdiv', 'your_post_type', 'side' );

WordPress uses the very same add_meta_box function you would use to add modules to your post type. This means that it can easily be removed, just like any other metabox, with remove_meta_box.

Keep in mind that removing the publish metabox doesn’t mean the user can’t publish posts, so don’t forget about capabilities too!

Quick tip: Hide the Home Item on Your Front Page in WordPress

The title says it all — this one-liner CSS will hide the home item from your navigation menu, when you’re on the home page:

body.home .current-menu-item { display: none; }

If you’re not yet using wp_nav_menu to build your navigation menus, then, uhm, wake up! Also, the snippet relies on the body_class usage in your theme, and if your theme doesn’t use one, uhm, wake up! :)

I also want to avoid the discussion about how the primary navigation should be consistent through each and every page on the website, if at all possible :)

Never Set Defaults in the Database

Speaking of defaults, NEVER SET DEFAULTS IN THE DATABASE. Not ever. The theme options you store in the DB should always, always, *always* be something the user has selected. If the user selects the default, then that’s fine to set in the DB. but you don’t set it in the DB just because it’s not set at all. get_theme_mod has a second option for the default value. So does get_option for that matter. Use this.

Otto Wood, WordPress Core Developer on Custom Admin Screens

Thanks, Otto, for clearing that up :)

Default Custom Background for WordPress 3.4

Custom backgrounds are handled through add_theme_support since WordPress 3.4, and although you can add your custom callback to wp_head, you don’t need you. You can simply pass a default-image to add_theme_support for your custom background:

add_theme_support( 'custom-background', array(
	'default-image' => get_template_directory_uri() . '/images/default-background.png',
) );

The default custom background image will be used when one is not set, or the user hits the “Restore Original Image” button in the custom background options. However, the old method should still be compatible with WordPress 3.4, unless you’re doing something wrong :)

How to: Disable Jetpack Subscriptions Notifications

It’s more difficult to keep this in mind, than it is to actually execute it. Sometimes we (content creators) want to publish content to see how it feeds to our Twitter or Facebook accounts, or to see how it turns up in the RSS feed, so what we usually do is publish a “test” post and delete it afterwards (together with the tweet and Facebook post.)

However, sent e-mails cannot be deleted, once they’re out there, there’s no turning back. It’s not the best user experience to receive a “testing my twitter plugin” e-mail, right? Jetpack is one of the best ways to deliver e-mail notifications, about your new posts, to your subscribers. I wrote about it before and actually use it myself.

The best way to prevent all of this is of course to use dummy accounts or staging servers. The second best way to test your posts publishing out, is to wait for actual content (and if things didn’t work, try again next time), but sometimes we want to just try it out in our live environment, and if you do that, don’t forget to disable Jetpack subscriptions, which can be done in two easy steps. In the Jetpack configuration page under Subscriptions, hit Learn More and you’ll see a Deactivate button.

Disable Jetpack Subscriptions

Don’t worry, you will not loose all your subscribers, they’ll start receiving notification e-mails, when you Activate the subscriptions module again on that very same page, and yeah, don’t forget to activate it back when you’re done “testing things out.”

That’s about it! How do you handle test posts you don’t want your subscribers to see? Can you easily deactivate the e-mails with other plugins and services such as Feedburner? Share your thoughts and comments and thank you for subscribing!