- How to write custom php in wordpress
- Some reasons for writing custom php
- Writing custom wordpress plugin
- Suggested posts:
- How to Allow PHP in WordPress Posts and Pages (Easy Tutorial)
- Why Allow PHP in WordPress Posts and Pages?
- How to Allow PHP in WordPress Posts and Pages
- How to Manage Your PHP Shortcodes
- How to Insert PHP Code into WordPress Posts and Pages
- Using PHP Code Snippet
- Setting Up Your Code Snippet
- Using PHP in the WordPress Editor
- Customizing Your Website
- How to write custom php in wordpress
- Some reasons for writing custom php
- Writing custom wordpress plugin
- Suggested posts:
How to write custom php in wordpress
If you are just writing text articles then you never need to write any custom php in wordpress. In case you want to write dynamic articles or pages, you will very soon need to write custom php.
Some reasons for writing custom php
- You want to include a source code or some other content from a file.
- You may have some device specific code for mobiles (based on user agent).
- You may have a custom query param and based on that want to show some specific information on a wordpress page.
- You want to customize the theme you are using
- You want to customize some wordpress default functionality.
Writing custom wordpress plugin
You can write your own plugin (with a php file at a minimum with any name). You will have to put the plugin folder in . /wp-content/plugins/ folder. e.g. If your plugin name is foo and it has a file foo.php, then foo.php should be placed at path . /wp-content/plugins/foo/foo.php and then it can be activated like a any plugin from wordpress admin ui.
Two types of php code (among many others) you can write with plugin are:
- shortcode: Here is how shortcode code snippet looks like:
function display_ip_impl ($atts) < echo $_SERVER['REMOTE_ADDR']; >add_shortcode('display_ip', 'display_ip_impl');
function my_single_tag_title($prefix, $display=false ) < // Some logic to decide title return $title; >add_filter('single_tag_title', 'my_single_tag_title');
- Insert author information at the end of a post.
- Or change html title and append a common suffix.
- Change h1 heading for tag and category pages
- Append related information or links to every post based on some data outside wordpress tables.
Suggested posts:
How to Allow PHP in WordPress Posts and Pages (Easy Tutorial)
Last updated on May 11th, 2023 by Editorial Staff | Reader Disclosure Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.
Do you want to allow PHP in your WordPress posts and pages?
By adding custom code to your site, you can fine-tune WordPress to better suit your needs. However, by default, WordPress does not let you add PHP directly to your pages.
In this article, we will show you how to allow custom PHP in WordPress posts and pages.
Why Allow PHP in WordPress Posts and Pages?
If you try typing PHP code directly into the WordPress block editor, then you will notice that WordPress strips away a lot of your code or even deletes it entirely.
WordPress doesn’t allow PHP in posts and pages for security reasons. This is because a simple mistake in your PHP code can cause all sorts of common WordPress errors.
However, there are some cases where you may need to add PHP to WordPress posts and pages.
For example, if you make money through affiliate marketing, then you might need to add advertising code to your site.
You may also have developed your own PHP functions. For example, you can use custom PHP to validate user logins on your site’s members-only pages.
With that being said, let’s see how you can allow PHP in WordPress posts and pages.
How to Allow PHP in WordPress Posts and Pages
WordPress may not allow PHP directly in posts and pages, but it does allow shortcodes. This means you can add custom PHP to your content by creating a shortcode and then using it in your pages and posts.
The easiest way to add custom shortcodes in WordPress is by using WPCode. This plugin allows you to create as many shortcodes as you want and then link those shortcodes to different sections of PHP code.
The first thing you need to do is install and activate the free WPCode plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.
Upon activation, head over to Code Snippets » Add Snippet.
Here, you will see all the ready-made snippets you can add to your website. These include snippets that allow you to completely disable WordPress comments, upload files that WordPress doesn’t support by default, and more.
Since you are creating a new snippet, you need to hover over ‘Add Your Custom Code.’ Then, click on ‘Use snippet.’
To start, type in a title for the custom code snippet. This can be anything that helps you identify the snippet in the WordPress dashboard.
After that, open the ‘Code Type’ dropdown and select ‘PHP Snippet.’
In the code editor, simply either type in or paste the PHP code that you want to use.
In the following image, we are creating a snippet that shows the post’s publication date.
After that, scroll to the ‘Insertion’ section. WPCode can automatically add your code to different locations, such as after every post, frontend only, or admin only.
Since we want to add the custom PHP using shortcode, click on ‘Shortcode.’ WPCode will now show the shortcode that you can add to any page, post, or widget-ready area.
After that, you are ready to scroll to the top of the screen and click on the ‘Inactive’ toggle so that it changes to ‘Active.’
Finally, click on ‘Save Snippet’ to make the snippet live.
Once you have done that, you are ready to add the custom PHP to your WordPress website by opening any page or post.
In the block editor, simply click on the ‘+’ button and type in ‘Shortcode.’ When it appears, select the Shortcode block to add it to the page or post.
You can now paste the shortcode into this block. For more information, please see our beginner’s guide on how to add a shortcode in WordPress.
When you are ready to make the code live, either publish or update the page. Now, if we visit our website, then we will see the shortcode in action.
How to Manage Your PHP Shortcodes
If you want to remove a code snippet from a page or post, then you can simply delete the Shortcode block.
However, at some point, you may want to remove a custom PHP snippet from your entire website. In this case, it’s often easier to simply deactivate the snippet, as this will disable it for every page or post.
To do this, simply go to Code Snippets » Code Snippet.
Then, just find the code that you want to deactivate and click on its toggle to turn it from Enabled (Blue) to Disabled (Grey).
After that, it’s a good idea to visit your website and check that the deactivated code isn’t causing any problems.
If your WordPress blog looks and acts normally without the code, then you may want to permanently delete the snippet by going to Code Snippets » Code Snippet.
Here, simply hover over the snippet that you want to delete and then click on ‘Trash’ when it shows up.
We hope this article helped you learn how to allow PHP in your WordPress posts and pages. You can also see our top tips to boost WordPress speed and performance and our expert picks for the must have WordPress plugins to grow your website.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
How to Insert PHP Code into WordPress Posts and Pages
It’s not a common practice for developers to insert PHP code into WordPress posts and pages. Usually, site owners will modify theme files to create a desired effect. However, some people find placing snippets into posts and pages to be more ideal given certain situations.
If you’re trying to make changes to how a theme behaves, I suggest using a child theme instead of placing PHP in posts. But if you’re looking to include a post or page-specific element with PHP, you can do so with snippets and shortcodes.
Today, I’m going to go over how to place PHP in posts and pages. I still advise making certain changes directly from theme files, but this method works exceptionally well in many instances.
Using PHP Code Snippet
If you’ve tried to insert coding in WordPress, you may notice how it will either strip certain elements or prevent the use altogether. Perhaps one of the best ways to get around this problem is by using PHP Code Snippet.
This plugin will take your snippets of PHP code and turn them into usable shortcodes for WordPress. And because it’s a shortcode, it will work in posts, pages and even the Text widget for a sidebar component.
You can use this to create advertising codes from affiliates, validating logins from users or any other function you might have developed in PHP. Just make sure you’re using a safe and current version of PHP when making modifications.
Install and activate the Insert PHP Code Snippet plugin.
Setting Up Your Code Snippet
Click the “XYZ PHP Code” function from the left.
Click the button to “Add New PHP Code Snippet.”
Input a Tracking Name for the snippet. This is what the plugin will use to “name” the shortcode. It’s probably better if you keep it short and succinct so you can easily identify it later. It doesn’t have to be anything fancy. For example, you could use the word, “Clock” if you are inserting a PHP coded clock on your site.
Once you are done, click the “Create” button on the bottom.
Now, you will see a shortcode created for the code snippet. You can copy and paste this into any area of WordPress that allows the use of shortcodes.
Using PHP in the WordPress Editor
While the shortcode is useful in some situations, what if you don’t want to remember to copy and paste the text? Perhaps you just want to insert PHP in posts from the editor screen.
PHP Code Snippet does that as well. Let me show you where that function is located when creating or editing a post.
Go to Posts and click, “Add New.”
In the visual editor for WordPress, you’ll see a new button. Click the “PHP” from the editor tool bar.
Your list of currently saved PHP code snippets will be visible from this button. Click the snippet you want and the plugin will insert the shortcode automatically.
This feature is not available in the Text editor of WordPress. It is available only in the Visual editor for posts, pages and some custom post types. If the button is not available, you’ll have to paste the shortcode yourself.
Customizing Your Website
This method is one of the easiest ways to customize WordPress with PHP code in pages and posts. It’s a simple way to deliver a more engaging experience for visitors. While some PHP adjustments will still require modifying theme files, you can use this plugin to deliver some of the more simpler additions.
What kind of PHP elements do you include in your website design? How often do you modify the files of your themes for specific functions in PHP?
How to write custom php in wordpress
If you are just writing text articles then you never need to write any custom php in wordpress. In case you want to write dynamic articles or pages, you will very soon need to write custom php.
Some reasons for writing custom php
- You want to include a source code or some other content from a file.
- You may have some device specific code for mobiles (based on user agent).
- You may have a custom query param and based on that want to show some specific information on a wordpress page.
- You want to customize the theme you are using
- You want to customize some wordpress default functionality.
Writing custom wordpress plugin
You can write your own plugin (with a php file at a minimum with any name). You will have to put the plugin folder in . /wp-content/plugins/ folder. e.g. If your plugin name is foo and it has a file foo.php, then foo.php should be placed at path . /wp-content/plugins/foo/foo.php and then it can be activated like a any plugin from wordpress admin ui.
Two types of php code (among many others) you can write with plugin are:
- shortcode: Here is how shortcode code snippet looks like:
function display_ip_impl ($atts) < echo $_SERVER['REMOTE_ADDR']; >add_shortcode('display_ip', 'display_ip_impl');
function my_single_tag_title($prefix, $display=false ) < // Some logic to decide title return $title; >add_filter('single_tag_title', 'my_single_tag_title');
- Insert author information at the end of a post.
- Or change html title and append a common suffix.
- Change h1 heading for tag and category pages
- Append related information or links to every post based on some data outside wordpress tables.