Natively WordPress does not execute PHP code inserted on pages or posts. If you need to execute a script, you have to find a way to insert PHP code into your WordPress website.

One will typically want to do this if they have custom code that needs to execute when a visitor opens a page or post or when they click something on that page.

So how would one accomplish that?

One option is to create a shortcode with the WordPress Shortcode API then insert it into any page or post the usual way you would insert a [shortcode]. Of course, this requires some knowledge of the PHP language.

One could also create a template inserting the PHP code into it.

For a developer, any of these routes would viable and relatively easy.

How about those web admins who need to insert PHP code into their pages or posts but have no faint idea how to interpret more-so write a single line of PHP code, or a knowledgeable person who does not want to mess around with the code.

Like always, the solution for such an individual is in a WordPress plugin.

You will today learn how to insert PHP code to a WordPress website with Woody ad snippets.

Step by Step: How to insert PHP code to a WordPress website with Woody AD Snippets plugin

Woody ad snippets plugin for wordpress

1. Install the plugin

  • Access to your WordPress administrative area. Navigate to Plugins, Add New. In the search box enter Woody as snippets.
  • Click Install and then Activate.

install Woody ad snippets plugin

2. Create and save PHP code snippets

Once the plugin is installed, on the Left Menu, Click Add Snippets under Woody snippets.

woody ad snippets plugin menu

On the page that opens Click Create Item under PHP snippet

add php snippet

Give it a name and then paste or write your PHP code in the code editor.

paste php code

3. Next, set where the code is going to execute

set where to execute the code

Now let’s put this to the test.

4. Insert PHP Code snippets to your WordPress website

Assuming we want to show our visitors the current time and date for whatever purpose,

We would need to insert this simple PHP script into the page or post we want to display the date and time.

<?php

echo "The date today is : " . "\n";

echo date("M,d,Y h:i:s A") . "\n";

?>

So we go back to the above step and insert the code.

insert the code to display the date

Set the base option of where to execute the code to – Where there is a shortcode. Note the option chosen is not that defined and highlighted – be sure to click it twice or thrice to be certain.

In the description text area, enter something relevant, something you can remember. A name someone else can quickly identify and relate with the purpose of the code.

choose the option of where there is a shortcode

Next, you can publish to save.

publish and save the snippet

The road to any third party code that is to run on the whole site like Google Analytics or Google Adsense ends here. You do not need to do anything else.

However, if we want to insert PHP code to a particular page or post, we shall continue.

We then go to that page and insert PHP code to it with the newly created shortcode.

Next, open or create the post or page you want to insert PHP code into.

add snippet to a page or post

Using the Gutenberg editor – click the + Add Blocks icon.

Start typing woody then Click Woody snippets.

From the drop-down menu, select the code you want to insert if you have several snippets created.

select the code you want to insert

Publish to save your changes.

Our front end should now execute the PHP script when someone goes to that post or page.

PHP code is now executed and working on front end

That is the simplest and safest way you can Insert PHP code to a post or page without trying to hack WordPress or your theme files.

Want to try out other plugins? Then you can consider these.

Insert PHP Code Snippet

Code Snippets

If all you want to insert to your website is your Google Adsense or analytics or any other code that you set and forget then you could use;

WordPress Ad Manager & AdSense Ads – Ad Inserter

In Conclusion

You should have learned a way to insert PHP Code snippets to a WordPress website with Woody Ad Snippets and also know a couple more alternative plugins you could use.

If you are using one of the new modern and pricey themes, before you consider using a WordPress plugin, check with your theme documentation or theme author if there is a possibility of inserting PHP code using the theme.

Most new themes can allow you to do that. The best place to check would be the theme options for sitewide usage like tracking PHP code or with a widget to insert PHP code to a single page or Post.

If you found this useful, it is okay to share with your friends and don’t forget to leave your comments and thoughts in the comment section below.

You may also want to see our picks for the top 5 WordPress plugins for inserting code to your page headers and footers