PHP in WordPress Posts, Pages and Widgets!

with 1 Comment

I am getting ready to enhance my WordPress site using phpBB3 Sessions variables.

Of course, to do this I wanted a quick and easy way to add PHP code into WordPress Posts, Pages and Widgets. This would allow me to display or hide a number of things based on if a user was logged into my phpBB3 forum. It would also allow me to login to phpBB3 from inside of WordPress as well as use any of the phpBB3 user information in post, pages and widgets! Ultimately, I want people to use WordPress to register for my phpBB3 forum (and for the members only information in WordPress too)!

My goal is NOT complete WordPress phpBB3 integration. For example, logging into WordPress will NOT log you into phpBB3 or vice versa. I did NOT want this for security reasons. The forum is for member posting and the CMS / blog use of WordPress is restricted for just a few selected people to post.

As the phpBB3 sessions project develops, I will post below the links to it.

This post is about easy ways to include PHP code into the body of Posts, Pages, and Widgets.

PHP Code Widget

WARNING: BEFORE CONTINUING YOU MAY WANT TO READ
PART TWO!

In Part Two I discuss why I may NOT be using the PHP Code widget plugin.

My first priority was to add PHP to a Widget. To my delight I found a plugin called PHP Code Widget!

http://wordpress.org/extend/plugins/php-code-widget/

It is super simple to install…

  1. Download the Widget Plugin
  2. Unpack it (unzip it)
  3. Upload execphp.php to the /wp-content/plugins/ directory
  4. Activate the plugin through the ‘Plugins’ menu in WordPress
  5. Use the widget like any other widget.

It works just like the Text widget, but also allows working PHP code to be inserted. For example, if you put this in your PHP Code widget:

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

Would look something like this in your side column:

=================
Testing

PHP Code

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

=================

Globals and Scope

You need to keep in mind that this is run inside of a function. Therefore before you use a global variable you need to declare it first. Something like this…

[php] <?php
global $my_global_var ;
echo $my_global_var ;
?>;
[/php]

Part 2

That is enough for this Post. In Part Two I will deal with adding PHP into Pages and Posts.

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
Executable PHP widget (PHP Code Widget Plugin Version 2.1)
I am now recommending Exec-PHP ~ Read Part Two!
Page Links To Plugin Version 2.4
SyntaxHighlighter Evolved Version 3.1.1

Information on adding phpBB3 Sessions to WordPress…

Still working on it…

One Response

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

Leave a Reply