
Sometimes the only way to add the functionality you need to a WordPress website is to edit the site’s function.php file. I’ve seen thousands of WordPress how-to articles throughout my career as a web developer and so many of them say to add this code to the site’s functions.php file or header.php file and you’re good to go. Sounds easy right?
Technically, yes, it is easy. It’s also an incredibly dangerous, and possibly even stupid thing to do because:
- Any WordPress or theme update can completely wipe-out all of your changes, forcing you to edit the same files after each and every update.
- If you make a mistake in editing the functions.php you can summon the dreaded white screen of death. In this case your screwed unless you have FTP access and can undo the changes you made. And do not EVER, EVER, EVER edit your website’s files using WordPress’s built-in code editor. In fact, you should disable that abomination immediately after installing WordPress.
- You can create a child theme and make any necessary edits to the functions.php file, but this is a pain too, and mostly unnecessary in my opinion.
As counterintuitive as it sounds, the absolute easiest way to to insert custom code into a WordPress website is to create your own custom WordPress plugin. Sure “custom plugin” sounds complicated, but it’s actually super simple to accomplish and only takes a few minutes of work.
Using the code below as a template, just insert your custom PHP code where it says to, save the .php file and install it into your WordPress website.
Don’t forget to fill in the blanks, inserting your name, your name and a short description of what the plugin does (replace the square brackets too or they will appear in your Installed Plugins page). This obviously isn’t vital, but good practice to ensure the functionality of each plugin is well understood by anyone else working in the WordPress admin area.
<?php /** * Plugin Name: [Insert Name] * Author: [Insert Your Name] * Description: [Insert Short Description] * Version: 1.0 * Text Domain: plugin-name */ //Add your custom code below this line ?>
Once you install and activate your plugin it will appear as any other WordPress plugin in your Installed Plugins list. And if it gives you trouble you can deactivate it easily.
This technique is especially great if you have a standard chunk of php code that you use for all your WordPress websites – just create a plugin and install it anywhere it’s needed – you’ll never have to edit the functions.php file again!
Note: If you get the following error message when installing your custom WordPress plugin through the WordPress’ Upload Plugin interface, don’t worry, you didn’t do anything wrong!
The package could not be installed. PCLZIP_ERR_BAD_FORMAT (-10) : Unable to find End of Central Dir Record signature
Just .zip your plugin first before attempting to install it – the WordPress’ Upload Plugin interface requires all uploads to be zipped or it is unable to process them.
Click Here To Download Our Sample Plugin