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.

Insert PHP Code Snippet

Setting Up Your Code Snippet

Click the “XYZ PHP Code” function from the left.

XYZ PHP Code

Click the button to “Add New PHP Code Snippet.”

Add New 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.

Snippet Tracking

Input the PHP code.

PHP Code

Once you are done, click the “Create” button on the bottom.

Click Create

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.

Shortcode

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

Add New Post

In the visual editor for WordPress, you’ll see a new button. Click the “PHP” from the editor tool bar.

PHP Button

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.

PHP Snippet

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?

4 thoughts on “How to Insert PHP Code into WordPress Posts and Pages”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.