Code snippets for WordPress

One of the most useful files within WordPress is functions.php, a file that resides in your theme folder. Functions.php is used to define functions, filters and other things that are used by your theme. By adding simple code snippets, it can also be used to add extra features and to the extend functionality of your theme, and also WordPress itself.

You can add code snippets to functions.php manually, or via a simple plugin. To add code snippets manually, simply copy and paste one of the snippets we’ve listed below into your functions.php file and hit ‘save’. Just remember to save a backup copy of your original file before you start in case you make a mistake. The alternative method, preferred by many, is to download a free plugin from the WordPress plugin repository called, appropriately, ‘Code Snippets‘, and paste the snippets into the plugin. Regardless of the method you choose, the code snippets are exactly the same.

If you add code snippets manually, remember that only the functions.php in your active theme is loaded and used within WordPress. If you add them via the ‘Code Snippets’ plugin, they will remain in use even if you change themes.

code snippets for wordpress

Useful Code Snippets for WordPress

Create a new custom post type

This is probably one of the most popular snippets in use these days, as people realise how useful custom post types can be. Adding a custom post type is pretty easy and requires only the snippet below. First, paste the snippet into your functions.php file and then edit it, replacing ‘Hotel’ with the singular name of your chosen custom post type, and ‘Hotels’ with the plural name. So for example, if you want to call your new custom post type ‘Magazines’,  replace ‘Hotel’ with ‘Magazine’ and ‘Hotels’ with ‘Magazines’.

You can also optionally add a custom taxonomy to your custom post type (see the next snippet). If you do so, you’ll need to edit the name of the taxonomies in line 27 – ‘taxonomies’ => array(  ‘custom-taxonomy1’, ‘custom-taxonomy2’ ), – and if you don’t need a custom taxonomy you should delete or comment out line 27.

Create a custom taxonomy

Creating a custom taxonomy for your custom post type takes just a few lines. Add this code to your functions.php above the snippet that you use to create the custom post type. Then, change the taxonomy name (country + countries) to whatever you want your taxonomy to be called. Also, change ‘hotel’ to the name of the custom post type you want to associate the taxonomy with. To create more than one taxonomy, simply duplicate the snippet, taking care to be sure you change all the names properly.

Require a featured image before publishing

It can be quite annoying to spend ages writing a post, only to discover at some point in the future that you forgot to specify a ‘featured image’ for the post. Similarly, if other people are able to create posts on your site, you probably don’t want to spend your time checking to see if they added a featured image before posting. This little snippet obliges you to add a featured image before your post can be published – if you try to publish without an image, the post saves as a draft, and isn’t lost!

Add shortcodes to text widgets

Many themes and plugins use shortcodes to display certain things (login forms, for example), but by default shortcodes can’t be used in text widgets, which restricts where you can use them. To allow shortcodes to run in your text widgets, just paste this code into your functions.php:

Restrict access to the WP Admin area to administrators.

A simple snippet which will ensure that only administrator-level users can access your site’s admin area:

Disable WordPress Emoji features

Love them or hate them, the recently added WordPress Emoji features are easily disabled:

Disable XML-RPC Pingbacks

XML-RPC Pingbacks can be abused by hackers and are used almost exclusively by spam posters anyway, so why not just disable XML-RPC Pingbacks?

Change the default WordPress email name

New users are often confused when they test their new site’s email, only to find emails are being sent using the name ‘WordPress‘ instead of their site name. Changing this default setting is very easy:

Redirect newly registered users

Use this code snippet to send newly registered users to a specific page after they register

Add custom thumbnail sizes

If you want to be able to use a different image size than the ones WordPress allows, you can add a custom thumbnail size via functions.php with this code. You can choose to have a ‘hard-cropped’ image (the crop shape is measured out from the centre of the image) or you can specify (for example) a specific width and allow the image height to be proportional. Simply edit the values in the snippet below. You can create as many image sizes as you like.

Make Font Awesome available

Remove Query Strings From Static Resources

If you’ve visited the gtmetrix or pingdom sites to check your site’s performance, it’s possible that you were told that you need to ‘Remove Query Strings From Static Resources’. Doing this can help caching and reduce loading times, though the differences will be small. Getting rid of Query Strings is easy; simply paste the following into your functions.php:

See when a user last logged on

If you want to be able to see whan your users last logged on, this little snippet allows you to do so: