Developer · Guide
How to Add PHP to WordPress (Step by Step)

Short answer
Add PHP to WordPress with a code snippets plugin, a child theme's functions file or a small custom plugin. WordPress does not run PHP typed into posts, so use a shortcode when you need PHP output inside content. Take a backup first.
Key takeaways
- The post editor does not run PHP, by design.
- A code snippets plugin is the safest method for most site owners.
- Edit a child theme's functions.php, never the parent theme's.
- A custom plugin keeps code working when you change themes.
- Shortcodes let PHP output appear inside posts and pages.
How do you add PHP to WordPress?
You add PHP to WordPress with a code snippets plugin, a child theme's functions.php file, or a small custom plugin, and you never paste raw PHP into the post editor.
WordPress is built on PHP, but it does not run PHP typed into posts or pages. The editor treats it as text, and security rules block it on purpose.
Instead, PHP goes into files that WordPress loads, or into a plugin that stores and runs snippets for you. Each method suits a different kind of code and a different level of comfort.
This guide covers each method step by step, plus how to run PHP inside page content with a shortcode. It ends with the safety checks that prevent a single typo from taking your site down.
| Method | Best for | Survives theme updates | Risk if code has an error |
|---|---|---|---|
| Code snippets plugin | Small tweaks, beginners | Yes | Low, many plugins auto-pause bad snippets |
| Child theme functions.php | Theme-related changes | Yes | High, can cause a critical error |
| Custom plugin | Features that should outlast the theme | Yes | High, but easy to deactivate |
| Must-use plugin | Code that must always run | Yes | High, cannot be deactivated in the dashboard |
| Shortcode | Running PHP output inside content | Depends where it is defined | Same as the method that holds it |
| Template file | Changing page layout | Yes, if in a child theme | High |
Can you add PHP directly in the WordPress editor?
No, the WordPress block editor and classic editor do not run PHP, so code pasted into a post or page displays as text or gets stripped out when you save.
This is a security feature. If editors could run PHP from a post, anyone with author access could take over the whole server.
Old plugins that let you run PHP inside posts still exist, but they carry the same risk. Avoid them, especially on sites with more than one user.
When you need PHP output inside content, wrap the code in a shortcode or a custom block instead. The section on shortcodes below shows how.
What should you check before adding PHP?
Before adding PHP, take a full backup, confirm you can reach your files through FTP or your host's file manager, and test the code on a staging site if you can.
A single missing semicolon in functions.php can cause a critical error and lock you out of the dashboard. File access lets you undo the change even then.
Check the source of the code too. Snippets copied from old blog posts may use functions that have since been removed, or may not work with your PHP version.
A staging copy is the safest place to try new code. Our guide on setting up a staging site for WordPress shows how to create one.
How do you add PHP with a code snippets plugin?
Install a snippets plugin such as Code Snippets or WPCode, create a new PHP snippet, paste your code, choose where it runs, and activate it.
This is the easiest method for most site owners. The code is stored in the database, it survives theme changes, and you can switch snippets on and off with one click.
The general steps are:
- Install Code Snippets or WPCode from Plugins → Add New.
- Open the plugin's menu and add a new snippet.
- Choose PHP as the code type.
- Paste your code without the opening <?php tag unless the plugin asks for it.
- Pick where it should run, such as everywhere, admin only or front end only.
- Save and activate the snippet.
Many snippet plugins check for errors and refuse to activate code that would break the site. That safety net is the main reason to choose this method.
Our guide on how to use the WPCode plugin covers its settings in detail.
How do you add PHP to functions.php?
Add PHP to the functions.php file of a child theme, not the parent theme, using the theme file editor or FTP, and save only after checking the code carefully.
Every theme can have a functions.php file. WordPress loads it on every request, so any function or hook you place there runs across the site.
Editing the parent theme's file is a mistake. The next theme update replaces the file and wipes out your code.
A child theme fixes that. It inherits everything from the parent but keeps its own functions.php, which updates never touch. The child themes handbook explains how to create one, and our explainer on what a child theme is covers the basics.
To edit the file, go to Appearance → Theme File Editor, select the child theme and open functions.php. Block themes may show this under Tools instead. If the editor is missing, our guide on the theme editor not showing explains why.
Add your code at the end of the file. Leave out a closing ?> tag, because stray spaces after it can cause header errors.
How do you create a custom plugin for your PHP?
Create a new folder in wp-content/plugins, add a PHP file with a plugin header comment, paste your code below it, then activate the plugin from the Plugins screen.
A custom plugin keeps your code separate from the theme. If you switch themes later, your features keep working.
A minimal plugin file looks like this:
<?php
/**
* Plugin Name: My Site Tweaks
* Description: Custom code for this site.
* Version: 1.0
*/
// Your code goes here.
Save it as something like my-site-tweaks.php inside a folder of the same name. Upload it over FTP, then activate it under Plugins.
The Plugin Handbook basics explain the header fields and file structure. If something breaks, renaming the folder over FTP turns the plugin off straight away.
When should you use a must-use plugin?
Use a must-use plugin when code must run on every request and should never be turned off by accident, such as security rules or site-wide settings on a client site.
Must-use plugins live in wp-content/mu-plugins. WordPress loads every PHP file in that folder automatically, and they cannot be deactivated from the dashboard.
Create the folder if it does not exist, then add a PHP file with your code. There is no activation step.
That strength is also the risk. A broken must-use plugin can only be removed through file access, so test the code elsewhere first. The handbook page on must use plugins lists other limits.
How do you run PHP inside a post or page?
Register a shortcode in your snippets plugin, child theme or custom plugin, put your PHP logic in its callback, and then add the shortcode to any post or page.
Shortcodes are the safe bridge between content and PHP. The editor stores only the short tag, while the code stays in a file or snippet you control.
Here is a simple example that prints the current year:
add_shortcode( 'current_year', function () {
return date( 'Y' );
} );
Add [current_year] to a post, and WordPress replaces it with the year. The callback must return its output rather than echo it, or the text appears in the wrong place.
The add_shortcode() reference explains the arguments. Our explainer on what a shortcode is covers how they work in the editor.
How do you use hooks in your PHP code?
Hooks let your PHP run at the right moment, such as when a page loads, a post saves or the footer prints, without editing WordPress core files.
Actions run code at a point in time. Filters change a value before WordPress uses it. Almost every snippet you add relies on one of them.
For example, this snippet adds a line to the site footer using an action:
add_action( 'wp_footer', function () {
echo '<p>Thanks for visiting.</p>';
} );
Our guide on WordPress hooks, actions and filters explains how to find and use them. The official Hooks documentation has the full reference.
What should you do if your PHP code breaks the site?
If new PHP breaks the site, remove or undo the code through FTP or your host's file manager, then turn on debugging to find the exact error before trying again.
For a snippets plugin, many offer a safe mode you can trigger with a URL parameter. Check your plugin's docs for the exact method.
For functions.php or a custom plugin, open the file over FTP and remove the new code. For a plugin, renaming its folder is enough to disable it.
Debug logging shows the file and line number of the error. Our guide on enabling debug mode in WordPress walks through it, and the PHP manual explains any function the error mentions.
If you see the message about a critical error, follow our guide on fixing the critical error in WordPress.
How do you keep custom PHP safe and easy to manage?
Keep custom PHP in one place, comment what each snippet does, check it against your PHP version, and remove snippets you no longer need.
Scattered code is hard to debug. If some lives in functions.php, some in a snippets plugin and some in a custom plugin, finding the cause of an error takes far longer.
Write a short comment above each snippet saying what it does and why. Future you, or a developer you hire, will thank you.
Check that code works with your server's PHP version before adding it. Our guide on WordPress PHP compatibility explains what to look for.
Never add code that takes input from visitors without sanitizing and escaping it. Unchecked input is one of the most common ways custom code opens security holes. The OWASP Top Ten lists the main risks to watch for.
Frequently asked questions
Is it safe to edit functions.php in WordPress?
It is safe if you edit a child theme's file, take a backup first and have FTP access ready. A single typo can cause a critical error, so many site owners prefer a code snippets plugin, which can catch errors before the code runs.
Will my PHP code disappear when the theme updates?
Code in the parent theme's functions file is replaced on every update. Code in a child theme, a snippets plugin or a custom plugin stays in place. Move any custom code out of the parent theme to keep it safe.
Do I need the opening PHP tag in a code snippet?
Most snippet plugins add the opening tag for you, so you paste only the code itself. Files such as a custom plugin or functions file need the opening tag at the very top. Check the snippet plugin's editor, which usually shows whether it is included.
Can I run PHP in a widget?
Not directly. Text and HTML widgets do not run PHP. Create a shortcode with your PHP logic and place it in a shortcode block or widget instead. This keeps the code in a file or snippet you control rather than in the widget content.
Can I add PHP on WordPress.com?
Only on plans that allow plugins and theme file access. Lower plans do not let you install snippet plugins or upload custom code. A self-hosted WordPress site gives full access to add PHP through any of the methods covered here.
Sources
- Child Themes WordPress Developer Resources
- Plugin Basics WordPress Developer Resources
- Must Use Plugins WordPress Developer Resources
- add_shortcode() WordPress Developer Resources
- Code Snippets WordPress.org Plugins
- PHP Manual php.net
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
Related guides
About the editors
Researched from the primary sources listed above and checked against our editorial rules before publishing.


