Themewaves
How-To

How Do You Add PHP Code in a WordPress Page?

Add PHP to WordPress pages safely with shortcodes, theme files, and tested code.

How Do You Add PHP Code in a WordPress Page?

Understanding PHP in WordPress

Graphite server box with a crimson cable on a white desk for PHP processing
How PHP runs on a server

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

Geometric graphite blocks and a crimson block showing WordPress code paths
Three ways to add PHP

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

Closed graphite notebook and crimson bookmark beside a neat cable on white
Organized PHP snippet planning

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.

  1. Open the plugin's snippet area in the WordPress dashboard.
  2. Create a new snippet with a clear name, such as current year.
  3. Paste only the function code that the snippet needs.
  4. Save the snippet and copy the shortcode that it creates.
  5. Open the target page and place the shortcode in a Shortcode block.
  6. 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

Graphite folders and crimson divider arranged for safe WordPress code work
Safe PHP code organization

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.

ProblemLikely causeFirst fix
Code appears on the pagePHP was pasted into page contentMove it to a snippet, plugin, or child theme
Shortcode stays visibleSnippet is off or the name is wrongCheck the snippet status and spelling
White screenPHP syntax or function errorCheck the staging debug log
Feature breaks after a theme changeCode lived in the old themeMove 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

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

  2. 02
    Create the PHP snippet

    Add a focused function with a unique prefix. Follow the plugin format and avoid unsafe visitor input.

  3. 03
    Register the shortcode

    Let the plugin create the shortcode when supported. Otherwise, register your callback with add_shortcode.

  4. 04
    Insert the shortcode

    Open the WordPress page and add the shortcode in a Shortcode block. Save a backup before testing.

  5. 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.
Share XFacebookWhatsApp

Related reading

About cookies on this site

Essential cookies keep pages loading and the enquiry form working. Anything beyond that is optional and we only set it if you say yes. Details live in our Cookie Policy.