Posts Tagged php

Fixing your theme so that it works with WordPress.com Stats…

WordPress.com Stats is one of the most popular WordPress plugins.  I have tried using it in the past, but it never collected stats.  I never actually pursued a solution to this because I was using another stats plugin that was partially functional and able to give me most of the information I wanted. 

Today I spent a little time searching the WordPress support forums and found that my theme was missing a function call that the plugin relies on. 

Simply put, in your theme’s footer.php file, right above your </body> and </html> tags, you need to add:

<?php wp_footer(); ?>

And that’s all there is to it. You can be sure that this is safe to add because every theme is supposed to have this line. wp_footer() doesn’t generate any output so you shouldn’t see any changes to your site’s display.

If it still doesn’t work, be sure you are testing it while logged out. (You can always use another browser to save time.) It will not register hits from a logged in user. Failing that, if you’re using the WP-Super-Cache (as you should be) plugin, try clearing your cache.

, , ,

3 Comments

How To Execute PHP code in a PHP String (and return the output to another string)

Ever thought “Gee, it would be nice if I could store some PHP in a database and execute it at will?”

Well you’re in luck my friend, PHP was thinking of you when they created the eval function.

The syntax for eval is very simple:

eval ( string $code_str );

Unfortunately, the output of eval does not work like you might expect it to.  You can’t, for instance, do this:

$string_awesome = eval(string $code_str)

The string will remain empty and eval will function as though it weren’t at the right hand side of an assignment.  This is because of the way eval works. Eval is not intended to deal solely with code that outputs something to the screen. It simply executes the provided code inline with the rest of the code. It can share variables, functions, and anything else with the main body. So the following would work just fine:

$number = 1;
eval ("\$number = \$number + 1"); 
echo $number;

The preceding would output 2. Notice that it was necessary to escape each dollar sign.  Eval requires a pure PHP string.  Any normal PHP operations have to be escaped out, including single quotes, double quotes, dollar signs, etc or it will not work properly. 

Since eval is not primarily concerned with output, it doesn’t return any.  In order to capture the output to a string you have to do something called output buffering. Output buffering simply captures any output of desired PHP statements and allows you to manipulate it before display (or even choose not to display at all).  It has many applications beyond the scope of eval.   Output buffering uses a variety of  functions. We will demonstrate most of them in the following examples.

Back to our original example, let’s assume we have a string of PHP code which we are going to execute via an eval statement and we would like any output to be captured to a string.  To do this, we’d simply run:

ob_start();
eval("echo \"This is some really fine output.\"");
$this_string = ob_get_contents();
ob_end_clean();

Simple enough right? With ob_start, we…start output buffering. We use ob_get_contents to capture the collected output to a string, and we use ob_end_clean to exit the whole thing nicely. The value of $this_string will be “This is some really fine output.”

But what if we had two eval statements to run and wanted two different strings?   Well, we  could just run the above code twice, but PHP provides a function called ob_get_clean that simplifies the task.  The resulting code looks like:

ob_start();
eval("echo \"This is some really fine output.\"");
$this_first_string = ob_get_clean();
eval("echo \"This is some more fine output.\"");
$this_second_string = ob_get_contents();
ob_end_clean();

Using ob_get_clean for the first part of the capture scrubs the output buffer and allows it to collect more output without restarting the whole process.  This results in cleaner code (and I expect faster executions). 

There is one more trick in the output buffering bag that may prove useful. If you want to capture the output of an eval to a string but also display it as if it were simply being executed inline, you can simply use one of the flush functions.  Flushing sends the output buffer along its merry way, as if it were never captured.  It also cleans the output buffer so it is important to run one of the ob_get* functions before we use flush.  PHP even provides variations of the core functions using flush:

ob_start();
eval("echo \"Some awesome output. \"");
$this_string = ob_get_contents();
ob_end_flush();

The preceding code will output the output buffer and also store it in $this_string. You can also use ob_get_flush or ob_flush to further simplify this depending on your particular application.

, ,

3 Comments

Random Quote Script in PHP

There are many random quote scripts out there, but not all are created equal. This morning I decided I wanted to create a random quote generator for my own amusement. Specifically, I wanted a page that would return a random proverb from Puddn’head Wilson’s Calendar of Mark Twain’s The Tragedy of Puddn’head Wilson.  A quick Google search seemed to indicate there was no single place to retrieve these quotes.  I decided to place all of the quotes in a single text file and place a pound sign (#) between each of them as a delimeter.

For example:

Whoever has lived long enough to find out what life is, knows how deep a debt of gratitude we owe to Adam, the first great benefactor of our race. He brought death into the world.  - Pudd'nhead Wilson's Calendar
#
Adam and Eve had many advantages, but the principal one was, that they escaped teething.  - Pudd'nhead Wilson's Calendar
#
... 

Most random quote scripts have you manually create an array of strings. Such as:

$quotes[] = "This is a quote"; 
$quotes[] = "This is another"; 

But, this is extremely inefficient from an end user perspective. And it’s not easy to add quotes. Instead, I chose to read in the file as one string and split it up based on my chosen delimeter (#).

The resulting code ended up being very simple:

$quote_repository = "wilson.txt";
$quotes_raw = file_get_contents($quote_repository);
$quotes = split("#",$quotes_raw);
 
srand ((double) microtime() * 1000000); 
$r = rand(0,count($quotes)-1); 
echo "<p>".$quotes[$r]."</p>";

To shortcut, the following would also be acceptable:

$quotes = split("#",file_get_contents("wilson.txt"));
srand ((double) microtime() * 1000000); 
$r = rand(0,count($quotes)-1); 
echo "<p>".$quotes[$r]."</p>";

And that’s all there is to it.  It pulls in all of the quotes, splits them into arrays, and displays a random one each time the script is loaded.  It’s elegant and extensible. Click here for a random Puddn’head Wilson quote.

, ,

3 Comments

Creating and Editing Images in PHP

Yesterday I promoted a dynamic badge I created for America Held Hostage.  Today I’m going to explain how I went about creating this badge.

First, I designed my badge in Photoshop:

Couldn’t I have just used PHP to create this image? Well, of course I could have. But that wouldn’t be a responsible use for resources either system or development. The essentials of the badge won’t change and making it all dynamic is silly.

Now that we have the badge, we simply want to add text to it using PHP. In this case, we’re going to add a countdown till January 20, 2009.

Read the rest of this entry »

, ,

2 Comments