Using PHP Inside Content of WordPress Blogs

Using PHP inside of content on WordPress sites may not produce the expected results. As WordPress is built in PHP, you might think you could just start typing PHP code inside a post to pull some information from a database. Sorry to say, but it doesn’t work that way.

WordPress takes your blog content and runs it through some backend shenanigans to create the output that you see on the screen. WP expects to find other content in posts, like text and images, not code.

To run PHP code inside a blog post or Page, you’ll have to work a little differently than simply filling up the space in the text editor.

There are two ways (at least) of using PHP inside WP blogs to produce the desired output. The first way is to set up a WP template page that points to a pre-written PHP page that is identified in a Page. The second way is to use a plugin that facilitates PHP code execution inside content areas in WordPress. Both ways have their advantages and disadvantages.

Using WP Template Pages to Execute PHP Code

A template page is used to identify a pre-written PHP page. By saying “a pre-written PHP page” I mean a .php file that you’ve already written and which has been executed successfully by a server. A template file has at the top of it a PHP comment which identifies the template name followed by an include statement that identifies the .php file. WP template files must follow this format to be properly identified by the WordPress engine as a template file.

<?php
/*
Template Name: soccer-roster
*/
include (TEMPLATEPATH . ‘/soccer-roster.php’);
?>

The above snippet  is a complete template file which indicates the name of the template as “soccer-roster” and the path to the associated PHP file, soccer-roster.php. The template file is best saved with “template” in the file name. It must be located in the site’s theme folder.

After creating the template file and .php file, upload both to the theme folder. Create a blank Page. Do not put anything where you would normally write the content, but go ahead and give it a meaningful title. On the Add New Page page, look for the “Page Attributes” widget. Click the down-arrow to expand the list of templates and select your template. Using the example above, you would select the “soccer-roster” template. If the template is not stored in the theme folder, it will not appear in this drop-down list. Save the page and preview it. The PHP code, from soccer-roster.php, should be executed just as you would expect.

PROs

  • No interference from WP, code interpreted as stand-alone PHP.
  • Page renders as expected.

CONs

  • Can only be used with Pages, not posts or text widgets.
  • May have to wrestle with adapting PHP output into style of site.
  • CAUTION: Use a child-theme to safely save site-specific template files.

If your site relies heavily on plugins, using template pages may be the best method, see below. If it’s not a big deal to style the PHP output pages like the rest of your site, creating WP Pages that use templates is a breeze. If you need to put code in a post or text widget, keep reading.

Using WP Plugins to Execute PHP Code

Modifying WordPress themes is not for everyone, so using a plugin to execute PHP code may be the easier option. There are more than a handful of plugins available for the task. I chose to use Exec-PHP plugin for WordPress. It is a very well documented plugin. Thank you, Sören.

Download, unpack, upload and activate the plugin in the usual fashion. Verify these settings to make the plugin do its magic:

  • Disable tag balancing by unchecking ‘WordPress should correct invalidly nested XHTML automatically’ in the Settings/Write menu.
  • Disable the WYSIWYG editor (visual editor) in the user’s settings through the Users/Your Profile menu.
  • Assign the ‘unfiltered_html’ capability to the user, if user is not the administrator. Can use role manager plugin to assign this capability to other users.
  • Assign the ‘exec_php’ capability to the user, if user is not the administrator. Can use role manager plugin to assign this capability to other users.

Once I disabled the visual editor, my site was ready to go.

PROs

  • Easy, just change a couple of settings in blog admin. Plugin gives highly visible warning on Add New Post or Add New Page pages if the settings are not correct.
  • PHP code can be written in the normal fashion using the <?php ?> tags. It does not need to be marked up in any special way as it might with other plugins.
  • PHP can be used in text widgets and posts, as well as Pages.

CONs

  • Can’t use plugins that embellish or rely on the WYSIWYG editor.

Didn’t try any other PHP-execution plugins because Exec-PHP worked as soon as the visual editor was disabled.

As mentioned either method will get you there, it’s just a matter of style. If you need the visual editor to create your posts, use the template method. If you want to put PHP code in posts and text widgets, use the plugin method. Good luck!

Which Versions of PHP and MySql Run Your WordPress Blog?

How Healthy is Your WordPress Blog? Do you know what version of PHP or MySQL your server is running?

The improvements in WordPress continue. Check the state of your blog’s health by running the new Health Check plugin. Currently, it will check for the versions of PHP and MySQL that are running your blog. In order to work with WP 3.2, your server will need to be up-to-date and running —

  • PHP version 5.2 or greater
  • MySQL version 5.0.15 or greater

In an attempt to help others into the 21st century, WordPress will no longer work using PHP 4 or MySQL 4.

WordPress will also drop support for Internet Explorer 6. Yay! There are other improvements coming, so stay tuned!

Scheduled Posts Calendar on New Post Page via Plugin

When scheduling posts on your WordPress blog, a few of the right plugins can make this job easier and more manageable.

First, use the Dashboard Widget Scheduled Posts plugin. This plugin creates a widget for your WP Dashboard that lists the upcoming posts that are scheduled, but not yet published.

Second, use the Future Posts Calendar plugin. This plugin creates two sets of calendars based on your scheduled posts. One calendar resides in the right column of the New Post page that you’ll see when you’re writing a new post or editing a previous one. The second calendar will be seen by site visitors as it resides in a widget that you can choose to add to a sidebar of your blog.

When there are no scheduled posts, the future calendar on the New Post page will have the current date in bold.

No future posts scheduled at this time.
No future posts scheduled at this time.

When at least one post is scheduled for future publication, the future calendar – either on the New Post page or in the blog sidebar – will have differently colored backgrounds depending on the number of posts scheduled for that day.

Posts scheduled to appear on the 20th and 21st on the month.
Posts scheduled to appear on the 20th and 21st on the month.

A green background indicates one post, light blue means two posts and blue means three posts scheduled for that day.

 

Clicking a date in the calendar will change the scheduled posting date. Nice feature!

If you’re not bothered by editing code, you can alter the PHP file to make a dashboard widget, too.

If you want to ad a widget to your dashboard, uncomment the line add_action('wp_dashboard_setup', 'fpc_setup_dashboard_widget'); in the future_calendar.php file.

The dashboard widget only shows the date of future posts in the calendar format, so I prefer the Dashboard Widget plugin as it includes post titles.

Dashboard Widget Plugins for Scheduled Posts in WordPress

It takes time to save a post or page that you’re working on to check out the dates that you may have scheduled other posts. Even in WordPress 3.1 the only place to see the dates of scheduled posts is on the Posts page. When you’re working on pages or posts, it would be nice to see at a glance when other content will be posted to the web site.

I reviewed two plugins that were developed to display the scheduled posts as a Dashboard Widget.

Future Dashboard Widget creates a widget for the dashboard that lists the Scheduled Posts by date, including the post titles and first line of content. The entries are listed in reverse time order so the next post to be posted will be at the bottom of the list. A link is provided to see “All Scheduled Posts”.

Dashboard widget of scheduled posts from Future Dashboard Widget plugin.
Dashboard widget of scheduled posts from Future Dashboard Widget plugin.

Note the day, date, time, title and text format with last posting listed first.

Dashboard: Scheduled Posts creates a Scheduled Post widget for the dashboard, but lists the entries in order so that the next post to be published will be listed at the top of the list. A link is provided to see “View All” scheduled posts.

Dashboard widget of scheduled posts from Dashboard: Scheduled Posts plugin.
Dashboard widget of scheduled posts from Dashboard: Scheduled Posts plugin.

Note the title, date and text format with first posting listed first.

I’m going to go with the Dashboard: Scheduled Posts plugin. I like the widget format better than that of the Future Dashboard Widget plugin and the order that the posts are listed seems more intuitive. Thanks Viper007!