How to add PHP code to your WordPress site without modifying the source code

Sometimes WordPress website owners and developers may need to add specific PHP codes to the website pages to get some additional features and characteristics that the WordPress template used by default does not provide. 

By default, WordPress prevents modifying or running any code that is placed directly within the various pages or links of the site. Therefore, we cannot add PHP codes directly within the website pages. Here the following question may come to our minds: Is there a way through which you can benefit from PHP codes in order to add some unique programming commands to your site?

Well, yes, there is a way, and in this article we will explain to you the correct and practical ways to add PHP codes within the pages and articles of your WordPress site to add some of the features and modifications that you want to implement within your site.

We will also show you some practical examples of useful PHP codes that you can use to change the look, appearance, and functionality of some parts within your site.

The general formula for writing PHP code correctly

The PHP language does not differ much from other programming languages. It has a general formula that must be adhered to when writing any code. Failure to adhere to that formula may cause programming errors, and the codes will not work unless they are written according to the correct general formula in which PHP codes are written. 

Here is the general and basic formula for writing PHP code, whether within WordPress or on any other site:

<? php \\ Here the codes are placed ?>

As we note, the general formula for writing language codes begins with the markup (<?php) and ends with the markup (?>), and between them, PHP language code is written. Of course, each code has a special function, and there are many codes that add many modifications that can be created using the programming functions available in this important programming language.


Ways to add PHP code to your website pages

As we mentioned before, WordPress, with its default settings, does not allow adding PHP codes directly into the content of articles or pages, so we will explain below the correct ways to add PHP codes within any page within your WordPress site in a correct manner and without causing any programming problems on the site. 

It is worth saying here that adding PHP codes is completely different from adding CSS codes to a WordPress site . WordPress allows adding CSS codes directly because it merely makes modifications to the appearance of the site, unlike the PHP language, which mainly affects the functionality of the site in most cases.

In the following paragraphs, we will review two different methods for adding PHP codes. The first will be through using one of the WordPress plugins, and the second method is the manual method, in which we will resort to modifying the code files on the site.

1. Use a plugin to include PHP codes

There are many WordPress plugins that embed and run PHP code within the pages and posts of your WordPress site. Before we review one of the most prominent of these additions, we must delve a little into how these additions work. 

All plugins that are used to embed PHP code rely on converting that code; Which are often long, into short codes . These short codes can be inserted within the site without any blocking by WordPress. They can also be inserted within the pages of the site.

The result is that you will be able to run PHP codes by converting them and linking them to the short code within the relevant page easily and without any problems. The add-on that you will choose for the task of inserting PHP codes is the Code Snippets add-on.

This plugin is one of the most popular plugins used to embed external PHP codes into WordPress website pages and articles easily and safely. 

First, we will need to install the add-on on your site, by entering the WordPress control panel, then “Add-ons,” then “Add New.” You type the name of the add-on in the search for add-ons box and it will appear for you. Then all you have to do is download the add-on and install it.

Then, after installing and activating the plugin, its settings tab will appear within the WordPress control panel tools. You can enter these settings option to start adding the code that you want to implement within the pages and articles of the site.

As you can see in the previous image, when you enter the settings page for the add-on, two fields will appear in front of you as follows: 

  1. In the first field, we will write a symbolic name for the code that we will include. 
  2. In the second field, we will write code in the PHP language, in which you must adhere to the general formula for writing language code, as we explained at the beginning of the article. 

Let’s take a practical example now, we will use a simple PHP code that prints the current day’s date.

As you can see, we wrote the following simple code in the PHP code field: 

<?php
echo “Today’s date is: ” . date ( “Y/m/d” ) ;
?>

In this particular code, we relied on the PHP language function used to print the date (Date), and we printed a text sentence “Today’s date is.” When this function is activated, the current day’s date will be printed within the page or article on which the code will be run. 

After adding the required code to the add-on page, the next step will be to enter the add-on settings page again. A window will appear that includes the names of the added codes. In our example, we have the “Date” code that we added, and next to it you will find the short code, as in the picture. If you insert this short code into one of the articles or pages of the website, the code will be applied directly. The Date function used in the code will print the current day’s date. 

We will now copy the short code shown in front of you in the image above and paste it into the editing page of one of the site’s articles, as in the image:

As the image above shows, we pasted the short code for the code, which will activate the feature of displaying the current date on the example article page. Now we will publish this article and open it in a browser to see the fruits of our previous steps: 

As you can see in the image above, the code has been run and the current date has successfully appeared on the page. 

With the same mechanism, you can include any code written in PHP in order to add the modifications you want to the pages of your site, and copy the short code that the add-on gives you after inserting the code into it. Then, when you paste the short code into one of the articles or pages of the site, it will be applied correctly without any problems.

2. Include PHP codes manually

In the previous paragraphs, we explained the easiest way to include PHP codes within your WordPress site, which is by using one of the plugins. We will now move to the manual option that many people prefer, where we will explain how to manually include php codes within your site without the need to install any auxiliary plugins. 

We reiterate a fundamental matter, which is that modifying code requires sufficient experience and knowledge from you, and we do not advise you to carry out this procedure if you do not have sufficient skill to modify WordPress code files. We also advise you to create a secondary child theme from the current template so that the added codes are not deleted when you update the template if any problem arises. 

It should be noted that the codes that you will include within the WordPress code file will be deleted if you change the template. As for the first method in which we used one of the WordPress plugins, the code will not be deleted even if you change the site’s template in the future, of course, as long as the plugin is still present and effective in your location. 

To add PHP codes to your site manually, you need to go to the WordPress control panel, then the “Appearance” option, then “Edit Templates”, and finally open the functions.php file and put the required code in it, as in the picture.

As the image above shows, we have added the following code: 

echo ‘<script>alert(“hi”)</script>’;

This code is in the PHP language, and it displays a welcome box to the visitor containing the phrase “Hello” when the visitor enters directly to the site’s pages. 

Note : Did you notice that in this method we added the code without adhering to the general syntax of the PHP language? 

The answer is that the functions.php file to which we added the code is a file originally written in the PHP language and contains the PHP syntax by default, so there was no need to write the basic syntax of the PHP language before writing the code directly, in other words we can write the PHP codes Directly as we saw in our example. 

After writing the code and saving the file, we will open one of the website pages to verify the results. Immediately after opening the page, we will see a welcome box containing the word “Hello” as in the image below.

In the same way, you can add any other programming codes in the PHP language to your site. You can search for specific PHP codes that perform specific modifications or functions on your site, insert them into your site, and then add the codes in the ways that we explained to you. As we mentioned, the main advantage of adding codes manually or through the plugin is the ability to add modifications and distinctive features that the current template you use on your site may not provide you with.

Ready-made templates for useful PHP codes 

In the previous parts of the article, we learned the different ways to include and activate PHP codes within the various pages and links of your site.

Of course, the article here will not be limited to explaining all the codes and all the features and features that can be implemented using the PHP language, but we will present some ready-made codes written in the PHP language that can be used to add some features or implement some useful commands within your site.

1. PHP code to change the welcome text in the WordPress control panel 

Within the WordPress dashboard is a welcome text, often in the following format: “Thank you for using WordPress.” Sometimes when you manage a large website with a large number of writers and editors, you may want to change this text and put a message or text that reflects the identity of your site or company instead of this default welcome text. 

You can change that welcome text within your WordPress dashboard to any other text you want via the php code:

function remove_footer_admin () {
echo ‘all rights reserved’;
}
add_filter(‘admin_footer_text’, ‘remove_footer_admin’);

As we mentioned in explaining the ways to add codes, you can place the code via adding a help or manually via the functions.php file to be executed and change the default welcome text, as in the picture: 

Notice after we confirmed the change and activated the code how the welcome text in the control panel changed:

You can at any time review the code and change the sentence we set “all rights reserved” to any other sentence or text you want to show. 

2. php code to set the default Avatar image for the book 

When you have a WordPress site with a staff of writers, each writer’s account avatar appears by default based on the email the writer uses. If a writer does not have an avatar on his email, WordPress sets the default avatar, which is a white avatar. 

The following code, when included within the functions.php file, enables you to change the default image of the book within your site, as shown in the image:

In the code, you can replace the link: (example.com/wp-content/uploads/2021/04/WPar-logo1.png) with any link to the image you want to set as the default avatar image for writers and members on your site. 

After saving the code, you can go to the WordPress control panel, then “Settings,” then the “Discussion” option. You will notice that the default image that you added using the code appears, as in the picture:

3. Code to hide the welcome screen in the WordPress control panel 

Do you want to hide this welcome screen that appears in front of you once you enter the control panel? Well, it’s simple, just add the following code inside the functions.php file, and immediately after saving the file, you will notice that the welcome screen disappears completely.   Note : The option to show the welcome window again will not be available unless the code is deleted.

4. php code to show the number of registered members on the site 

Adding the following code inside the functions.php file will create a “short code” that you can use to show the number of registered members on your WordPress site, as in the picture: 

After saving the file, if you add the short code [user_count] to any of the site’s articles, pages, or links, the number of registered members on the site will be shown. 

In the end, adding PHP codes within a WordPress website is not difficult, and in our article we provided a comprehensive guide on the simplest ways to accomplish this. You can now add any code to activate some features and properties that templates do not usually provide.

You can always expand your horizons of knowledge about useful code for a WordPress site and try it directly, but remember that it requires good familiarity and knowledge of code, so be sure to learn all things related to programming languages ​​and be careful when modifying code within your site, and there is no harm in making A backup copy of the site before you start modifying any of the software files within your site in case of any possible error

Avatar photo
I am a young man who has been working in WordPress and e-marketing for 10 years. I would like to share my experience with you so that we can become professional in WordPress I will be happy to share the experience with you.