How Do You Add PHP Code in a WordPress Page?
Add PHP to WordPress pages safely with shortcodes, theme files, and tested code.
Understanding PHP in WordPress

WordPress does not run PHP code placed directly in a page or post. The editor treats that code as page content. It may show the code or strip parts of it.
PHP runs on the server before WordPress sends a page to the visitor. A theme or plugin can run PHP safely at that stage. The editor can then show the result through a shortcode.
This answers the main question about how to add PHP code in a WordPress page. You need a shortcode, a trusted plugin, or a theme file. Do not paste PHP into a Paragraph block and expect it to run.
Choose the method based on who needs to edit the feature. A small site feature may suit a shortcode plugin. A site-wide feature belongs in a plugin or child theme.
Methods for Adding PHP to WordPress

There are three common ways to add PHP to WordPress. Each method has a different level of control and risk.
- Snippet plugin: Add PHP in a controlled admin screen, then place its shortcode in the editor.
- Custom plugin: Store your function in a site plugin that remains active after a theme change.
- Child theme: Add code to the child theme's functions file for theme-specific work.
A snippet plugin is often the simplest choice for a new user. It keeps PHP outside page content. It also lets you disable one snippet without editing theme files.
The WordPress editor can insert the shortcode in a Paragraph or Shortcode block. The PHP still runs from the plugin or theme. The editor only holds the shortcode name and its settings.
For deeper control, use a custom plugin. WordPress explains the shortcode system in its shortcode documentation. That source shows how WordPress registers and handles shortcode callbacks.
Using PHP Code Snippets and Custom Shortcodes

The Insert PHP Code Snippet plugin can create a shortcode for a PHP function. Install it from the Plugins screen, then activate it. Use a current backup before adding code.
- Open the plugin's snippet area in the WordPress dashboard.
- Create a new snippet with a clear name, such as current year.
- Paste only the function code that the snippet needs.
- Save the snippet and copy the shortcode that it creates.
- Open the target page and place the shortcode in a Shortcode block.
- Preview the page, then test it while logged out.
For example, a small shortcode can return the current year. The function may look like this: function site_year() { return date('Y'); }. The shortcode registration may look like this: add_shortcode('site_year', 'site_year');.
Some snippet plugins add the shortcode registration for you. Follow the plugin's own format in that case. Do not add the PHP opening tag if the plugin already supplies it.
You can also create a custom shortcode in a child theme or site plugin. Use a unique prefix for your function name. A name such as tw_site_year reduces clashes with other plugins.
Keep shortcode functions focused. They should return a value instead of printing it. Escape visitor input before showing it. WordPress offers useful rules in its security guidance for developers.
Best Practices for Adding PHP

Good PHP work starts with the right place for the code. Use a custom plugin for features that should survive a theme switch. Use a child theme for code tied to one theme's layout.
Avoid the built-in Theme File Editor on a live site. One syntax error can cause a fatal error. You may also lose access to the dashboard until the file is fixed.
Use staging when possible. Staging is a private copy of your site. Test the snippet there before moving it to production.
- Back up the database and files before each change.
- Give each function a unique prefix.
- Use strict checks for values from forms, URLs, and cookies.
- Escape output for its final setting, such as HTML or a URL.
- Limit admin access to users who need to edit PHP.
- Keep a short note about each snippet and its purpose.
Security matters because PHP can read data and change site behavior. A copied snippet may contain unsafe queries or weak input checks. Only use code from a source you trust and understand.
Plugin and theme compatibility also matters. A new theme may remove a function that your snippet calls. A plugin update may change a hook or shortcode name.
Test with your key plugins active. Check the site on mobile, in a private browser, and with a normal visitor account. Small tests catch most conflicts early.
Troubleshooting Common PHP Issues
When PHP does not run, start with the smallest test. Create a shortcode that returns one fixed word. If that works, add your main logic in small steps.
A blank page often points to a fatal PHP error. Turn on logging in a staging copy, then check the debug log. Never show raw errors to public visitors.
If the page shows the shortcode itself, check three things. Confirm the snippet is active. Check the shortcode spelling. Then confirm that the shortcode sits in a block that supports shortcodes.
A function name conflict can stop a snippet from working. Search your active plugins and theme for the same name. Add a unique prefix and test again.
| Problem | Likely cause | First fix |
|---|---|---|
| Code appears on the page | PHP was pasted into page content | Move it to a snippet, plugin, or child theme |
| Shortcode stays visible | Snippet is off or the name is wrong | Check the snippet status and spelling |
| White screen | PHP syntax or function error | Check the staging debug log |
| Feature breaks after a theme change | Code lived in the old theme | Move site-wide code to a custom plugin |
To edit PHP code in WordPress, change one part at a time. Save a backup of the last working version. If the error began after one change, undo that change first.
When learning how to run PHP code in WordPress, focus on safe placement and small tests. The editor is for content. Plugins and theme files are for PHP logic.
Step-by-step
- 01 Choose a safe code location
Use a snippet plugin for a small feature. Use a custom plugin or child theme for code that needs more control.
- 02 Create the PHP snippet
Add a focused function with a unique prefix. Follow the plugin format and avoid unsafe visitor input.
- 03 Register the shortcode
Let the plugin create the shortcode when supported. Otherwise, register your callback with add_shortcode.
- 04 Insert the shortcode
Open the WordPress page and add the shortcode in a Shortcode block. Save a backup before testing.
- 05 Test and deploy
Test the feature on staging, while logged out, and on mobile. Move it to the live site only after it works.
Frequently asked questions
- Can I put PHP code directly in a WordPress page?
- No. WordPress does not run PHP placed in normal page content. Use a shortcode, plugin, or theme file instead.
- How do I add PHP code to a WordPress page?
- Add the PHP to a snippet plugin or custom plugin. Then place its shortcode in the WordPress editor.
- How do I run PHP code in WordPress without editing a theme?
- Use a trusted snippet plugin such as Insert PHP Code Snippet. It lets you run a function through a shortcode.
- Should I use the WordPress Theme File Editor for PHP?
- Avoid it on live sites. A syntax error can lock you out of the dashboard, so use staging and a child theme or plugin.
- Why does my PHP shortcode show as plain text?
- The snippet may be inactive, or the shortcode name may be wrong. Check the shortcode block and the exact spelling.
- How can I test PHP code before using it on a live site?
- Use a staging copy with a recent backup. Test the page while logged out and check the debug log for errors.
Related reading
How Do You Manually Update a WordPress Plugin?
Update WordPress plugins by hand without risking site stability.
How Do You Convert Wix to WordPress?
Move from Wix to WordPress with a safer, step-by-step migration plan.
How to Move From Squarespace to WordPress Without Downtime
Move your site to WordPress with a safe export, clean rebuild, and smooth launch.