10 Incredibly Useful WordPress Customizations

Aug04
10 Incredibly Useful WordPress Customizations

WordPress is the most versatile and popular blogging software on the internet today, but out of the box it doesn't always do what you would like it to. Fortunately, many hacks have been found to whip it into shape and get even more functionality out of it for your own personal use.

In this article, we’ll show you 10 useful WordPress customizations along with explanations as to how they work to get more functionality out of this awesome blogging engine.

1. Create a Send to Twitter button

Tweet This

Twitter only allows tweets to be 140 characters, and that's usually never enough to display a long post title from WordPress. Here's how to create a button to make your long url in to a tiny one.

Open up your functions.php file in your WordPress theme and create a "getTinyUrl" function to automatically send your link url to tiny.url when clicked.

Links with a Napoleon Complex

<?php function getTinyUrl($url) {
    $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
    return $tinyurl;
}
?>

Now that we have a function to pass our url to tiny.url we can set up our button in our theme. Create your link and add the WordPress function for the title and call your getTinyUrl() function. You can see this link in action at the bottom of the page in the "Enjoy This Post" section.

Tweet my link, I dare you.

<a href="http://twitter.com/home?status=Currently reading <?php the_title(); ?> : <?php $turl = getTinyUrl(get_permalink($post->ID)); echo $turl ?>" title="Tweet this">Tweet This</a>

2. Create A Maintenance Page For Your WordPress Blog

maintenance page

WordPress doesn't have an option to temporarily redirect users to a maintenance page when updates, design problems are being fixed or tweaks are being done to your site. You may not want your users to see this, so we'll use the .htaccess file to redirect uses to another page.

First create your maintenance page, then open your .htaccess file (usually located in the root of your server at the root of your wp installation) and create a backup just in case things go awry. Now enter the following code into your .htaccess file and change two to your maintenance page url, then enter the ip address or addresses that you don't want to have re-directed on the third line exactly as they are formatted in the script.

Manipulate the apache webserver to do your bidding

RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
RewriteRule $ /maintenance.html [R=302,L]

Now an explanation of the code that was entered into the .htaccess file. The first line tells the server to process the script below it. Line two tells our Apache web server to make an HTTP request to the specified parameter, in this case, our maintenance page. Line three specifies the ip address it should ignore for the request and the last line tells our server to serve up our maintenance page to any computer that isn't our specified ip address.

3. Automatically Retrieve The First Image From Posts

wordpress thumbs

When uploading images for posts, WordPress automatically re-sizes them and creates a thumbnail. If you would rather grab the first image from the post and use that as your post thumbnail instead of manually entering it into a custom field, here's how.

Open your functions.php file and enter the following code. Make sure you have created a default thumbnail in case your post doesn't have one.

Catch That First image

<?php
function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img .+src=[\'"]([^\'"]+)[\'"].*/>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Define a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}
?>

Save your functions.php file and open your index.php or home.php file and call the function like this:

<image src="<?php echo catch_that_image() ?>" title="First image in the post" />

The function will parse the images on the page and retrieve the first one, if no image is found it serves up the specified default image.

4. List The Most Popular Posts Without A Plug-In

popular posts

Displaying the most popular posts is a good way to make visitors stay longer on your website, as is displaying related posts. There are many plugins to this, but what fun is a plug in when you can hack WordPress instead?

Simply enter the code below into your sidebar.php file and change the 5 to however many posts you would like displayed.

The List of Popularity Contest Winners

Popular Posts

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5"); foreach ($result as $post) { setup_postdata($post); $postid = $post->ID; $title = $post->post_title; $commentcount = $post->comment_count; if ($commentcount != 0) { ?> <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"> <?php echo $title ?></a> {<?php echo $commentcount ?>}</li> <?php } } ?> </ul>

The above code executes a WordPress database query to get a list of the five posts with the most comments. The results are then wrapped in an unordered HTML list and displayed on screen.

5. Use Shortcode to Display Related Posts on a Single Page

related entries

There are endless amounts of plug-ins to display related posts in your blog, once again, what fun is a plug-in when you can add your function?

If your functions.php file is still open from the rest of the hacks, enter the code below and save it. You can change the number five in line three of the code to the amount of posts you want displayed.

Shortcode: Easy, Breezy, Beautiful

function related_posts_shortcode( $atts ) {
	extract(shortcode_atts(array(
	    'limit' => '5',
	), $atts));

	global $wpdb, $post, $table_prefix;

	if ($post->ID) {
		$retval = '
    '; // Get tags $tags = wp_get_post_tags($post->ID); $tagsarray = array(); foreach ($tags as $tag) { $tagsarray[] = $tag->term_id; } $tagslist = implode(',', $tagsarray); // Do the query $q = "SELECT p.*, count(tr.object_id) as count FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < NOW() GROUP BY tr.object_id ORDER BY count DESC, p.post_date_gmt DESC LIMIT $limit;"; $related = $wpdb->get_results($q); if ( $related ) { foreach($related as $r) { $retval .= '
  • '.wptexturize($r->post_title).'
  • '; } } else { $retval .= '
  • No related posts found
  • '; } $retval .= '
'; return $retval; } return; } add_shortcode('related_posts', 'related_posts_shortcode');

Once you have the functions file set, enter the shortcode into your post page and save it. What the code does is selects the specified number of posts from your WordPress database and displays them in a generated un-ordered list.


[related_posts]

6. Separate multiple author comments from the others

author style

When you have a multiple author blog, you can enable styling each authors comments differently with a simple "if" and "else if" statement so each one has it's own unique style.

Bling your author comments

  • This code checks the comment loop for the specified email addresses and displays a variable based on that email address, so when more than one author shows up on a page, they all have different looks to their comments.

    7. Automate the year change in your copyright footer

    new year

    Opening up your footer.php file every first of the year and editing it is just one more of those little things that you just don't want to have to do. Why not make it change automatically? Adding the php code below will display the year in your footer and change when the year does. No editing required.

    Party like you don't have to do this every year

    Copyright © <?php echo date('Y') ?> Your Site.
    

    8. Disable Search Engine Indexing Posts Using Custom Fields

    no follow

    This could be a trivial one, but there might be an occasion where you want to keep certain posts off the search spider radar. Inserting a custom code in your header.php file and using the custom fields in WordPress posts can add this functionality easily.

    Robot Repellent

     <?php $cf = get_post_meta($post->ID, 'noindex', true);
        if (!empty($cf)) {
        echo '<meta name="robots" content="noindex"/>';
    }?>
    

    When creating your post, simply add a custom field named "noindex" (or whatever you wish) and leave the textarea next to it blank.

    9. Remove WordPress Smart Quotes / Curly Quotes

    quotes_dumb

    Smart / curly quotes might look nice typographically, but they are be a major pain if you want to copy and paste source code. Open your functions.php file and paste in this code to remove the WordPress default formatting.

    Ixnay on the smart curls

    <?php remove_filter('the_content', 'wptexturize'); ?>>
    

    If you feel like getting a little more daring, add this code to remove them in the comments section.

    Ixnay on the comment smart curls

    <?php remove_filter('comment_text', 'wptexturize'); ?>
    

    10. Disable Automatic Post Saving

    auto_draft

    WordPress automatically saves posts in timed increments. This can gunk up your database, cause posts to get reverted if something happens in your browser, etc. Paste this code into your functions file to stop the automatic debauchery from happening.

    function disableAutoSave(){
        wp_deregister_script('autosave');
    }
    add_action( 'wp_print_scripts', 'disableAutoSave' );
    

    Author: Jason Pant

    Hi, I'm Jason. A front end developer from Grand Rapids, Michigan, and founder of VagrantRadio. I enjoy working with Css, jQuery, Php, learning new techniques in Photoshop and you should follow me on Twitter or Google Buzz.

    Enjoy this post?

    If you enjoyed this post, please take a second to promote it via the various social bookmarking sites below. Don't forget to subscribe to the RSS Feed for more interesting articles. Thanks for visiting!

    submit to delicioussubmit to diggsubmit to facebooksubmit to redditsubmit to stumbleuponsubmit to twittersubmit to mixxAdd to Google Buzzsubmit to Design Moosubmit to designfloatemail this someone

    2 Comments

    1. Tnelson

      Hey, I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog!…..I”ll be checking in on a regularly now….Keep up the good work! :)