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