1. Log into your WordPress dashboard.
2. Navigate to the post or page you want to add the code to.
3. Click the “Text” tab at the top right of the post or page editor.
4. Paste the code into the text editor.
5. Click the “Update” or “Publish” button to save your changes.
6. View the post or page to make sure the code is working correctly.
Looking to know how to run PHP code on a WordPress post or page? You’ve come to the right place. Here, we will uncover how to add PHP to a WordPress page or post so you can improve their functionality.
Reasons to Add PHP Code to WordPress Post or Page
By default, WordPress doesn’t allow you to run PHP code in posts or pages. The recommended way to add PHP functions is to modify the child theme or create custom page templates.
Despite so, there are occasions where you might want to add specific PHP functions to an individual post or page. In these cases, you can override the rule using WordPress code widget plugins.
The following tutorial will guide you through the process of using the Insert PHP Code Snippet plugin to add PHP code directly to a WordPress post or page.
How to Add PHP Code to WordPress Post or Page Using a Plugin
Before diving into the tutorial, make sure you have installed and activate the Insert PHP Code Snippet plugin. Check this tutorial for thorough instructions on how to install a WordPress plugin and activate it properly.
- From your WordPress dashboard, navigate to XYZ PHP Code -> PHPCode Snippets.
- In the PHP Code Snippets page, click Add New PHP Code Snippet. Add your desired PHP function and its tracking name to the appointed boxes. In this example, we will add the date() function that returns the local date and time. Then, click Create.
- If the process is successful, a confirmation message and the new function will appear on the screen. For future use, make sure all the PHP functions you want to use are active by checking the Action section. Active code snippets will show a green pause symbol while inactive ones will have a green checkmark next to them.
- To add a PHP code to a WordPress post or page, paste the snippet shortcode of the function you want to use in the desired location. If you’re using the Gutenberg block editor, use the shortcode or code block to display it.
- That’s it! Your newly created post should execute the PHP code like the following example:
Just because WordPress doesn’t allow you to run PHP code in a post or page, it’s not impossible to do so. Using WordPress code widget plugins, you can add various PHP functions to your posts or pages easily.
Do you find this tutorial helpful? Let us know in the comments section below!
Adding PHP to WordPress FAQ
Here are some frequently asked questions if you are interested in learning more on how to add PHP to WordPress.
Where Do I Put PHP Files in WordPress?
PHP files can be added to WordPress in a variety of ways, depending on the type and purpose of the file. Generally speaking, your PHP files should go into one of three folders: wp-content, root website, and the theme’s functions.
Can I Insert PHP Code in WordPress Page Without Plugin?
No, WordPress doesn’t allow you to run PHP code in a post or page without the help of a plugin. We recommend using WordPress code widget plugins instead to add various PHP functions to your posts or pages easily.
Can I Put PHP Code in an HTML Page?
Yes, you can add PHP code in an HTML page by enclosing all of your PHP code inside of <?php ?> tags at the top of the HTML markup for your page.
Adding PHP to WordPress: How to Add Code into Any Post or Page
WordPress is a powerful content management system that allows you to easily create and manage websites. It also allows you to add custom code to your posts and pages, including PHP. Adding PHP to WordPress can be a great way to extend the functionality of your website and make it more dynamic.
Why Add PHP to WordPress?
Adding PHP to WordPress can be a great way to extend the functionality of your website. PHP is a powerful scripting language that can be used to create dynamic webpages, interact with databases, and much more. By adding PHP to WordPress, you can create custom features and functions that are not available in the core WordPress software.
How to Add PHP to WordPress
Adding PHP to WordPress is relatively easy. The first step is to create a new file in your WordPress theme directory. This file should be named something like “functions.php”. This file will contain all of the PHP code that you want to add to your WordPress site.
Once you have created the file, you can add your PHP code to it. Make sure to include the opening and closing PHP tags at the beginning and end of the file. Once you have added your code, you can save the file and upload it to your WordPress theme directory.
Once the file is uploaded, you can then add the code to any post or page on your WordPress site. To do this, simply add the following code to the post or page:
<?php include('functions.php'); ?>
This code will include the functions.php file in the post or page, allowing the PHP code to be executed. You can also add the code directly to the post or page, but this is not recommended as it can be difficult to maintain and debug.
Adding PHP to WordPress is a great way to extend the functionality of your website. By creating a functions.php file and including it in your posts and pages, you can easily add custom code to your WordPress site. This can be a great way to create custom features and functions that are not available in the core WordPress software.