WordPress Development: Best Practises

WordPress Development: Best Practises

Andy Holland

When designing and building WordPress themes and plugins, you need to ensure that you follow some best practice guidelines to get other people to enjoy them too. After all, that’s what we want when we write the code. Starting out with best practice methods means that you’re starting out on the best footing.

WordPress Coding Standards

Rule number one of creating anything for WordPress is make sure that you follow the WordPress Coding standards. These help to improve readability for everyone, remove the risk of common errors and also means that anyone who wants to amend your code can get their head around exactly what it is that you’ve done.

Because WordPress is open source, it really helps if everyone writes their code in the same way. Then everyone is on the same page. It may be difficult at first, but once you have got to grips with the style, it will be second nature and it’ll mean that all of your code is clean and easy to read.

The standards are separated into the four main languages and can be found here:

CSS Coding Standards

HTML Coding Standards

JavaScript Coding Standards

PHP Coding Standards

Avoiding Function Name Collisions

A name collision is when a function has the same name as another function which has been defined. If, for example, you have a function called get_the_post_terms()and your plugin has the same name you will end up with this:

Fatal error: Cannot redeclare get_the_post_terms() (previously declared in…

If you want to avoid this, there are a number of things you should do.

  1. Use a prefix

If we design a plugin called Elementary Digital’s Awesome Plugin, we can use a eda_prefix in all the functions to stop this happening.

  1. Wrap Your Functions inside a Class

Even if your plugin is super simple and doesn’t need a class, making one can help keep it organised. Try something like this:


class Eda_Mailer {

static function send( $post_ID )  {

$friends = ‘jeffhead@example.org’;

mail( $friends,”New post!”, ‘Check my new post in ‘ . get_permalink( $post_ID ) );

return $post_ID;



add_action( ‘publish_post’, array( ‘Eda_Mailer’, ‘send’ ) );

In this example the class name is prefixed and the function is only simple. To call it, I simply need to do this:

1 Eda_Mailer::send( $post_id );

Comments on Code

Comments in the code can be a dev’s best tool when creating anything. It means that everyone understands what each part is for and then even when a code has grown in complexity, everyone can still tell what each part is doing. As a collaborative project, WordPress and its developers should continually do each other the service of making everything just a little bit easier.

This is quite common in WordPress. For example, in the wp_mail() function which is in wp-includes/pluggable.php we find:


* Send mail, similar to PHP’s mail


* A true return value does not automatically mean that the user received the

* email successfully. It just only means that the method used was able to

* process the request without any errors.


* Using the two ‘wp_mail_from’ and ‘wp_mail_from_name’ hooks allow from

* creating a from address like ‘Name <email@address.com>’ when both are set. If

* just ‘wp_mail_from’ is set, then just the email address will be used with no

* name.


* The default content type is ‘text/plain’ which does not allow using HTML.

* However, you can set the content type of the email by using the

* ‘wp_mail_content_type’ filter.


* The default charset is based on the charset used on the blog. The charset can

* be set using the ‘wp_mail_charset’ filter.


* @since 1.2.1


* @uses PHPMailer


* @param string|array $to Array or comma-separated list of email addresses to send message.

* @param string $subject Email subject

* @param string $message Message contents

* @param string|array $headers Optional. Additional headers.

* @param string|array $attachments Optional. Files to attach.

* @return bool Whether the email contents were sent successfully.


function wp_mail( $to, $subject, $message, $headers = ”, $attachments = array() ) {




// Send!

try {

return $phpmailer->Send();

} catch ( phpmailerException $e ) {

return false;



This helps us to see what is happening.

We hope with these tips you can make life easier for yourself, your colleagues and the community in general.

Andy Holland

Author Andy Holland

More posts by Andy Holland