This is a guest post from Margaret Saunders who is a web hosting consultant for IXwebhosting, a web hosting company based in Ohio.
Of all of the content management systems available for free on the Internet, WordPress has consistently been shown to be one of the best over and over again. While WordPress has thousands of themes and plugins that can be used to customize a website and provide additional functionality, sometimes webmasters will want to do something that there isn’t a plugin for, or they will want to do it by themselves without having to add a plugin. In these instances, they have to turn to WordPress hacks. What follows is a list of some of the most popular and most important WordPress hacks.
WordPress Hack #1: List recent comments.
There are a few plugins that allow webmasters to display the most recent comments posted on their WordPress sites, but these plugins are usually confined to the sidebar. With a WordPress hack to list the recent comments on a site, the recent comments can be shown anywhere on the page, including inside of a post. Add the following code to your functions.php file.
<?php function recent_comments($src_count=10, $src_length=60, $pre_HTML='<ul>', $post_HTML='') { global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, SUBSTRING(comment_content,1,$src_length) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT $src_count"; $comments = $wpdb->get_results($sql); $output = $pre_HTML; foreach ($comments as $comment) { $output .= "<li><a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"on " . $comment->post_title . "\">" . strip_tags($comment->com_excerpt) ."...</a></li>"; } $output .= $post_HTML; echo $output; } ?>
Whenever you want to add the recent comments, just add this code:
<?php recent_comments(); ?>
WordPress Hack #2: Pull the first picture from posts.
WordPress’s home page, the search pages, and the archive pages are all capable of displaying only a small portion of a number of posts in a list. While there is no built-in option for these lists to automatically display the first image from each post, it can be easily done with a WordPress hack, and it looks great. To implement this hack you need to edit your functions.php file in your theme and implement this code:
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)){ $first_img = "/images/default.jpg"; } return $first_img; }
In case you have a post that does not have an image, don’t forget to implement a default image on line 10. Now, you can simply call the function within the loop to display the first image from the post.
<?php echo catch_that_image() ?>
WordPress Hack #3: A print button on posts and pages.
Lots of plugins are available that give someone the ability to easily print the content from WordPress posts and pages. However, a lot of these plugins subject the readers to advertisements. Instead of imposing this on the readers, it can be a better option to use a WordPress hack to create a print button that will take the reader directly to the print dialog window. To do this open your single.php file (for individual posts) from the theme folder and add the following code:
<a href="javascript:window.print()" rel="nofollow">Print this Page</a>
WordPress Hack #4: Automatically include the author’s information with each post and page.
On article directory sites, there is often a resource box at the bottom of each article that gives a brief description of the author of the article. Along similar lines, it’s possible to make a box of information about the author appear with every post or page on a WordPress site using WordPress hacks. For this you’ll need your Style sheet and add this to it:
.postauthor { }
Then you’ll need to go through your index.php file within your theme and look for a similar code like this:
<?php the_content('Read the rest of this entry »'); ?>
Above this code you implement the following:
<div> <?php the_author_description(); ? > </div>
All that is left to do now is to add your own style to format the about author box.
WordPress Hack #5: Buttons to send posts to Facebook or Twitter.
The two most popular social networking sites are Facebook and Twitter. Because of their popularity, it’s very important that a website is integrated with both platforms. With a WordPress hack, buttons can be added to send a post or page from WordPress directly to Facebook or Twitter without having to configure some annoying plugin. For the Facebook button add this to the loop in your single.php file in your theme.
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>" target="blank">Share on Facebook</a>
For twitter add this to your loop:
<a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Click to send this page to Twitter!" target="_blank">Share on Twitter</a>
In Conclusion
Just because there is a plugin for a feature in WordPress, that doesn’t mean that, it’s good enough to make people want to use it. Many people enjoy doing things themselves so that they have full control over what is added to their websites. These people often prefer WordPress hacks over WordPress plugins because of the sense of doing it themselves and accomplishing something important. The six plugins listed here are good examples of people taking matters into their own hands when it comes to their WordPress websites.
For further questions and suggestions don’t hesitate to contact me.
Margaret Saunders
margaret.saunders@ixwebhosting.com
Really cool explanations.. 🙂