Tag Archives: tips

A Note About get_template_part and Child Themes

Tip: if you’re wondering why your WordPress template file (for example index.php) is not being executed, perhaps a different file (archive.php) is overriding it. Now this may sound obvious, but not when you start using get_template_part to organize your theme files, and not when you’re making a child theme.

Let’s take a quick look at the following snippet:

get_template_part( 'content', get_post_format() );

That will look for files like content-link.php, content-gallery.php and so on, and will always fall back to content.php if none of the others are found. Now, imagine you’re working on a child theme. You create content.php and content-gallery.php. You expect gallery posts to pick up content-gallery.php, which is correct.

You also expect a link post to pick up your content.php file because there is no content-link.php in your child theme, right? This is also correct, unless your parent theme has a content-link.php file, which will be of higher priority to the template loader, despite the child-parent relationship between the two themes.

It does make sense, otherwise a simple index.php file in your child theme would override all of the parent theme’s templates, because index.php is a fallback for everything. That would render child themes useless.

So when working with get_template_part (and theme templates in general, thanks Chip!) especially child themes, don’t forget to check (and preferably study) the parent theme’s template files. Also keep the template hierarchy fresh in your head every time you create a new theme template file. It tends to evolve with every release.



Sync Time and Date in Ubuntu Linux

Quick tip! If you’re using virtual machines to develop and test your web applications, you probably use the “save state/snapshot” feature instead of turning it off and on every time. I noticed after a while that my time was out of sync (a few weeks late) which caused a bunch of problems. Luckily, I found this simple command:

sudo ntpdate ntp.ubuntu.com

It will sync your time with the Ubuntu Time server. You can even add a cron entry as suggested by this article. To display the current time and date, simply type date. Hope this helps :)



WordPress Multisite with Wildcard Subdomains

If you’re working with WordPress Multisite in your local environment, you might have noticed that dealing with a subdomain install is a pain, because your hosts file doesn’t support wildcard entries for hosts, i.e. you cannot do something like this:

127.0.0.1 *.multisite.lo # will not work!

There are quite a few solutions though, first and easiest of which is to run multisite with a subdirectories setup. The second solution would be to manage you hosts file manually and create an entry for each subdomain in you installation. This could be a pain if you’re working with quite a large amount of subdomains, in fact you can end up spending most of your time editing you hosts file and flushing DNS cache.

I wrote a little snippet that you can use in a plugin file or functions.php which prints out a string you can copy and paste into your /etc/hosts file. It generates an entry out of all the existing domains in your WordPress multisite install and maps them to the IP address you specify.

add_action( 'wp_footer', 'print_entry_for_hosts_file' );
function print_entry_for_hosts_file() {
    global $wpdb;
    $domains = $wpdb->get_col( "SELECT domain FROM $wpdb->blogs;" );
    echo "127.0.0.1 " . implode( ' ', $domains );
}

The wp_footer hook will get it to print the entry in your theme’s footer but you can attach it to any other hook if you like. When actually pasting the generated entry into your hosts file, make sure you remove the previous entry for your domain to keep the file clean and shiny. Comment out the add_action line to hide the mess until you add a few more sites to the network and need to update you hosts file again.

I agree it’s still a bit of a pain but it really helps! The third and probably most correct way of handling wildcard domains working locally would be to set up a DNS server. It’s not always worth the trouble though.

If you had experience dealing with wildcard domains in WordPress multisite, please share your thoughts in the comments section below. If you installed a local DNS server, feel free to link to any guide or tutorial on how to do that on your operating system. Thanks!



About the "Lock in Effect" in WordPress Themes and Plugins

The WordPress themes and plugins market is huge these days. With all that wide range of products available, we sometimes stumble into situations where we’d like to change our mind, i.e. use a different plugin or theme instead of the one we’re currently using.

Eventually we figure out that it’s incredibly tough to replace some of the themes and plugins, because as soon as they’re deactivated, all (or part) of our data is lost, and the new theme or plugin that was supposed to replace the old ones, doesn’t see the data we previously had. So we say that we’re locked in.

This post describes the lock in effect, shows you some methods to identify such themes and plugins. Plus, if you really do have to use one such theme or plugin, we’ll cover some tips on escaping or locking yourself out.

Continue reading



How to Get the Current URL in WordPress

Here’s a quick tip! I was wandering around the web for the perfect solution to retrieve the current URL in a WordPress theme or plugin. I found a bunch of solutions for PHP, but not directly related to WordPress so I thought there has to be an easier way, and after a few hours of examining with global variables seems like I found it.

global $wp;
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );

Beats all the $_SERVER approaches and no need to identify whether the host is using SSL, etc. You can add a trailing slash to that with trailingslashit if you need to, and it looks much cleaner too. Hope you enjoy that, and thanks for tweeting ;)



Posted this yesterday on Twitter, thanks for all the retweet and fave love! I started seeing quite a lot of tweets and posts on how to do things “without a plugin” in WordPress and 99% of them involve writing snippets in your theme’s functions.php file. Now, how does that differ from writing a plugin?

  1. It’s more difficult to manage and maintain.
  2. If something stops working you can’t “deactivate” your functions.php snippets one by one to figure out which is causing the problem, you can do this with plugins.
  3. When you switch to a new theme you’ll have to port and then merge and cleanup the functions file too.
  4. It’s easier to share plugin files than it is to share functions.php snippets.

I’m pretty sure there are a lot more reasons. So dear all, write plugins, even if they’re dead simple and do one thing only. The “all in one” plugins carry the real overhead which is never used. Then, when you’re ready, submit your plugins to the WordPress.org Plugins Directory to give back to the community. That’s why the open source WordPress is so strong.



Google Analytics Proxy with Nginx

Here’s a quick tip! If you need to serve a specific script, stylesheet or any other file from your own domain, you can easily proxy it with nginx. A good example is the ga.js file for Google Analytics. Here’s how I proxy it with nginx, in the server context:

# Google Analytics Proxy
rewrite ^/ga.js$ /ga/ last;
location /ga/ {
        proxy_pass http://www.google-analytics.com/ga.js;
        break;
}

This rewrites the ga.js filename to the /ga/ pseudo-directory, in the context of which I can use the proxy_pass directive to fetch the file from Google. This way I have total control over the file that’s being served and especially the HTTP headers, which I was after in the first place.

You can repeat the trick with basically any file, but keep in mind that each one is a little extra load on your server, so add a caching layer where possible.



Protected Meta in WordPress

I was doing a few tweaks to the Twitter Embed plugin earlier today and found out that authors that access to the custom fields interface could exploit them to print unfiltered HTML. This happened because I cached the HTML retrieved from the Twitter API in an unprotected meta field to the post.

The easiest workaround was to add an underscore prefix to the meta key, so my_meta_key for example, would become _my_meta_key. For a second though, I thought that somebody might type in the underscore in the custom fields interface too, so I spent a few minutes reading the admin ajax source code.

It turns out that there is a call to a function called is_protected_meta defined in meta.php, which does the check for the preceding underscore, so the custom fields interface will not allow working with protected meta fields no matter on what user level, which is good! It still allows themes and plugins to use get_post_meta and update_post_meta.

So the lesson learned today — if you’re storing sensitive data in your post meta fields, don’t forget to protect them from eyes by prefixing your meta keys with underscores.



Here’s a quick tip! Don’t apply the_content filters because some plugins (especially social share plugins) will use that filter to add something extra before or after your content, assuming it’s your main post or page content, which is fair. I’m also pretty sure that attachment posts can add things like images there too.

So if you’re using the_content filter somewhere in your footer for example, to allow automatic paragraphs and perhaps shortcodes, you’re better off with wpautop and do_shortcode. You can also create your own unique filter tag with apply_filters and then add the two functions to your new filter with add_filter.



Updated my Sony Vaio VPCM12M1R netbook to Ubuntu 11.10. The update went quite smooth although wireless immediately stopped working. Well it didn’t quite work with Ubuntu 10.10 either but the RT3090 package fixed it last time. Luckily I didn’t remove the package and was lucky to get things working on 11.10 simply by:

sudo dpkg -i rt3090-dkms_2.3.1.3-0ubuntu0~ppa1_all.deb

And obviously rebooting the netbook. Everything’s back to normal now so yeah, yet another successful Ubuntu upgrade. Loving the changes to Unity in 11.10!