Useful pieces of code

With plugins it’s very easy to expand the functionality of a WordPress website. On the frontend as well as the backend. Plugins can make your life as site owner a lot easier. Especially when your not very technical. Tens of thousands of plugins are waiting out there to be used.

Code or plugin

Although plugins are easy to work with, but not always necessary. Sometimes a piece of code can do the trick. You can put pieces of code in your theme files or the configuration files fo your WordPress installation. These will do exactly the same thing as a lot of plugins. Just like plugins, there are numerous pieces of code available that will make your website just that little bit better, faster, safer or more interesting.

This website uses pieces of code, aka code snippets, too. To make sure all traffic comes in on the safe https pages, I’ve added a code snippet to the .htaccess file. You will only have to do this once and never look at it again. Whilst plugins can get outdated.

Be careful

When adding code snippets to the WordPress or theme files, you have to be very careful! One mistake and the website can get the famous “white screen of death”! So make sure you always have a fresh copy of the file you are editing. Or use a tool like Notepad++, so you can undo changes you’ve made to the file and save it again.

If you make any changes to the functions.php of your theme, be aware that when you change themes, the code snippets of the old theme will not work anymore. You will have to edit the functions.php of the new theme as well.

With major changes, it is always good to have a recent backup! So you can always go back to the situation before the changes. But if all goes well, you don’t have to look at the changes for a long time!

Example code

An example of a code snippet: define(‘DISALLOW_FILE_EDIT’, TRUE);
This code snippet, that you put in the wp-config.php, turns of the standard file editor in WordPress. This file editor is a known weak spot for hacks. If you don’t use it, disable it with this code!

Code library

Web-ID is building a library of code snippets, with the code and explanation. You can find them under Code snippets! Have fun coding!

Do you have useful code snippets? Please leave them in the comments!

Ronald Heijnes
Ronald Heijnes

Since 2008 I keep myself busy with the functionality, management, maintenance and performance of self hosted WordPress. I like to share this knowledge. All in my spare time!

Articles: 44

Leave a Reply

Your email address will not be published. Required fields are marked *