How To Add Custom Code To A WordPress Website

Here are four different ways that you can use to add custom functions or filters or actions to your WordPress site. Basically, these are four ways to add any custom PHP code to your WordPress site. Choose your favorite one:

Theme function.php

Add the code to your theme’s functions.php file. This method is NOT recommended because your custom code will be erased when you update your theme. If you are never going to update the theme, then this method if for you.

Child theme function.php

Add the code to your child theme’s functions.php file, if you’re using a child theme. This is the preferred method for many people. You can paste the code at the end of the file, below any exising code.

NOTE: If the file has a “closing PHP tag,” which looks like this: ?>, then be sure to place your code ABOVE that line. Just create a new line above the closing PHP tag and insert your code there. However, many functions.php do not have a closing PHP tag, so you may then simply place your code at the end of the file.)

Another NOTE: There is one drawback for using this way to paste custom code. If you ever decide to change your child theme, you’ll lose your code, or you’ll manually have to copy the code over to your new theme. The third and fourth way, listed below, are better ways because they keep your custom code intact regardless of any theme that you use.

Use a plugin

This third way is the easiest way to add custom code. Use the Code Snippets plugin and add your custom code there. First install the Code Snippets plugin:

  • Go to your WordPress dashboard –> Plugins –> Add New.
  • Search for “Code Snippets”.
  • Click “Install Now” under “Code Snippets”.
  • Click “Activate” to activate the plugin.

Now, you can add custom code any time, like this:

  • Go to your WordPress dashboard –> Snippets –> Add New.
  • Enter a title for the code snippet.
  • Paste your code.
  • Click “Save Changes” at the bottom

Create your own plugin

This fourth way is for more advanced users. This gives you the most control while having a lighter footprint than the above method. Make a custom plugin to hold all of your custom functions. To do this, use a text editor and create a new blank file. Paste this at the very top (you can change “Me” to your own name on line 3):

<?php
/*
Plugin Name: Custom Functions by Me
Version:     1.0
*/

Now, you can place your custom code below this. Save the file and name it custom-functions-by-me.php, or change the “me” to your own name. The important thing is for it to end with .php. This file is your new custom plugin. Upload this file to your server, into your plugins folder which is at yoursite/wp-content/plugins/

An alternate way of uploading this file is this: on your computer, compress/zip the file so that it becomes a .zip file. Then, you can simply upload it through your WordPress dashboard –> Plugins –> Add New –> Upload Plugin. Click “Browse” to find and upload the .zip file, then click “Install Now.”

Whichever way you upload (or install) the file to your server, you’ll still have to go to the WordPress dashboard –> Plugins page to activate the plugin by clicking “Activate” under the “Custom Functions by Me” title.

Your custom code will then be active on your site. You can always add more custom code to that file.

Conclusion

Whichever of the above four methods you choose, now you know how to add custom functions, actions, filters, or just about any PHP code to your WordPress website.

You may also like...