Web Development 4 min read

How to Insert Custom PHP to a WordPress Post

Written on 15 Jan 2020
Overview

WordPress will not execute PHP inside of a WordPress post. In order to do this, we’ll either need to use a plugin that allows you to insert PHP snippets, or manually define a snippet using shortcodes.

Insert Custom PHP to a WordPressPost
There are two ways to add PHP to a WordPress post from the editor. The first, using a plugin to do it for you, is the easiest, and likely safer option. There are many plugin options out there, but we’re demonstrating with “PHP Code Snippet”. This plugin works by allowing you to define a code snippet, then insert it later via a shortcode in your actual post.

Doing it with a plugin

To start, install the Insert PHP Code Snippet plugin, then click activate.
step 1
Then, navigate to the PHP Code Snippets section under the plugin’s menu.
step 2
Click “Add New PHP Code Snippet”.
Add New PHP Code Snippet
Here, you can make your snippet. The Tracking Name is what it will show up as in the editor, and what you can use to call it manually. It does not support spaces, but if you insert a space it will replace it automatically.
Add New PHP Code Snippet
Simply add the code you want in this snippet in the text box, then click create.
It should be active by default, but make sure by mousing over the icon on the left side of the “actions” column.
step 5

Adding the snippet your post automatically

If you’re using the Gutenberg (Block) editor, you’ll want to first add a classic block
step 6
In this block, you’ll see a new button for PHP, select your snippet.
step 7
You’ll get something that looks like this.
step 8
If you’re one of the millions using a classic editor plugin or are on an older version of WordPress, it will be in the toolbar by default.
Adding snipped to WordPress Post
Once you’ve added your snippet, update and check the post. You should see the result of your PHP.
Hello World WordPress Post

Adding the PHP snippet your post manually

You can, of course, just type it in using the format below.
[xyz-ips snippet=”Insert Tracking Name Here”]

Adding PHP to your post with a shortcode

If you refuse to install yet another plugin, you can do what these plugins do manually with a similar result. This, of course, is not the recommended way, but you can do it.
To do this, you’ll be taking advantage of shortcodes in your theme’s functions.php file. The caveat of doing this is it will only work with this theme applied. If you do it this way, you may want to make use of a child theme, ensuring that your new functions aren’t overwritten when your theme gets an update.
Find your functions.php file in your theme’s directory, and add your shortcode to it. Refer to the link above to the WordPress Codex if you’re unsure how to make it. Here’s a simple example.

function test_func( $atts ){
    return 'This is PHP from your functions file' ;
  }
  add_shortcode( 'test', 'test_func' );
Save it, then call it in your post with
[test]
You should now see this
Working PHP in a WordPress Post
If you need any help sprucing up your WordPress site, look no further than the professional WordPress development at CodeClouds. We do custom WordPress themes, WooCommerce plugin development, and CRM integration. Contact us today!
professional WordPress development

Share this article

  • twittertwitter
43.1k reads
Contents

Similar Reads