Tag Archives: tips

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 :)



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!



It’s important for a network (WordPress Multisite) to have a valid default theme for newly created sites. The current version sets this to twentyeleven in default-constants.php. If your default theme isn’t installed or disabled network-wide, you might face some errors when creating new sites in the network. You can define WP_DEFAULT_THEME in your wp-config.php file.



This tweet gained some good attention on Twitter, so I thought it would be good to explain why. Then I found a support forums thread where Mark Jaquith pretty much explains it all:

esc_url() is for something like <a href="SANITIZE_THIS">text</a>

So if you’re going to use the URL in your HTML output, like a href attribute for a link, or a src attribute for an image element, you should use esc_url().

esc_url_raw() is for other cases where you want a clean URL, but you don’t want HTML entities to be encoded. So any non-HTML usage (DB, redirect) would use this.

The esc_url_raw() function will do pretty much the same as esc_url, but it will not decode entities, meaning it will not replace & with &#038 and so on. As Mark pointed out, it’s safe to use esc_url_raw in database queries, redirects and HTTP functions, such as wp_remote_get.

Oh, there’s now a codex entry for esc_url_raw too!



Daniel Bachhuber: The Zen of WordPress Development

Daniel Bachhuber of Automattic’s WordPress.com VIP team, gave this awesome talk at WordCamp Phoenix 2012 earlier this year. He walked through some things developers are overlooking when working with WordPress, and some great tips and tricks to speed up your development workflow.

One thing I learned from that talk is that I should stop using Textmate’s “search in project” and use ack instead, which is faster, available in the command line environment (no need for GUI), and has a bunch of options for output customization. By the way, here’s how you install ack on OS X:

  • Download and install MacPorts if you haven’t already.
  • Open your Terminal and type sudo port install p5-app-ack

You can find the notes and slides to the presentation on Daniel’s blog, and by the way, it was originally called “Five tenets to mastering WordPress development” :)

Hope you enjoy the video and feel free to share your thoughts in the comments section below.



Native Image Sizing “On the Fly” with WordPress

I tweeted it out not too long ago, and it seems to have gotten people’s attention. So why do WordPress theme developers still use TimThumb? One of the valid reasons seems to be dynamic image sizing. Of course if you’re using TimThumb for post thumbnails in your theme, your life saver is the add_image_size function — you don’t need “dynamic resizing” since all your thumbnails are of equal sizes throughout your theme.

However, the valid reason I seem to have bumped in, is the fact that sometimes you would want to use a specific size for an image, but only once, for one post or page. The add_image_size function here will add a lot of overhead, crunching all the uploaded images into the size you want to use only once. Like a shortcode that can resize and crop an image from your media library to whatever dimensions you tell it to:

[image src="http://example.org/wp-content/uploads/2012/03/image.png" width="150" height="150"]

Right, a shortcode (argh!) to dynamically resize an image. Fair enough, a lot of premium themes seem to have something similar, but mostly use TimThumb because it’s so simple. However, I’ve created an image shortcode function that resizes and crops images from the media library on the fly using the image_make_intermediate_size function available as early as WordPress 2.5!

It saves the resized image on disk, and returns the absolute URL to that image. Of course you’d want to add things like captions, alt tags and so on, but the basic idea is there. Why is this better than TimThumb?

  • It stores the image files on disk and uses absolute URLs to those images, meaning you won’t have problems hosting your thumbnails on a CDN.
  • It uses functions native to WordPress and works with the WordPress media library, so extra files are deleted when an attachment is deleted — no more junk in your cache/folder.
  • It doesn’t have a zero-day vulnerability :)

There are drawbacks too, like the fact that it’s a shortcode! Although I’m pretty sure we can use the same technique and hook it directly to the content, bypassing the shortcodes madness. Oh, and it doesn’t support remote files too, only the ones in your media library, but that’s a good thing, as we have all figured out by now.

Feel free to fork the Gist on Github and experiment with the code, I’m sure you can think of how to make it better, easier to use and faster. Thoughts and comments welcome, and thank you for stopping by!



Just a reminder, since this weekend I had to help out with yet another TimThumb-powered WordPress theme, broken by a “security patch” at the web hosting provider. This time it was Acquisto by Press75. The problems of Acquisto are not limited to thumbnails.



Using Jetpack for E-mail Subscriptions in WordPress

You can find a bunch of “subscribe to posts” plugins in the WordPress.org directory but from my personal experience, it’s very difficult to find something as good and reliable as Jetpack.

Jetpack E-mail Subscriptions for WordPress

The major difference is behind the scenes — when most post subscription plugins will use the wp_mail function to distribute your new post (using sendmail from your server or an SMTP account,) Jetpack will simply send your new post to the WordPress.com server, which will take care of the distribution for you.

Continue reading