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
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.
2. Create and save PHP code snippets
Once the plugin is installed, on the Left Menu, Click Add Snippets under Woody snippets.
On the page that opens Click Create Item under PHP snippet
Give it a name and then paste or write your PHP code in the code editor.
3. Next, set where the code is going to execute
- Run Everywhere – the PHP code is inserted and run site-wide – that is – the code runs on all your pages and posts of the website. Typical usage for this is if you are running some analytics or tracking code. If you are using Google analytics of Google Adsense, you paste the code you get from Google or any other source here.
- Where there is a shortcode– with this option set, your code is going to only execute on the page or post that you add your shortcode.
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.
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.
Next, you can publish to save.
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.
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.
Publish to save your changes.
Our front end should now execute the PHP script when someone goes to that post or page.
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.
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
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.
You may also want to see our picks for the top 5 WordPress plugins for inserting code to your page headers and footers