Don’t Hide the Fact That You’re Using WordPress

There are quite a few blog posts, plugins and hacks suggesting to hide the WordPress version number, or hide the overall fact that you’re using WordPress. Don’t do it — it’s pretty useless.

There are hundreds if not thousands of ways to not only find out the fact that you’re using WordPress, but also find out the exact version number, regardless of any plugins or hacks changing or hiding the “generator” meta tag, the readme file and so on. A great post by my brother Gennady illustrates that.

Security

Most of these “hide my WP” solutions tend to market themselves from a security standpoint, especially with the recent botnet attack on WordPress sites. The truth is that these attacks don’t really care which version of WordPress you’re running. In fact, they don’t even care whether you’re running WordPress at all! How? Well that’s easy, they just take your domain and blindly fire POST requests to a file called wp-login.php, even if you’re running a non-CMS pure HTML website.

The same applies to known theme and plugin vulnerabilities. Go ahead and check your web server’s access logs, there’s a pretty good chance you’ll find requests to timthumb.php even though none of your themes or plugins use the TimThumb library.

So from a security perspective, the secret sauce is to use a strong password, as well as keep your themes, plugins and especially WordPress core up to date. Plugins such as Google Authenticator and Limit Login Attempts can give you that little extra protection.

The Ferrari Analogy

Sometimes people try hide the fact that they’re running WordPress because they’re afraid other humans will spot that and think they’re “unprofessional” or cheap. Well WordPress is the most professional content management system known to human kind, trusted by some of the largest companies worldwide and although free and open source, certainly not cheap.

When you buy yourself a new Ferrari, do you remove the Ferrari logos before showing it to your friends? No. Although if you did, it would still be obvious.

To wrap that up — don’t hide the fact that you’re using WordPress. Use a strong password, keep it updated and drive it with pride. If you bought a premium “hide my WordPress” plugin, you should ask for a refund and buy something useful instead.

Using get_template_part within Shortcodes

The get_template_part function is one of the most useful things available to WordPress theme developers. Although mostly used in themes for public, get_template_part is often used in custom WordPress websites as an alternative to the PHP include or require.

When using get_template_part with the Shortcode API, there are two things you should always keep in mind:

  • get_template_part executes .php files which (most likely) generates output
  • shortcode callback functions are expected to return a string and not generate any output

So when calling get_template_part within a shortcode callback function, you’ll see that all the output generated by get_template_part is output before the post content, and not replaced inline.

The solution is to use PHP’s output buffering. Create a buffer in your shortcode callback before running get_template_part, clear the buffer and return the content right after. Here’s a quick example with an ads shortcode, which can insert your theme’s ads.php file contents anywhere within a post or page:

function my_ads_shortcode( $attr ) {
    ob_start();
    get_template_part( 'ads' );
    return ob_get_clean();
}
add_shortcode( 'ads', 'my_ads_shortcode' );

The ob_get_clean() function stops buffering and returns whatever was output to the buffer after ob_start(). The same approach could be used with other functions and statement that generate output, such as include and require.

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.

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.

WordPress E-mails in HTML

WordPress sends e-mails in plain/text by default, and you can change that using the wp_mail_content_type filter. However, if you change the content type to text/html for all your e-mails, you might break some messages that contain links, because WordPress puts <angle brackets> around links.

From the RFC2396:

Using <> angle brackets around each URI is especially recommended as a delimiting style for URI that contain whitespace.

If you’re going to change the content type for all your e-mails issued by WordPress, don’t forget to rewrite the default messages with proper markup. You can do that with various other filters, like retrieve_password_message.

Finally, if you’re going to send e-mails in HTML, please make sure they all respond well to narrow screens on mobile devices, and preferably contain a plain/text version of the same message.

WordPress Actions vs. Filters

If you’re still wondering about the difference between WordPress actions and WordPress filters, Michael Fields posted a very cool explanation in this forum thread called Actions vs. Filters. I think it’s the best explanation of actions and filters I have seen so far, well done Michael, and keep up the good work!

Let’s pretend that WordPress is a Mexican restaurant and we have ordered a taco as illustrated by the following code…

<?php $taco = 'chicken'; ?>

Hat tip to Philip for finding this little two-year old treasure in the forums. If you’re interested in a very detailed article about the internals of actions and filters, check out Gennady’s post called Inside WordPress Actions and Filters.

Has anyone else stumbled across interesting ways of describing WordPress functions and APIs? Feel free to share your links!