Understanding get_template_part

If you’re creating WordPress themes you surely came across the get_template_part function at one point or another. It’s one of those hidden gems inside of WordPress that don’t get the attention they deserve. Let’s change that.

The get_template_part function is essentially a PHP include or require, on steroids:

  • It already knows where your theme is located and it will look for the requested file in that theme’s directory
  • It doesn’t issue a warning or fatal out if the requested file does not exist
  • It can search for other suitable files, if the requested one is not found
  • It knows about child themes and parent themes

Long story short, the get_template_part function allows you to break your theme down into smaller templates (or template parts), which can be reused across your other templates.

Although get_template_part is similar to a PHP include or require, you should not use it to include things like your theme options code, sidebars registration, custom widgets, etc. The get_template_part function should only be used to get template parts.

Let’s start off with some basic examples.

Continue reading

Tip: wp_cron() runs during init

Pro tip: wp_cron() runs during the init action at the default priority, i.e. 10. If you’d like things to be available during your cron tasks, make sure you initialize them earlier: the init action at priority 9 and less, or other actions that run before init — beware of these, since other things might not be available at that time.

Also, Core Control is a good plugin to test and debug cron tasks.

WordPress 3.5

WordPress 3.5 is here and it’s awesome. Media’s lovely, hyphens in shortcodes now work, and switch_to_blog() is so fast, your dog will never catch it, unless your database is down, in which case you should try HyperDB.

Always nice to see your own name in the list of contributors, which makes 3.5 the fourth major version I contributed to with at least one patch. The feeling is good, but I hope to do much more for 3.6.

Have you contributed a patch that landed in 3.5? If not, head over to the Core Contributor Handbook to find out how you can help!

Theme Publish is now on WordPress.com

I released a WordPress theme called Publish earlier this month, and today, thanks to the awesome Theme Team of Automattic, Publish has been made available to all WordPress.com users.

WordPress Theme Publish

Publish is a clean and minimal WordPress theme, perfect for (but not limited to) single-author blogs. While working on shipping Publish to WordPress.com customers, a bunch of improvements have been made to the theme itself, like support for uploading a custom logo (vs using Gravatar), better compatibility with many sidebar widgets and more.

I’m excited to see how Publish does on WordPress.com, and looking forward to launching more themes. You can start a new blog on WordPress.com with Publish or head over to WordPress.org for the downloadable version. Enjoy!

Reminder: Don’t Use the Short PHP Open Tag

Jetpack 2.0.1 was released last night, immediately followed by 2.0.2, which fixed a fatal error on some hosts, caused by a short PHP open tag. So here’s a reminder: never use the short form of the PHP opening tag:

<? _doing_it_wrong(); ?>

Always use the long form:

<?php _doing_it_right(); ?>

If your grep can do Perl regular expressions, you can search your entire codebase with a negative lookahead like this:

grep --include=*.php -rP '<\?(?!php|xml)' *

Joseph Scott has a simpler example that works without the -P flag. If you’re building WordPress themes, the Theme Check plugin will scan your theme for short tags automatically. Plugin Check will do the same thing for your plugins.

Stay safe.

Double Titles when Sharing on Google+

If you’ve got double titles when sharing your WordPress posts on Google+, it’s probably due to duplicate OpenGraph tags, which might have been caused by the latest update to Jetpack, which added OpenGraph tags in version 2.0. If you’re already running a plugin that outputs the “og:” tags in your document head, you’ll have to disable one or the other.

For example, to have WordPress SEO by Yoast not output the OpenGraph tags, go to SEO – Social and disable Facebook OpenGraph. If you’d like to disable Jetpack’s OpenGraph support, you’ll have to write some code, preferably in a plugin file:

add_filter( 'jetpack_enable_open_graph', '__return_false' );

Other plugins might be different, but you get the idea.

WordPress Community Summit 2012 Photos

I’m not very good with cameras, but here are some photos taken during the WordPress Community Summit, held in Tybee Island, GA earlier this month.