Using custom PHP snippets in WordPress

Tickera
8 min readFeb 28, 2023

If there is one thing WordPress should owe to its popularity, it would certainly be its flexibility. WordPress as a platform is full of hooks that make it easy to modify it in any way imaginable.

Hooks and filters are an essential part of the WordPress development framework. They provide developers with a way to modify the behavior of WordPress core code or plugins/themes without editing the original code directly. Hooks are points in the WordPress code where developers can add their custom code to modify the default behavior. WordPress provides two types of hooks: actions and filters.

What’s more, this approach is widely adopted by developers of third-party plugins and themes who more often than not implement useful actions and filters all across their plugins and themes. All of that allows website owners to add custom functionality to their sites through the use of PHP snippets.

These snippets can be used to modify or extend the functionality of existing WordPress plugins and themes, or they can be used to create entirely new functionality that is specific to the needs of the site owner. For example, if you’re using Tickera WordPress Event Ticketing System, you can modify its functionality in many ways using snippets such as the ones found here.

Photo by Ben Griffiths on Unsplash

There are several methods for adding custom PHP snippets to WordPress websites, each with its own advantages and disadvantages. In this post, we will explore some of the most popular methods and discuss when they are appropriate to use.

Method 1: Functions.php File

One of the oldest methods for adding custom PHP snippets to a WordPress website is by editing the functions.php file. This file is located in the theme folder of the WordPress installation and is responsible for loading the functions that define the theme's behavior. However, this same file can be used to modify any other active plugin on your website and is not limited to modifying only that particular theme’s functionality.

So, to add a custom PHP snippet to the functions.php file, you should download it to your computer (via FTP client for instance), open the file in a text / code editor of your choice, and add the code to the bottom of the file and then save changes. When done, you should upload it back to your website by overwriting the original functions.php file

How do I do this?

For example, let’s say that you’re using one of WordPress’ default themes called Twenty Twenty-Three. Files of this theme will be located within /wp-content/themes/twentytwentythree/ folder. Now, by default, this particular theme does not contain functions.php file which might be the case with some other themes as well. So, if the theme you’re using does not contain functions.php file, you should create a new file within the root folder of the theme that you’re currently using and name it functions.php. Of course, if the theme does contain functions.php file, then we will be using that existing one.

So, you have either created a new functions.php file or downloaded the existing one from the theme folder of your website. The next step is to edit this file. Previously we have mentioned that you should use text / code editor of your choice. However, it is important to know that these files must be edited with text editors that support plain text editing or use software specifically designed for editing code. There are lots of free ones available on the market: NetBeans, Atom, Notepad++, and similar. If you don’t want to use any third-party software, you can get away by using Notepad on Windows or TextEdit on Mac (but make sure that you have selected the Format -> Make Plain Text option).

Whichever approach you decide to make, adding PHP snippets will remain the same and will be in most cases just a matter of copy/pasting and once you’re done adding a snippet, you should save the changes and upload functions.php file to the root of the theme that is currently active on your website.

NOTE: if you will be using this method to add PHP snippets to your website and will be using your theme’s existing functions.php file, before you do anything, you should download a copy of this file which you will leave intact so that you have a backup if anything goes wrong.

Advantages:

  • Easy to implement.
  • No need for additional plugins or tools.
  • Code is loaded directly into the theme, making it easy to maintain.

Disadvantages:

  • If not done properly, the code can break the website.
  • Can be difficult to manage if multiple customizations are needed.
  • Changes made to the functions.php file can be lost during theme updates or if the theme is changed.

Method 2: Child Theme

A child theme is a WordPress theme that inherits all of the functionality and design of its parent theme but allows website owners to modify the theme without affecting the original code. This is accomplished by creating a new theme folder that contains only the files that need to be modified.

Photo by Lavi Perchik on Unsplash

How do I do this?

Creating a child theme in WordPress is consisted of few steps.

  1. First, we will need a folder that will store the files of the child theme so you should create a new folder within /wp-content/themes/ folder.
  2. Now, the important part and what will make this folder get recognized as a child theme is its name. So, if you’re using a theme that is originally stored in /wp-content/themes/twentytwentythree/ folder, your child theme should be stored in the folder that bears the same name as the original theme with the addition of -child. In this example, when Twenty Twenty-three theme is used, the child theme should be named twentytwentythree-child.
  3. Once you have created a folder, you will have to create a new file in this folder named style.css. This file is responsible for theme styling which, although we won’t need for adding custom PHP snippets, it has to be present in the child theme’s folder as it will contain valuable information in the header that defines it as a theme. So, once you have created style.css file in the folder of your child theme, you should add the following header to it (of course, change the values according to your parent
/*
Theme Name: Twenty Twenty Three Child
Theme URI: http://example.com/twenty-twentythree-child/
Description: Twenty Twentythree Child Theme
Author: John Doe
Author URI: http://example.com
Template: twentytwentythree
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
Text Domain: twentytwentythreechild
*/

If you have done everything properly and style.css file contains the proper header that defines this as a child theme of your theme, you should be able to see this child theme in the Appearance -> Themes area at the back end of your website. If that is the case, simply activate it. The styling of your website will still be used by the main theme and this child theme will serve us only as a carrier of functions.php file that will contain our custom PHP snippets.

4. With that said, the next step is obviously creating a functions.php file inside the child theme’s folder. From now on, the process of adding snippets is pretty much identical to adding snippets to theme’s functions.php file but instead of adding snippets to the original theme’s fuctions.php file, you will be adding them to the child theme’s functions.php file.

Advantages:

  • Changes made to the child theme do not affect the parent theme.
  • Code is loaded directly into the theme, making it easy to maintain.

Disadvantages:

  • Requires knowledge of theme development.
  • Can be time-consuming to create and manage.
  • Changes made to the child theme may be lost during theme updates.

Method 3: Plugin

Another popular method for adding custom PHP snippets to WordPress is by using a plugin. There are many plugins available that allow website owners to add custom code snippets to their sites without the need for programming knowledge.

One of the most popular plugins for this purpose is Code Snippets, which allows website owners to add custom PHP code snippets to their WordPress site without editing any files. What’s more, it will prevent any custom code you add to this plugin from executing if it causes fatal errors which is an excellent safety net if you’re not entirely sure whether the snippet you want to use will work as expected.

Advantages:

  • Easy to implement and manage.
  • No need for programming knowledge.
  • Changes are not lost during theme updates.
  • Prevents execution of the code if it causes fatal errors and therefore prevents breaking of your website.

Disadvantages:

  • None… really!

Method 4: Custom Plugin

For more complex customizations, it may be necessary to create a custom plugin. This is a standalone PHP file that can be uploaded to the WordPress installation and activated like any other plugin.

How do I do this?

To create a custom plugin, you should first create a folder that will contain your custom plugin files inside /wp-content/plugins/ folder. You can name this folder any way you like since it does not play any significant role (unlike the child theme folder name). Once you have created a folder, you should create a new PHP file in it which we’ll be using for our PHP snippets. The easiest approach is to simply name this file index.php

Once you have created index.php file, you should edit it using your preferred text editor and first add a header that will make WordPress recognize this as a WordPress plugin. Typically, the header in your index.php file should contain something like this:

/*
Plugin Name: My Custom Plugin
Plugin URI: https://example.com/
Description: Add custom php snippets
Version: 1.0.0
Author: John Doe
*/

If you have entered a header like this, you should now be able to see this plugin within the Plugins area of the back end of your website. Go on and activate this plugin. At the moment, as we don’t have any code added to this plugin, it does nothing. However, as soon as you add some snippets below this header, it will start executing the code snippets you have added to it.

Advantages:

  • Allows for complex customizations.
  • Changes made to the custom plugin are not lost during updates to WordPress, other plugins or themes.

Disadvantages:

  • Requires programming knowledge.
  • Can be time-consuming to create and manage.
  • May not be necessary for simple customizations.

Conclusion

Adding custom PHP snippets to a WordPress website can be a powerful way to extend the functionality of the site and meet the specific needs of the website owner. The way you do it depends entirely on what you’re comfortable with and are used to working. However, the general rule of thumb would be to use Code Snippets plugin for smaller modifications and custom plugins for larger ones. Using a child theme is somewhat acceptable if you’re sure that you won’t be changing the theme any time soon (as changing the theme will make all these customizations to stop working). Editing functions.php file of the active theme on your website is the least reliable and thus least recommended way given that any modifications you do will be reset with each theme update or if you change the theme.

--

--

Tickera

Tickera.com is a top-tier event ticketing system designed for WordPress. Our team helps event organizers and businesses streamline their ticketing process.