Using PHP in WordPress Posts, Pages and Widgets!

with 2 Comments

Part Two – A Better Solution!

In Part One, PHP in WordPress Posts, Pages and Widgets! – I discussed that I want to use phpBB3 sessions and session variables in my WordPress Pages. To do this I would need to add PHP script codes to my Post, Pages and a Widget. Part One covered adding PHP script to Widgets via the PHP Code Widget.

In this Part I will cover A Better Way to add PHP script codes to my Pages, Posts and Widgets with Exec-PHP.

WordPress PHP Exec Plugin

PHP Exec 1.7 – When I wrote Part One, I had planed on trying PHP Exec 1.7. However, then I read This Note on the Exec-PHP plugin website

I only found Exec-PHP today!

I also noticed that Exec-PHP may also cover Widgets so everything I did yesterday is of little use! That is, unless I ONLY want to use PHP within a widget!

Therefore, I have decided to try Exec-PHP and shelve PHP Exec for now. By the way, am I the only one struggling with these names? Yesterday we uploaded execphp.php from the PHP Code Widget. Today we were going to upload phpexec.php fron the PHP Exec plugin. Now I am going to first try Exec-PHP! How confusing life can be!

WordPress Exec-PHP Plugin

I have Now decided to try the Exec-PHP plugin first!

Exec-PHP will allow me to type PHP directly into Pages, Posts and Widgets.

PHP Exec 1.7 requires the PHP code to be encapsulated in some XHTML tags that were later, hopefully, correctly stripped away allowing the modified PHP to run. PHP Exec 1.7 only works in Pages and Posts and requires the separate plugin for widgets. I have since started to suspect the PHP Code widget may not be secure. In others words allowing anyone who has access to widgets to run raw PHP code on the server.

From what I read, Exec-PHP does not do that. Likewise, Exec-PHP is reported to have security in both pages and post as well as in widgets.

As my Honeybun and I often remind each other, Life is a Transaction. For every good thing that you get, there is something that you must also give up. In the case of Exec-PHP it appears the WYSIWYG must be disabled. Not a great loss to me but perhaps it would be to someone.

Exec-PHP Works!

Here is the code I tested it with:

[php] <?php
echo ‘Current date and time:<br />’;
echo date(‘l<b\r />dS \of F Y<b\r />h:i:s a’) . ‘ UTC’;
?>
[/php]

And here is the output!

echo ‘Current date and time:
‘;
echo date(‘ldS \of F Yh:i:s a’) . ‘ UTC’;
?>

I also put this into a regular Text Widget and it worked perfectly!

The Cleanup!

Exec-PHP worked so well that I am removing the PHP Code Widget Plugin I installed yesterday in Part One. It is not needed.

The Catch!

The only catch to this was to remember that if you switch from HTML to Visual or vise versa it will really mess up your PHP tags. Do ALL editing of pages with PHP code on the HTML editor and then Update without going to the Visual tab. OR just turn off the Visual tab in the User settings!

Test Platform

This was tested with the Following:

WordPress 3.0.4
Multisite Subdomain Configuration

Atahualpa 3.6.4 Theme
Atahualpa MultiSite Mod

WP Multi-Network Plugin Version 1.1
Exec-PHP Plugin Version 4.9
Page Links To Plugin Version 2.4
SyntaxHighlighter Evolved Version 3.1.1

Information on adding phpBB3 Sessions to WordPress…

Still working on it…

2 Responses

  1. […] PHP in WordPress Posts, Pages and Widgets! […]

  2. […] Using PHP in WordPress Posts, Pages and Widgets! […]

Leave a Reply