How to Add PHP Code to Your WordPress Post or Page

There are several ways to add php code to your WordPress blog. (Note: For our purposes here, we are talking about executing php code, not displaying php code for others to see. We will be using php code to make our page more dynamic.)

PHP Plugin

If it’s only one or two lines of code, the a php plugin such as ezphp or php code for posts would probably be best. These usually use the php ‘exec’ command to execute the php code.

You must be careful with these because if you accidentally put them in the visual editor instead of the text editor, it will not work. It will change the less than and greater than signs to < and >. It will also change other special characters so that they can be seen on the blog instead of used in the php code.

For this reason, I don’t like to use these plugins very often. If my users can get in to edit the page, then there’s a chance they will annihilate my code.

Also, when there’s an error in the php code, it can be very hard to debug since there’s not much way to see which line the error occurs. It only gives an error for the exec statement.

Reusable Custom Page Template

If you want to use the same basic template in several different pages, using an ftp client, log into your site and make a copy of the current page.php file in the /wp-content/themes/{your-theme}/ folder. Don’t forget to replace {your-theme} with your current theme’s name. Give the copied file a new name. You can name it anything with an extension of .php. You might name it CustomTemplate.php.

Inside that file at the top, add the comment:

<?php /* Template Name: Custom Page */ ?>

Then change the code in the page to make it do what you want your new template to do. Once you’ve finished the code, you can go create a new page in your WordPress admin area. Over to the right you will have the option to choose which template you want to use. If you’ve done everything correctly, you should see your new template. Your page should use the php code from your custom template.

One-Page Custom Template

If you only need the custom template for one page, you can copy the page.php file as you did before, but don’t add the ‘Template Name’ comment. Name the file page-{slug}.php where {slug} is the slug name of the page you created in WordPress. Change the php code in the new template file to do what you want it to do and WordPress should take care of the rest!

In other words, I’ll create a WordPress page named ‘My Page’. The slug should be my-page. I’ll copy the page.php file and name it page-my-page.php and change the code to do what I want to do on My Page. When I view http://mydomain.com/my-page, I should see the desired results from my custom php page template.

Shortcodes

One of my favorite ways to add php code to a page is to create a shortcode. Then I can add the code anywhere I want with just a simple little line of code like this:

[my-shortcode]

Leave a Comment

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

Scroll to Top