Insert PHP Code Snippet Without WordPress Child Theme or Editing functions.php File.
During WordPress customization, we often need to insert different types of code snippets to our theme. It is a simple task if these Code snippets are written in HTML, CSS or JavaScript, as almost all popular themes and leading page builders provide the feature to write these user-side languages.
Normally, in almost every theme, there is an area where you can insert HTML, CSS and JavaScript code. Page Builders go even further, where you can write user-side code directly in a text editor or by using an Element or Widget for this purpose.
However, It becomes a bit complicated when it comes to writing a PHP code snippet. The most common way to achieve this is the use of a WordPress Child Theme, where developers insert PHP code inside an instance of functions.php file.
Alternatively, there are some free and premium plugins available to create and organize PHP code snippets. However, I have never been a big fan of either of these approaches.
An easy Method to Insert PHP Code Snippet
Luckily, there is another very simple, safe, and efficient method for writing PHP code. Years back when I started learning WordPress plugin development, this idea came into my mind that we can create a simple WordPress plugin where we can write all of our custom functions and code snippets.
So let’s see how we can create this simple custom plugin:
Basic Structure of Plugin File
It sounds a bit scary when I say we are going to write a custom plugin but believe me it’s simpler then creating a child theme or installing some third party plugin. Our plugins will consist of one file and will few lines of text in it.
Creating Text Document
First we need a text document where we can right our plugin code. It can be a simple text editor like text document or some more advance code editor such as Notepad++, Sublime Text and UltraEdit
To make things simple and demonstrate possibilities I am using a simple Text Document, which is built-in into every Windows operating system.
Create a folder inside any of your computer drive, right-click and choose New and Text Document from the drop-down menu.
Insert Plugin Code
A WordPress plugin file is written in PHP. So we need to insert some code that makes a plugin file recognizable to WordPress.
You can copy the complete code from WordPress Codex Site. However, I am using the minimum code required.
Paste the following code inside newly created text document and edit it to make it your own.
<?php
/**
* Plugin Name: Simple PHP Code
* Description: Simple plugin to insert custom PHP functions.
* Version: 1.0
* Author: Ali Imran
*/A Plugin File Extension
WordPress code is written in PHP so it's plugins. We need to convert this text document into PHP file for this:
Choose Save As from the File menu of Text Document and give a name to your plugin file. The name of the plugin should be without spaces and preferably in lower case.
Most importantly, you need to change the file extension to .php instead of .txt and hit the Save button.
Compress A Plugin File
To install a plugin through the WordPress installer, you need to compress your file with a .zip file extension.
Though there is another way to install a plugin where you can copy your plugin file to your plugin directory under wp-content/plugins folder, but i prefer to install plugins through the installer. As with this method your plugin go through wordpress standard testing process.
For compressing a plugin file right click on your plugin file and choose Sent to and then Compressed (zipped) folder.
And that's it your Simple WordPress plugin is ready to Install like any standard WordPress plugin.
Install and Activate Your Plugin
The next step is pretty standard and straight forward. Just login to your WordPress dashboard, go to Plugins in the left menu browse for your newly created plugin file and press install button.
When it finishes plugin installation, press Activate to activate the plugin.
Edit and Write PHP Code
So far, we have created our own custom plugin and installed it successfully into our WordPress. However, this plugin doesn't contain any PHP functions to perform a specific task. Now wThe next step is pretty standard and straight forward. Just login to your WordPress dashboard, go to Plugins in the left menu browse for your newly created plugin file and press install button.
Just go to Plugins menu on left sidebar and select Plugin File Editor from the sub-menu. Here, you can edit your plugin's PHP code. From the top-right drop-down menu, select your newly installed plugin and press Select. It will open your plugin file in a text editor.
Complete Example Code
<?php
/**
* Plugin Name: Simple PHP Code
* Description: Simple plugin to insert custom PHP functions.
* Version: 1.0
* Author: Ali Imran
*/
// PHP Code
function bartag_func( $atts ) {
$a = shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts );
return "foo = {$a['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );Why It is My Preferred Method
Creating a complete child theme or installing some heavy plugin to just insert a simple PHP function seems a bit overwhelming. So I found this method simple, and for a long time I've been using this method for almost all of my projects. There are plenty of reasons behind my preference
Final Thoughts
If you've been using a child theme and functions.php to insert PHP code snippets to your websites up till now, I would highly recommend that you create your own simple plugin for this, as it's a quick, reliable, and more efficient way to write PHP functions.
You can follow the step-by-step process in this article and develop your own plugin, or simply download a ready-to-install example plugin and start inserting PHP code into your website without hassle.

















