Posts tagged "wordpress"

Freeing myself of WordPress

I wanted to simplify everything. I wanted to write posts in Markdown, not HTML. I wanted to save posts as files, not database entries. I wanted to free myself of my dependence on WordPress, PHP, and MySQL in one fell swoop.

So, Mango was born. Mango is file-based blogging software built on Django, the excellent Python web framework. I conceived Mango to scratch an itch, and I'll bet that others out there are itchy, too.

Using HTML5 time element in WordPress themes

I've begun retrofitting this site with HTML5 elements. I'm thoroughly enjoying the process (I love meaningful markup).

One of the first HTML5 elements I've introduced is the time element which, through its datetime attribute, provides a machine-readable version of dates and times.

<time datetime="2009-11-01T16:41:53+13:00">1 November 2009</time>

I wrote a function to generate the machine-readable dates and times for blog comments.

/**
 * echoes comment's date and time in format 2009-11-01T03:41:53+13:00
 */
function comment_datetime()
{
    $comment = get_comment($comment);
    $local = strtotime($comment->comment_date);
    $gmt = strtotime($comment->comment_date_gmt);
    $seconds = abs($local - $gmt);
    $hours = (int) ($seconds / 3600);
    $minutes = (int) (($seconds - $hours * 3600) / 60);
    $output = get_comment_time('Y-m-d\TH:i:s');

    if ($local == $gmt)
        $output .= 'Z';
    else
        $output .= ($local > $gmt ? '+' : '-')
                . str_pad($hours, 2, '0', STR_PAD_LEFT) . ':'
                . str_pad($minutes, 2, '0', STR_PAD_LEFT);

    echo $output;
}

While looping through comments in your WordPress theme, call the above function to print a valid datetime string.

<time datetime="<?php comment_datetime(); ?>">

Looping more than once with the WordPress loop

When I decided to write my own WordPress theme, I thought a good approach would be to duplicate the default theme and go from there. Since that day I have rewritten much of the code. The loop in the index.php file, however, remains unchanged. The loop looks like this:

if (have_posts()) :
    while (have_posts()) : the_post();
        // code
    endwhile;
endif;

As well as displaying the three most recent posts on the home page, I wanted to display links to slightly older posts on the sidebar. I discovered a WpRecipes post on using two different WordPress loops which suggests adding the following line of code just before the loop:

query_posts('showposts=5&offset=3');

The offset ensures that posts do not appear in both places.

WordPress login redirect

Sometimes we require users to log in to a WordPress site in order to access front-end functionality hidden from guests. In such instances, we can simply provide a standard login link:

<a href="<?php bloginfo('url'); ?>/wp-login.php">log in</a>

While this gets the job done, it takes users to the dashboard after they have logged in: they must then click on a link to return to the front-end, at which point an additional click may be required to get them back to the page they were viewing. Since WordPress 2.6.2 it has been possible to circumvent this round trip from origin to wp-login.php to wp-admin/ to / and finally back to origin by including a value for redirect_to in the href:

<a href="<?php bloginfo('url'); ?>/wp-login.php?redirect_to=<?php echo urlencode($_SERVER['REQUEST_URI']); ?>">log in</a>

The above returns users to their starting point after they've logged in.