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!

Twenty Ten Child Theme Mods

Creating WordPress child themes is a slick way to make your own theme. All it takes are a few easy steps. Here’s an example using the standard Twenty Ten theme as the parent theme:

  • modify the child-stylesheet to make things look how you want
  • create child-directory for new child theme
  • add template files to child-directory, if you wish
  • upload child-directory to /wp-content/themes/
  • activate new child theme

The child-stylesheet must be saved with the name “style.css” in the child-directory. The child-stylesheet header (which has to be at the top of the sheet) must contain a few lines that identify its parent theme, like so:

You can add a line for the Theme URI: and Author URI:, if you like. Only the Theme Name: and Template: lines are required, the others are optional.

The @import rule indicates the directory of the parent theme and the location of the stylesheet. All you have to do is put in the new css rules below the import line.

That’s it!

If modifying the stylesheet doesn’t quite get all the changes you want, realize that you can add any template file to the child-directory and that will over-ride the parent file with the same name. For example, say you want to stick in a block for an advertisement right under the header image. Copy header.php and add a new <div> at the bottom of the page just below <div id=”main”> and stick the ad code in this division. Upload this new header.php into the child-directory and it will replace the one from the parent template.

Activate your new child theme and see how it looks!

Mass Delete Spam Comments in WordPress

So, the spam is getting out of control on your WordPress blog? Even if you have activated the Akismet plugin – and you should – SPAM may keep filing up the comments.

You can just let it ride and those comments identified by Akismet as spam will silently disappear in a month’s time. In the meantime your database will be getting bigger and bloated with thousands of lines of useless information. Will this bog down your blog? Perhaps it will.

I found another reason to manually delete all the spam comments on one of my WP blogs. From the WP Dashboard I saw 1000+ spam comments and went to edit the comments. When I clicked on ‘spam’, the anti-virus software on this computer, AVG, detected a threat on the spam comments page and blocked my access to it. Even after shutting down the browser and returning to the edit spam comments page another day, the AVG software wouldn’t let me in. How could I delete the spam if I can’t get to the page to do it? Even if I could get to that page it would take a long time to delete so many comments. There must be a better solution to getting rid of spam comments in WordPress.

So, how do you mass delete spam comments in WordPress? Check out this excellent video that provides a solution using phpmyadmin and your WordPress blog database.

Steps to follow:

  1. sign in to CPanel
  2. open up phpmyadmin and select the WP database
  3. click on “wp_comments” table and browse to see the comments
  4. backup database by clicking on database name in left column, click on export tab, select all tables under Export, select SQL, check “save as file”, click Go to download database backup to your computer
  5. select comments table, by clicking on the table name wp_comments, then click on the SQL tab
  6. use an sql query to mass delete spam comments in WordPress by typing the following in the Run SQL Query box:

    DELETE FROM wp_comments WHERE comment_approved = 0

    Click Go, click OK.

  7. Comments deleted!

If you are using a spam catcher, like Akismet, the value for comment_approved may be set to “spam”, so you’ll need to alter the query as follows:

DELETE FROM wp_comments WHERE comment_approved = ‘spam’

Don’t forget to use the single quote marks to enclose the word spam.

Verify that the number of records in the comments table has been reduced to the number of approved comments. Hooray!

Optimize the WordPress database by going to the Structure tab, or clicking on the database name in the left column, and at the bottom of the table listing click on “Check tables having overhead”. Choose “optimize table” from the spin box on the right and all the extra space that those nasty spam comments took up will be released and the database optimized.

Go back to database view and verify that the overhead space has been removed. The size column now represents the space that is actually used by the database.

Refresh your WP blog and the spam comments will be gone. Isn’t is great to see ZERO spam comments?!

Alternatively, you can catch spam before it lands in your WP blog by modifying the function.php file in your theme using these excellent directions from Tejaswini.

Also, try Soumen Halder’s list of ways to reduce comment spam on WP blogs.

Reblog this post [with Zemanta]