Add PHP Code to WordPress Post or Page

0 95

There are often times when we feel a need for adding PHP codes to our WordPress pages or posts. But the thing about WordPress is that it does not let us include any external PHP codes. However, WordPress allows us to run HTML codes and CSS codes. But when it comes to PHP, it does not allow us to do so.

So the question is how to Add PHP Code to WordPress Post or Page? Well if you have this question in your head. Then you better stick with this page. In this article, we will talk about complete steps that you need to follow to add PHP codes to your WordPress posts and pages.

Anyway, let just head into the topic without wasting much of the time:

How to Add PHP Code to WordPress Post or Page:

Well in order to add PHP code to WordPress post or page. We have to use a Plugin. Talking about the plugins, well there are already quite a lot of plugins are available online. But we will be using the PHP code snippets (Insert PHP) By Will Bontrager Software, LLC, Webcraftic.

Now you might be thinking what this plugin is about and how it can help you out? Well, let us clear the questions one by one.

What is PHP code snippets (Insert PHP)?

php code intro.jpg

As you can see in the name, it says Insert PHP, so you already have an idea about what this plugin is about. Using this plugin, you will be able to add PHP code snippets in admin area without editing your function.php file. In addition to that, you can use PHP snippets shortcodes in posts, pages and widgets with ease.

It is the newest version of the famous plugin called Insert PHP. Back in the days the Insert PHP plugin used to execute PHP codes insider the [insert_php][/insert_php] tags on WordPress pages and posts.

However, after the latest updates, the plugin now allows you to add special snippets insider your pages and posts. Plus the plugin also supports the old php insert method tags.

Talking about Snippet, well it is a small php code that helps in extending your WordPress capabilities. Plus it has a very friendly user friendly user interface which helps you to place and executes the PHP codes in an easy way. All you have to do is add your PHP code to the code editor and then save the snippet. After that add the snippet code to anywhere of your WordPress using a shortcode. And it will function from there.

However to make you understand in a better way, here is an example of using shortcodes:

if( is_user_logged_in() ) {

echo ‘Download file‘;

} else {

echo “You must be logged in to download the file.”;

}

Example of executing code for the whole website (global):

remove_action(‘wp_head’, ‘wp_generator’);

remove_action(‘wp_head’, ‘rsd_link’);

remove_action( ‘wp_head’, ‘wlwmanifest_link’);

remove_action( ‘wp_head’, ‘wp_shortlink_wp_head’);

Overall the plugin helps you to use the PHP code snippets in Admin panel, without making changes in the function.php. Although, there are ways to add PHP codes to your website by editing the function.php file. But that is a confusing process.

But using this plugin, you will be able to add the PHP code to your WordPress posts and pages by using the “[insert_php]” and “[/insert_php]” tags. For example, you have to follow this pattern to run the PHP codes:

[insert_php]
// Add your PHP Code Over Here //
[/insert_php]

Talking about how this plugin works, well whenever a user loads a web page. The PHP code gets compiled instantly and publishes the output directly on the webpage between the special tags. For example, you can use the PHP code snippets (Insert PHP) for these following tasks:

Publish local time (users’ computer clock settings being unreliable).

Insert output of a PHP script, or just to run a script whether or not it generates output.

Check/manipulate cookies or other actions that JavaScript can accomplish when using JavaScript is undesirable.]

So that was an introduction to the PHP code snippets (Insert PHP) plugin. Now let’s move to our main question which is the How to Add PHP Code to WordPress Post or Page. So go ahead and follow these steps:

Add PHP Code to WordPress Post or Page?

Step 1: First of all login to your WordPress Dashboard.

Step 2: From your WordPress dashboard select Plugins and then click on Add New.

Step 3: Now that you are on Plugins page, over here you have to search for the PHP code snippets (Insert PHP) By Will Bontrager Software, LLC, Webcraftic. (Beware of the alternative ones)

Step 4: After that install and activate the plugin.

Step 5: Now from the sidebar click on the PHP Snippets and then go to the + Add Snippet option.

Step 6: Then enter the PHP code that you want to display. For example, we have entered the Hello World PHP code. Then you have to select the “Where there is a shortcode” option.

code.jpg

Step 8: Now save it.

Step 9: Now simply copy the Snippet ID.

Step 10: Now go to your desired page or blog post and paste the code over there. Now publish the page or post, and you are all done.

code paste.jpg

display.jpg

Final Words:

So that was all for your question which says How to Add PHP Code to WordPress Post or Page. Now go ahead and try out the plugin and see how it is working for you.

However, if you face difficulties while getting started with the plugin. Then you may try to use some other plugins as well. Some of the PHP code snippets (Insert PHP) alternatives are the Insert PHP Code Snippet, Code Snippet.

However, if you ask what is the easiest to use one. Then we would still ask you to go with the PHP code snippets (Insert PHP) plugin. The plugin is free and does the job with ease.

Anway, if you have any more questions, then do comment below, and we will surely help you out.

Comments
Loading...