A simple PHP widget for WordPress

So my job this evening was to recreate a little PHP script I wrote a little while ago that would automatically display the streets that a church is praying for in any given month. It’s actually very simple to do, though you will need to install a plugin in WordPress as the standard Text widget doesn’t work with PHP.

Installing the widget

Click on Plugins and then Add new. Search for Executable PHP widget by Otto. Once installed go to the Widgets page. Drag and drop PHP code to the desired sidebar. Enter a title.

The Code

<p>This month we are praying for:<br/>
<?php
$prayermonth = date("m");

switch ($prayermonth)
{
case 1:
echo "January Streets";
break;
case 2:
echo "February Streets";
break;
default:
echo "everywhere";
}
?>
</p>

You don’t really need to see all the months to get the idea of what’s going on.

Editing month data

Look for the numbers that correspond with the month number and edit the text within the speech marks. Don’t delete the speech marks or the widget will display an error message. In fact it is a good idea to copy all the text in this box to a separate text editor (with my standard plug for Notepad++) and leave it there until you are sure everything is working.

When you have finished editing the streets, click on the Save button at the bottom of the widget. A small circle will appear while it is saving. This is the only sign that anything is happening. Before doing anything else, open another tab in your browser and check that it is working. If it is you can move on to something else. Otherwise copy and paste the text from the text editor back into widget box and start again (You did make that backup step didn’t you?)

How it works

This is about as simple as it gets with PHP and it’s a good example of how PHP and HTML work together. Remember that PHP is executed by the web server not the browser (in contrast to Javascript), so you won’t see any of the PHP code if you look at the source code of the home page. Here goes…

<p>This month we are praying for:<br/>
Well this is pretty straightforward. We start the paragraph, insert the text that never changes ending with a line break

<?php
This tells WordPress (and the web server) that some PHP is coming up

$prayermonth = date(“m”);
The creates a variable called prayermonth. The $ symbol marks this as a variable. Rather than give it a single value, we use the date function to give the current month number with “m”. Note the ; at the end of the line. As an aside, I’m assuming that month is a reserved word as that didn’t work.

switch ($prayermonth)
{
This is the clever bit. A switch statement is like an If … ElseIf… statement but a lot easier to work with when there are lots of variables; twelve in this case. It looks at $prayermonth and then does different things depending on what the variable contains. For example….

case 1:
echo “January Streets”;
break;
This is what happens if prayermonth contains 1. It prints January Streets into the widget. That’s what you’ll see in the web browser.The break command tells the server to skip to the end of the switch statement

default:
echo “All of Copthorne”;
A good idea to include, the default case is what is displayed if there is no matching case. That’s unlikely with this script but that’s no reason not to foster good habits.

}
?>
</p>
The { finishes the Switch statement. The ?> ends the PHP code and the </p> closes the paragraph

 

Respond to this post