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, by default, WordPress does not let you add PHP directly to your pages.

In this article, we will show you how to allow custom PHP in WordPress posts and pages.

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.

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

Luckily, there is a simple workaround.

WordPress may not allow PHP directly in posts and pages, but it does allow shortcodes. This means you can add custom PHP to your content by creating a shortcode and using it in a post or page.

The easiest way to add custom shortcodes in WordPress is by using WPCode.

The first thing you need to do is install and activate the free WPCode plugin on your website. For more details, see our step-by-step guide on how to install a WordPress plugin.

Upon activation, head over to Code Snippets » Add Snippet.

How to add a shortcode in WordPress

Here, simply hover over ‘Add Your Custom Code.’

Then, click on ‘Use snippet.’

Adding custom PHP to WordPress

To start, type in a title for the custom code snippet. This can be anything that helps you identify the snippet in the WordPress dashboard.

After that, open the ‘Code Type’ dropdown and select ‘PHP Snippet.’

How to add a custom PHP snippet to WordPress

In the code editor, 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.

Adding a custom PHP snippet using WPCode

After that, scroll to the ‘Insertion’ section. WPCode can automatically add your code to different locations, such as after every post, frontend only, or admin only.

Since we want to add the custom PHP using shortcode, click on ‘Shortcode.’ WPCode will now show the shortcode that you can add to any page, post, or widget-ready area.

Allowing PHP code in WordPress pages and posts

After that, you’re ready to scroll to the top of the screen and click on the ‘Inactive’ toggle so it changes to ‘Active.’

Finally, click on ‘Save Snippet’ to make the snippet live.

Saving custom PHP snippets in WordPress

Once you’ve done that, you’re ready to add the custom PHP to your WordPress website by opening any page or post.

In the block editor, simply click on the ‘+’ button and type in ‘Shortcode.’ When it appears, select the Shortcode block to add it to the page or post.

How to add a shortcode block to WordPress

You can now paste the shortcode into this block. For more information, please see our beginner’s guide on how to add a shortcode in WordPress.

When you’re ready to make the code live, either publish or update the page. Now, if we visit our website we’ll see the post’s publication date live.

How To Manage Your PHP Shortcodes

If you want to remove a code snippet from a page or post, then you can simply delete the Shortcode block.

However, at some point, you may want to remove a custom PHP snippet from your entire website. In this case, it’s often easier to simply deactivate the snippet as this will disable it for every page or post.

To do this, simply go to Code Snippets » Code Snippet.

Enabling and disabling custom PHP code in WordPress

Then, just find the code that you want to deactivate and click on its toggle to turn it from Enabled (Blue) to Disabled (Grey).

After that, it’s a good idea to visit your website and check that the deactivated code isn’t causing any problems.

If your WordPress blog looks and acts normally without the code, then you may want to delete the snippet completely by going to Code Snippets » Code Snippet.

Here, simply hover over the snippet that you want to delete and then click on ‘Trash’ when it shows up.

Deleting custom PHP code from WordPress

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 LS 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.