Five Useful WordPress Tricks I Just Discovered

I’ve been developing themes and plugins with WordPress for over fours years now and it seems like I learn something new on every project. There are so many useful functions built into WordPress just waiting to be discovered.

Here is a list of my latest finds. I hope these are as useful to you as they are to me.

Paragraph Wrapping

This one is especially helpful when you are working with custom fields. We do not want our end user to use HTML tags when adding content, but we also want to avoid large chunks of unpaginated content.

Luckily the WordPress team provides a built-in function that allows us to format paragraphs and keep our form input free of HTML tags.

Simply wrap your text in this function. Replace $text with the input that you wish to format.

<?php wpautop( $text ); ?>

For Your Reference: WordPress Function Reference

Twitter like post dates

Instead of a boring and useless date you can use “Twitter Like” post dates. Normally this would require some complicated JavaScript or PHP, but WordPress provides a useful function that allows easy relative post dates.

Basically instead of 12/05/2014 we can say “Posted 2 days ago”. This provides your readers with a better sense of how old the content is.

To achieve this we use the human_time_diff( $from, $to ); function. It takes two arguments, $from and $to. In our case, $from is post’s publish date and time and $to is the current date and time.

Add the following code to your post to display relative publish dates.

<?php human_time_diff( get_the_time('U'), current_time('timestamp') ); ?>

For Your Reference: WordPress Function Reference

Organize your custom post type content templates

One of my biggest pet peeves with WordPress Theme Development is file organization. By default all archive and single post templates are stored in the the main theme folder. This is fine for small themes without custom post types and taxonomies, but with a lot of custom archive and single post templates your theme folder will become cluttered.

Unless you add a function to your theme WordPress will not detect any templates stored in subdirectories. Since we want to store them in their own folder we can add the following code to functions.php

<?php 

add_filter( 'template_include', 'custom_template_include' );

function custom_template_include( $template ) {
    $custom_template_location = '/archives/';
    $cpt_tmp = NULL;
    if ( elr_is_cpt_archive() ) {
        
        if ( is_archive() ) {
            $cpt_tmp = get_stylesheet_directory() . $custom_template_location . 'archive-' . get_post_type() . '.php';
        } else if ( is_single() ) {
            $cpt_tmp = get_stylesheet_directory() . $custom_template_location . 'single-' . get_post_type() . '.php';
        }

        if ( file_exists( $cpt_tmp ) ) {
            return $cpt_tmp;
        }
    }
    return $template;
}

?>

Now your theme folder will be nice and clutter free!

Prevent those annoying revisions

Post revisions can be very useful but will quickly clutter your database. If you don’t use them, you can disable them.

This one is pretty easy. Just add the following line to your wp-config.php file.

define( 'WP_POST_REVISIONS', FALSE );

Check URL formats

When working with urls, especially those pulled from a database, you should ensure that they are properly formatted and free of malicious characters.

Simply wrap the url you want to escape in the esc_url(); function to check the url.

$test_url = 'http://testpage.com/test/';
$url = esc_url( $test_url );

There are many more esc functions built into WordPress.

Check them out here: http://codex.wordpress.org/Data_Validation