Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Allow PHP in WordPress Posts and Pages

Do you want to allow PHP in your WordPress posts and pages?

By adding custom code to your site, you can extend WordPress to better suit your needs. However WordPress does not let you add PHP directly to your pages by default.

In this article, we will show you how to allow custom PHP in WordPress posts and pages by using a free plugin.

How to allow PHP in WordPress posts and pages

How to Allow PHP in WordPress Posts and Pages

If you try typing PHP code directly into the WordPress block editor, then you’ll notice that WordPress strips away a lot of your code, or even deletes it entirely.

This is because WordPress doesn’t allow PHP in posts and pages by default for security reasons. A simple mistake in your PHP code can also cause all sorts of WordPress errors on your website.

Luckily, there is a simple workaround.

WordPress may not allow PHP in posts and pages, but it does allow shortcode. This means that you can add custom PHP to your content by creating a shortcode.

First, you’ll need to install and activate Insert PHP Code Snippet. If you need help, then please see our guide on how to install a WordPress plugin.

Upon activation, go to XYZ PHP Code » PHPCode Snippets. To add your first snippet, click on the ‘Add New PHP Code Snippet’ button.

Add new PHP code snippet

Now simply type a name for your snippet into the ‘Tracking Name’ field.

Your site visitors won’t see this name since it’s for your own reference only.

Adding a name to your WordPress PHP snippet

In the ‘PHP code’ field, either type in or paste the PHP code that you want to use.

In the following image, we’re creating a snippet that will show the post’s publication date.

Writing custom PHP code for WordPress

You can click on the ‘Preview’ icon on the far right under the Action column to check if the PHP code is working properly.

This will show how your snippet will look on your WordPress website.

Previewing a WordPress snippet in a post or page

Once you’re happy with how your snippet looks, click on the ‘Create’ button.

The plugin will go ahead and create a shortcode for this snippet.

Adding PHP to a page or post using shortcode

You can now add this custom PHP to any page or post using the shortcode.

To get started, simply copy the shortcode. You can then open up the post or page where you want to run your PHP code.

The final step is pasting this shortcode into a new Shortcode block or widget area. For step by step instructions, see our beginner’s guide on how to add a shortcode in WordPress.

How To Manage Your PHP Shortcodes

You can also use this plugin to manage all your PHP snippets.

At some point you may need to edit the snippets you’ve already created. With this plugin, you just need to make these changes once. The updated PHP code will then be used across your WordPress blog or website.

This is easier than going through each page and post and editing the PHP snippet manually.

To edit a snippet, simply go to XYZ PHP Code » PHPCode Snippets. Here, find the snippet that you want to change.

You can then go ahead and click on its ‘Edit’ icon.

Editing your WordPress custom PHP code

Once you’ve done that, you can make your changes to the snippet.

Don’t forget to click on the ‘Update’ button to save your changes when you’re done.

The Update bottom in the Insert PHP Code Snippet plugin

You can also deactivate any PHP snippet, which will stop the code from running on your website. You don’t even need to delete the snippet’s shortcode from your pages and posts.

To disable a PHP snippet, simply click on its ‘Deactivate’ button, which looks like a pause sign under the Action column.

Deactivating a custom PHP snippet

WordPress will now show an ‘Inactive’ label for this snippet.

To reactivate this PHP code, simply follow the same process described above. This will turn the ‘Inactive’ label into an ‘Active’ label.

We hope this article helped you learn how to allow PHP in your WordPress posts and pages. You can also go through our guide on how to create automated workflows in WordPress, and our top tips to boost WordPress speed and performance.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

5 CommentsLeave a Reply

  1. Put something like this in your template to create a javascript var to store the path, then use javascript to write it in your content?

    (removed some chars so this will show up –

    var templateDir = “<php bloginfo(‘template_directory’)>”;

Leave a Reply to fastasleep Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.