Do you want to add PHP code in your WordPress posts and pages? This guide will help you do it easily. Follow the steps below to make it work smoothly.
Why Use PHP in WordPress?
PHP is a programming language. It helps create dynamic content. With PHP, you can add custom functions to your website. This makes your site more interactive and useful for visitors.

Credit: www.wpbeginner.com
Understanding PHP in WordPress
WordPress uses PHP to run. But by default, it doesn’t allow PHP code in posts and pages. This is for security reasons. However, you can change this by using plugins or code snippets.

Credit: www.greengeeks.com
Steps to Add PHP Code in WordPress
Using A Plugin
One of the easiest ways to add PHP code is by using a plugin. Plugins are tools that add extra features to your WordPress site. Here are the steps:
- Log in to your WordPress dashboard.
- Go to Plugins and click Add New.
- Search for a plugin called Insert PHP Code Snippet.
- Install and activate the plugin.
- Go to XYZ PHP Code in the dashboard menu.
- Click on Add New PHP Code Snippet.
- Enter your PHP code in the box.
- Save the snippet and copy the shortcode.
- Paste the shortcode in your post or page where you want the PHP code to run.
That’s it! Your PHP code will now run in your post or page.
Using Code Snippets
If you don’t want to use a plugin, you can add PHP code directly. This method involves editing your theme’s files. Follow these steps:
- Log in to your WordPress dashboard.
- Go to Appearance and click Theme Editor.
- Select the functions.php file from the right-hand side.
- Add the following code at the end of the file:
function execute_php($html) {
if (strpos($html, "<" . "?php") !== false) {
ob_start();
eval("?" . ">" . $html);
$html = ob_get_contents();
ob_end_clean();
}
return $html;
}
add_filter('widget_text', 'execute_php', 100);
add_filter('the_content', 'execute_php', 100);
add_filter('the_excerpt', 'execute_php', 100);
- Save the changes.
- Now you can add PHP code directly in your posts and pages using the php ? tags.
Pros and Cons of Adding PHP Code
Pros
- Add custom functions to your site.
- Create dynamic and interactive content.
- Enhance user experience.
Cons
- Can be risky if not used properly.
- May slow down your site.
- Requires basic knowledge of PHP.
Best Practices
Here are some tips to ensure smooth usage of PHP in WordPress:
- Always back up your site before making changes.
- Test the PHP code on a staging site first.
- Keep your PHP code simple and clean.
- Use trusted plugins to avoid security risks.
- Update your plugins and themes regularly.
Common Mistakes to Avoid
- Not backing up your site before changes.
- Using complex PHP code without testing.
- Ignoring plugin and theme updates.
- Adding PHP code in the wrong place.
- Not using a child theme for customizations.
Frequently Asked Questions
How To Add Php Code In WordPress Post?
Use a plugin like Insert PHP Code Snippet. It helps include PHP in posts.
Is It Safe To Use Php In WordPress?
Yes, if you use trusted plugins and follow best practices. Always test on a staging site first.
Can I Run Php In WordPress Pages?
Yes, you can. Use plugins to easily add PHP code to WordPress pages.
Which Plugin Allows Php In Posts?
Insert PHP Code Snippet is popular. It is easy to use and effective.
Conclusion
Adding PHP code to your WordPress posts and pages can enhance your site. You can use plugins or code snippets to achieve this. Always follow best practices to ensure safety and performance. Start with simple PHP code and test it before going live. This way, you can make your WordPress site more dynamic and interactive.