Category: Development

  • The WordPress Loop

    I often get asked about “the loop” in WordPress, as many of my plugins require knowledge of it for successful integration.

    “The loop” is simply some WordPress code that handles the fetching of post or pages. Each WordPress theme has a number of files that handle different types of output – for instance, index.php is used for displaying a number of posts, page.php displays pages and single.php shows a single post. Each of these contains the code for the loop.

    The first line is usually this…

    <?php while (have_posts()) : the_post(); ?>

    And the last line is usually…

    <?php endwhile; ?>

    Everything in between is the setting up and displaying of the post/page in question.

    So, let’s use Simple Social Bookmarks as an example. You will probably want to put the function call for this just before that last line of the loop – that will it will be the last thing displayed after each post or page. However, you may want other content to appear first, in which case you will choose a different location between the above 2 lines to put it.

    What I can’t tell you is exactly where you need to put the code – that’s entirely down to you and how you wish the output. Indeed, there’s no reason why, in the case of Simple Social Bookmarks, you have to put it in the loop – you could have one at the top of each page. Bear in mind, however, that if you put code outside of the loop then it may not be able to get certain information – in out example, you’d have to supply a URL, as normally this is supplied by the loop to indicate the current post/page.

    I hope this makes sense. If not, you can read more about it at WordPress.org.

  • Damn you WordPress – 3.0 launches!

    WordPress 3.0 has been launched. And it’s late. And I’m busy all of tomorrow. And the weekend.

    I’m not going to risk the install now – sods law says it will go hideously wrong.  I could try the install whilst I’m at work but their wireless network is so slow it’s nearly impossible to use.

    Damn you WordPress (shakes fist at sky).

    (More details tomorrow)

  • Adding a Heat Map to WordPress

    ClickHeat Screenshot

    Wanting to learn a bit more about the visitors to this site, I thought installing a heat map on the site would be a good start.

    I initially used PicNet Mouse Eye Tracking – this is free, but an advert for the service appears at the foot of your site. However, I soon came across the OpenSource ClickHeat. You can use this on any kind of site and is a PHP-based installation.

    After much gnashing of teeth, here’s how I got it installed…

    • Download the latest version of ClickHeat and unzip it
    • Install this into a folder on your server
    • Now, from the address bar of your browser, open up the folder. e.g. If you installed it to www.mysite.com/clickheat, you’d go to this directory in your address bar.
    • This will start the installer. I found errors straight away – make sure the config, log and cache folders are writeable.
    • Done. You simply need to add the code to start tracking – return to the ClickHeat folder to access your heat map and make settings changes.

    To test it’s working, simply append ?debugclickheat to the end of any of your sites URLs.

    I added the JavaScript (which is generated from the ClickHeat admin screen) directly to my footer.php file in the theme folder, wrapping it in a check to ensure that an admin isn’t signed in (so it doesn’t track my clicking!).

    One more problem I cam across – in the ClickHeat admin screens I kept getting the following error…

    Warning: ob_start() [ref.outcontrol]: output handler 'ob_gzhandler' cannot be used twice in /xxx/clickheat/index.php  on line 51

    Obviously, I’ve replaced the directory path with “xxx” for security reasons.

    After some searching around, I decided the best solution was to make a modification to the ClickHeat code itself. Open up the index.php file within the ClickHeat folder and attempt to find the line that starts with the following…

    if (@ini_get('zlib.output_compression')

    Before this line, add the following…

    ob_end_clean();

    And that’s worked for me.

    I’ll report back on how I get on with ClickHeat.

  • WordPress – Making Post/Page Content Expire

    WordPress – Making Post/Page Content Expire

    There are some excellent plugins available to allow you to make time sensitive posts and pages expire. However, these work for the entire post/page rather than just a section.

    Here’s the simple solution – a shortcode that you wrap around some content and allows you to specify a date on which it should expiry and simply not display anymore.

    (more…)

  • Adding information to your WordPress dashboard

    Dashboard Screenshot

    If you wish to add a widget to your WordPress dashboard then that’s a different, and longer, discussion. Instead I’m going to show you how to add a simple message, of your choosing, to the “Right Now” box.

    How could you use this? You could use a database lookup beforehand, for instance, to report on useful blog/theme information. I’m using it right now to report on how my competition is running – I’m performing a count on the table and reporting the output back (see image to the right).

    First of all, you need to add an action for activity_box_end pointing to your new function. All of the code discussed here should be put in your theme’s functions.php file. In this example I’m pointing it to a function named show_admin_message

    add_action('activity_box_end','show_admin_message');

    Within your function you then simply need to output the HTML. Here’s a complete piece of code that you can use…

    add_action('activity_box_end','show_admin_message');
    
    function show_admin_message() {
       $output_text = "Put your output message here";
       echo '<div style="border: 1px solid #ffd700; margin: 10px 0 0; padding: 5px; background: #ffffba; color: #000;">'.= __($output_text).'</div>';
    }

    This will display the text “Put your output message here” in a yellow box, with a darker yellow border. The text will be in black.