Early this morning, I released Top 10 – A Page Counter and Popular Posts plugin for WordPress. I detailed my experience while designing this plugin. The major problem that I faced while working with this plugin was with WP Super Cache. WP Super Cache creates a static file of your post and displays this to the viewer. As a result, any PHP code that is present in your WordPress blog template is executed only once and the cached file is then served to your visitor.
The initial version of Top 10 used PHP to record the page views as well as display them and so using WP Super Cache ensured that both of these would never increment.
Here’s how you go about it.
The most important part of the file is:
The file does nothing other than print the date on the screen. You don’t even need the function for this. However, it serves as a basic structure we can build upon.
Now, why would you use PHP if you couldn’t pass parameters. e.g. take a look at this code:
We’ve passed a parameter id=1302 to our PHP file. Within the PHP file we will need to capture the parameter. Let’s take a look at the modified PHP file.
$_GET to retrieve the parameter id. You can pass multiple parameters to the PHP file.
If you’re interested in live code then download Top 10 and take a look at the source code.