Do you want to allow PHP in your WordPress posts and pages?
By adding custom code to your site, you can fine-tune 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.
Why Allow PHP in WordPress Posts and Pages?
If you try typing PHP code directly into the WordPress block editor, then you will notice that WordPress strips away a lot of your code or even deletes it entirely.
However, there are some cases where you may need to add PHP to WordPress posts and pages.
For example, if you make money through affiliate marketing, then you might need to add advertising code to your site.
You may also have developed your own PHP functions. For example, you can use custom PHP to validate user logins on your site’s members-only pages.
With that being said, let’s see how you can allow PHP in WordPress posts and pages.
How to Allow PHP in WordPress Posts and Pages
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 then using it in your pages and posts.
The easiest way to add custom shortcodes in WordPress is by using WPCode. This plugin allows you to create as many shortcodes as you want and then link those shortcodes to different sections of PHP code.
Upon activation, head over to Code Snippets » Add Snippet.
Here, you will see all the ready-made snippets you can add to your website. These include snippets that allow you to completely disable WordPress comments, upload files that WordPress doesn’t support by default, and more.
Since you are creating a new snippet, you need to hover over ‘Add Your Custom Code.’ Then, click on ‘Use snippet.’
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.’
In the code editor, simply either type in or paste the PHP code that you want to use.
In the following image, we are creating a snippet that shows the post’s publication date.
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.
After that, you are ready to scroll to the top of the screen and click on the ‘Inactive’ toggle so that it changes to ‘Active.’
Finally, click on ‘Save Snippet’ to make the snippet live.
Once you have done that, you are 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.
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 are ready to make the code live, either publish or update the page. Now, if we visit our website, then we will see the shortcode in action.
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.
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 permanently delete the snippet 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.
We hope this article helped you learn how to allow PHP in your WordPress posts and pages. You can also see our top tips to boost WordPress speed and performance and our expert picks for the must have WordPress plugins to grow your website.