Web & Software Developer

3 Ways To Add Custom Code To Your WordPress Website

In my WordPress help articles and documentation, you’ll find some articles that have custom code with instructions to “add this code to your functions file,” or “add this filter,” or “add this action,” or something like that.

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

  1. 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 does work.
  2. 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 existing 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 way, listed below, is the better way because it keeps your custom code intact regardless of any theme that you use.

  3. This third 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 “MySite” to your own site’s name on line 3):

    <?php
    /*
    Plugin Name: Custom Functions For MySite
    Version:     1.0
    */
    

    Now, you can place your custom code below this. Save the file and name it custom-functions-mysite.php, or change the “mysite” to your own site’s 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/. You can upload using FileZilla or sometimes directly through your hosting panel.

    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 For MySite” title.

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

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

By

We've 2 Responses

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]