Most Wanted WordPress Tips, Tricks, and Hacks

There are times when you come across a feature in a blog, and you just start thinking to yourself: How can I get this in my WordPress blog/site as well. Everybody have experienced this feeling. Sometimes you know you want it, and don’t know where to look for, or even what to look for. In this article we will be sharing some of the most wanted WordPress Tips, Tricks, and Hacks that you will definitely find useful.
These tutorials are classified under various skills level. For some tutorials, you will need to know basic HTML and some WordPress Knowledge. Use the link below as one of your resources:
WordPress Theme Cheat Sheet for Beginners1. How to use a Custom Page as a Home Page in WordPress
This is one of the most wanted hacks that users want to know how to accomplish. First you need to learn how to create a custom page. You will need to duplicate your page.php or create a brand new .php file and add the following code at the very top:
<?php /* Template Name: WPBeginnerT1 */ ?>
You can change the template name. Change any styling, that you want in that page. Go to your WordPress admin panel and create a new page and select this template.
Custom Page Template
Once you have published this page go to Settings » Reading in your admin panel.
Custom Homepage in WordPress
And select your page to be the homepage. Now you have yourself a CustomHome Page.
2. How to Create a Page that Displays Random Posts
Have you ever been to a site and saw this cool feature? They have a link in their top navigation to something like Stumbe! or Read Random Articles, or some other creative text. When you click on that link, it takes you to a page that displays one random page. Each time you refresh, you are delivered with a new post. Well this trick is just for you then.
You would need to follow the trick #1 in this article to create a custom page template. And simply paste this code in there:
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
This is a simple WordPress Loop that is running a query to display random posts and the number 1 in there is telling WordPress to only show 1 post. You can change that number, but most of the time people do it one post a time.
We have a Quick Reading Page on our site as well, so you can see this trick in action.
3. How to Display any External RSS Feed on Your Site
Display any External RSS Feed in WordPress
Have you seen other bloggers who display their other blog’s feed on their site. You want to do it too for extra promotion and traffic. Well here is the tutorial for you. Simply paste the following code in anywhere in your theme:
<?php include_once(ABSPATH.WPINC.'/feed.php');
$rss = fetch_feed('http://feeds.feedburner.com/wpbeginner');
$maxitems = $rss->get_item_quantity(5);
$rss_items = $rss->get_items(0, $maxitems);
?>
<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo $item->get_permalink(); ?>'
title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
<?php echo $item->get_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
4. How to Display Relative Dates in WordPress
Display Relative Dates in WordPress
Have you ever seen this in blog posts and comments and wondered how did this blogger manage to do this? Actually it is pretty easy.
You would need to download a plugin called WP-Relative DateOnce you have downloaded and activated the plugin, look in your single.php, index.php, and page.php for this code:
<?php the_date(); ?>
Replace it with:
<?php relative_post_the_date(); ?>
5. Allow Users to Submit News on Your Site
Submit a News in WordPress Site Feature
This trick is being adapted by many top sites to keep a useful resource section in their sidebar which is maintained mostly by site’s audience. Did you want to know how you could do that?
First you will need to download a plugin called TDO Mini Forms and followdirections.
6. Link to External Links from Your Post Title
Did you see other sites link to external posts from their post title? Well that is because it is completely useless to create a new post where inside you are going to tell users to go to another site to read it. You are wasting your user’s time. This trick will allow you to link to external links from your post title in WordPress.
First open your functions.php file and add the following codes in there:
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
}
These codes must be placed in php tags.
Then open your index.php and find the following code:
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
And replace it with:
<?php print_post_title(); ?>
This can also be accomplished by a use of a plugin called Page Links To.
7. Use WordPress as a Free Email Newsletter ServiceFree Email News Service using WordPress
Did you see other sites using Email Newsletter services? Well you can make your WordPress work this way as well. Simply follow this tutorial.
8. Display any Number of Posts in a Loop
Did you ever want to display a different number of posts on different pages. For example on your category pages, you want to display 10 posts which you can control from your Admin panel, but another page you want to show only 5 posts. Well this tutorial is just for you.
Open a Template file where you want to display an x number of recent posts:
// if everything is in place and ready, let’s start the loop
// to display ‘n’ number of posts, we need to execute the loop ‘n’ number of times
// so we define a numerical variable called ‘$count’ and set its value to zero
// with each iteration of the loop, the value of ‘$count’ will increase by one
// after the value of ‘$count’ reaches the specified number, the loop will stop
// *USER: change the ‘n’ to the number of posts that you would like to display
if ($count == "n") { break; }
else { ?>
// for CSS styling and layout purposes, we wrap the post content in a div
// we then display the entire post content via the ‘the_content()’ function
// *USER: change to ‘‘ to display post excerpts instead
// here, we continue with the limiting of the number of displayed posts
// each iteration of the loop increases the value of ‘$count’ by one
// the final two lines complete the loop and close the if statement
Source9. Highlight Author’s Comment
Highlight Authors comment
Have you ever seen this on blogs where author’s comments are distinguished from other comments? Well this is a simple and easy trick.
First you need to open your style.css in your template folder and add the following:
.authorstyle { background-color: #B3FFCC !important; }
Then you need to open your comments.php which is also located in your themes folder and find the code that looks some what like this:
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>"></li>
Replace it with:
<li class="<?php if ($comment->user_id == 1) $oddcomment = "authorstyle"; echo $oddcomment; ?>"></li>
Note you must change 1 to the user id of the author. Once you do this, your blog comments will have a different background for the author’s comment compared to the rest.
Thanks to Matt Cutts for this tutorial.
10. Create Thumbnails for Each Post and Display ThemCreate Thumbnails for Each Post in WordPress
This technique utilizes the new feature added in WordPress 2.9 to add Thumbnails to each post and you can display them anywhere in the loop.
11. Display Feedburner Subscriber Count as Text
Have you ever been to a site that is not using Feedburner button and is still showing subscriber count? Well they are probably using the trick we are about to share. Most designers use this trick to have custom styling and avoid the annoying feedburner button.
Simply copy and paste this code anywhere you like most likely sidebar.php<?php
//get cool feedburner count
$whaturl="http://api.feedburner.com/awareness/1.0/GetFeedData?uri=your feedburner id";
//Initialize the Curl session
$ch = curl_init();
//Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Set the URL
curl_setopt($ch, CURLOPT_URL, $whaturl);
//Execute the fetch
$data = curl_exec($ch);
//Close the connection
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
echo $fb;
//end get cool feedburner count
?>
You can wrap it around in styling to make it look like however you want.
12. Display Twitter Follower Count as Text
There are users who absolutely hate buttons like Feedburner buttons or Twittercounter buttons. Are you one of them? Do you want to display your twitter count as text, so it blends in to your new custom design? Well then this hack is just for you.
First you would need to create a file twitter.php and paste the following code in there:
<?php
$tw = get_option("twitterfollowerscount");
if ($tw['lastcheck'] < ( mktime() – 3600 ) )
{
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=wpbeginner');
if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
$tw['count'] = $match[1];
}
$tw['lastcheck'] = mktime();
update_option("twitterfollowerscount",$tw);
}
echo $tw['count'];
?>
Make sure to replace wpbeginner with your twitter name.
Then Simply place this code anywhere you want to display this:
<?php include("twitter.php"); ?>
Source13. Create Your Own Private Twitter Site using WordPressCreate Your Own Private Twitter Site Using WordPress14. Control When Your Posts are Available via RSS
Control When Your Posts are Available via RSS
There are times when you publish a post and suddenly find an error. You can go back in the admin panel and change it, but it is already published in the feeds. With this hack, you can put a delay of as many minutes as you like, so you can double check the post live.
Simply open your functions.php and add the following code in there:
function publish_later_on_feed($where) {
global $wpdb;
if ( is_feed() ) {
// timestamp in WP-format
$now = gmdate('Y-m-d H:i:s');
// value for wait; device
$wait = '10'; // integer
// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
$device = 'MINUTE'; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
// add SQL-sytax to default $where
$where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
}
return $where;
}
add_filter('posts_where', 'publish_later_on_feed');
Change the time from 10 minutes to whatever you like.
15. Allow Multiple Authors to be Associated with One Post
If you run a multi-author blog and have a post coming up that more than one author has contributed to, you can use this trick. This trick is being used by many top multi-author blogs.
Co-Author WordPress Blogs
Simply Download a Plugin called Co-Authors or Co-Authors Plus16. Change the Default Gravatar Button
Good Bye Mystery Man
The default mystery man is really annoying for most users. Plus if you have one more chance of branding your blog, then why not do it. Changing your default gravatar lets you brand your blog more. With this snippet below you can change your default gravatar.
First you need to open your functions.php which is located in your template folder. If you don’t have one then create one and insert the following code:
add_filter( 'avatar_defaults', 'newgravatar' );
function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo('template_directory') . '/images/gravataricon.gif';
$avatar_defaults[$myavatar] = &quot;WPBeginner&quot;;
return $avatar_defaults;
}
In the code the image is being extracted from the theme directory and it is called gravataricon.gif obviously you will change it to your image name. Where it says WPBeginner, that is the name of the avatar of how it will show in your admin panel options area.
Gravatar Settings
Head over to your admin panel and click Settings > Discussion and change the icon, and now you have a branded comment area with your logo.
17. Display Random Header Images in WordPress
Random Header Images
Most blog designs get boring if they have a huge header picture and it is static. This is when this tutorial comes in to make your header images dynamic because it rotates on each visit. You can select as many images as you want to rotate randomly. It brings life to a blog.
First you need to name your images in this format:
  • headerimage_1.gif
  • headerimage_2.gif
  • headerimage_3.gif
You must separate the name with an underscore. You can change the headerimage text to himage or anything you like.
Once you have done that paste the following code in your header.php where you would like the images to be displayed or in any other file.
<img src="http://path_to_images/headerimage_<?php echo(rand(1,3)); ?>.gif"
width="image_width" height="image_height" alt="image_alt_text" />
Make sure that you change the number 3 if you decide to do more than 3 images. This code is not exclusive for WordPress, it will work with any php based platform.
18. Only Display Certain Categories in a Menu
Display Certain Categories in a Menu
In many cases, users only want to display certain categories in their navigation menu at the top of the page. There are limited spots, that can only be filled by top categories, but if you use the default wp_list_categories code, it will show all categories. This is why this hack below comes in very handy when you want to create a navigation menu and only display certain categories.
<ul class="navmenubar" style="float:left; width:730px;">
<?php wp_list_categories('orderby=name&include=7,9,19,16,1,5,17,23'); ?>
</ul>
Note, you can also change the ‘include’ text to ‘exclude’ and show all categories and exclude those that you don’t want displayed. The numbers displayed in the code are the category IDs. Remember since WordPress shows categories in a list format, you will need to edit the CSS in order to make it work.
19. How set an Expiration Date for Your Posts
Set an Expiration Date for Your Posts
This hack comes becomes very useful when you are running a contest because you might be posting information such as clues or hints that you don’t want to stay up for ever. Instead of manually removing the article, you can just make it expire automatically. It also works if you have a product that you are offering a discount on. You posted it on your blog, but you don’t want that discount to stay on your blog after its over. So you can remove it automatically with this code.
All you need to do is replace your WordPress Loop with this code:
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example…
the_title();
the_excerpt();
}
endwhile;
endif;
?>
Once you have done that, you can use custom fields when writing a post to set an expiration date. Make sure you select the key “expiration” and use the the following date format: mm/dd/yyyy 00:00:00Now this hack does not remove or unpublish the article instead it just excludes the article from being displayed in the loop.
20. Delete Posts Revisions from Your Database
Delete Batches of Post Revisions
WordPress has a lot of good features and one of them is Post Revisions. This was included in WordPress 2.6, even though this is a good feature, it can cause some problems. One of them is increase the size of your database. Depending on how long it takes you to write a post, you might have as many as fifty post revisions. Now you can manually delete them, or you can run a simple query which we will show you in this post and get rid of all these useless revisions.
First thing you need to do is login to your phpMyAdmin and select your WordPress Database.
Click on the SQL Button and enter the following query:
DELETE FROM wp_posts WHERE post_type = "revision";
In this code basically we looked up a table wp_posts and removed every post that had a post_type revision associated with it. Now depending on the size of your database, this may save you a lot of space.
21. Create a Membership Directory using WordPressCreate a Membership Directory using WordPressThe end result is a moderated directory that allows members to enter information about themselves. Live Example.
22. Create a Peel Away Effect in WordPress
Peel Away Effect in WordPress
Have you arrived at a site where you see this page peel effect that some bloggers have. They are advertising their recent ebook, or some other product, and you want to do the same. Well then Hongkiat Blog has a great tutorial about this. The tutorial eliminates the use of plugin, but if you want an alternative option. Page Peel Plugin can get you the same functionality with a lot less work.
23. Custom CSS Stylesheet for Individual Posts
There are sites that use custom stylesheet for individual posts. Do you want to know how you can do it also? It is very simple. This is accomplished by the use of Custom Fields.
First you will need to open your header.php and insert this code somewhere in between <head></head> codes.
<?php if (is_single()) {
$customstyle = get_post_meta($post->ID, 'customstyle', true);
if (!empty($customstyle)) { ?>
<style type="text/css">
<?php echo $customstyle; ?>
<style>
<?php }
} ?>
Once you have done that you can add a custom field in each post with the name customstyle and add the css codes in there.
For example if you want the a certain image to have border you can add:
#coolimageclass{border: 5px solid #ccc;}
Use the format above and you now have custom CSS for your single posts.
24. Easily Add Custom Header, Footer, or Sidebar on Different Categories
Did you ever come across a site that is using different header or sidebar for some categories? Well this is how you can accomplish that as well.
To call a particular header, you will need to open your index.php and replace your normal header code with this one:
<?php if (is_category('Blogging')) {
get_header('blogging');
} else {
get_header();
} ?>
This code above is basically telling WordPress that if someone opens the category called “Blogging” then WordPress needs to display a file called header-blogging.php if it exist. If it does not exist, or if the category is not blogging, then WordPress displays the default header file.
To get a separate sidebar for each all you need to do is add the following code:
<?php if (is_category('Blogging')) {
get_sidebar('blogging');
} else {
get_sidebar();
} ?>
The code above will look for sidebar-blogging.php to replace the default footer file if the category is Blogging.
To get a separate footer for each category all you need to do is add the following code:
<?php if (is_category('Blogging')) {
get_footer('blogging');
} else {
get_footer();
} ?>
The code above will look for footer-blogging.php to replace the default footer file if the category is Blogging.
25. Create a Custom Login Page Design for WordPressCustom WordPress Login PageDo you have a multi-author site, or a community site powered by WordPress? Do you want to rebrand the login page design? Then this tutorial is just right for you.
26. Place Content Only in your RSS Feeds
RSS Footer
Do you have a lot of RSS Subscribers? Well then maybe you want to monetize it just like tons of other users who are doing it. The plugin called RSS Footer lets you add content to your RSS Feeds. You can style it however you like.
27. Disable HTML in WordPress Comments
Did you ever see in your SPAM folder, how many comments have tons of HTML codes which is basically links. That is how most spammers add links. But you can disable HTML in WordPress Comments to be functional. So if someone uses the strong code, it will not bold the text etc.
All you have to do is simply open your functions.php and add the following code:
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( '&apos;', "'", $comment_to_display );
return $comment_to_display;
Source – The original author also offers a plugin that you can download from his site.
28. Add Digg Buttons in Your Posts using Custom Fields
Doesn’t it get annoying posting digg buttons on your articles every time. But you don’t want to display them on every page by default because some articles are not digg worthy. Well this solution would be very helpful then. In this plugin, you will add the codes in your single.php once, and in each article that you want to display the button, you will just have to enable it using the custom field.
First open your single.php and find a code that looks like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Replace it with:
<?php if (have_posts()) : while (have_posts()) : the_post();
// check for digg button for single page
$digg = get_post_meta($post->ID, 'Digg', $single = true);
?>
Now you need to add the following code within the loop anywhere you like:
<?php // if there's a single page digg button
if($digg !== '') { ?>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
<?php } // end if statement
// if there's not a digg button
else { echo ''; } ?>
You may wrap it around with any styling that you want. Save the single.php and upload it to your theme folder.
Now when writing a post if you want to add a digg post simply add a custom field like shown in the screen shot below:
Add a Digg Button in Your WordPress using Custom Field
Whenever you specify this custom field, WordPress will display a digg button on your post.
29. Display Latest Sticky Posts in WordPress
Assuming that you have already created a custom page template and/or already have The Loop ready, paste the following code before the loop.
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );
/* Query sticky posts */
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
This code can very well be used in featured slider, or any other advanced feature that you would like to display on your site. This snippet is mostly geared toward a WordPress site that has a custom homepage or a magazine style look.
The credit to this code goes to Justin Tadlock and partially to Nathan Rice for coming up with the array slice solution.
30. Display a Retweet Button On Your Site
Add a Retweet button in WordPress
With Twitter getting so much exposure, as a blogger you should already be using it to your advantage. Power of twitter is like no other because it is word of mouth advertising. To make this easier on your readers, what you can do is place a prominent retweet button, so they can retweet the article with one click. Not only just that, but you should make it the way so you can track the retweets as well. That is where tweetmeme widget comes in.
In this tutorial we will have you create a button that will link to the text in the following format:
RT @ Title of the Post – Link
Add the following code in the template file of your choosing most likely single.phpFor the Large Button:
<script type="text/javascript">
tweetmeme_source = 'wpbeginner';
</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"> </script>
For the Compact Button:
<script type='text/javascript'>
tweetmeme_style = "compact";
tweetmeme_source = 'wpbeginner';
</script>
Remember to change the source to your twitter account name, this way you will not only promote your account to get more followers, but your article will be promoted as well.
31. Display Categories in a Drop Down Menu
Display Certain Categories in a Dropdown Menu
Some blogs have a lot of categories which they can’t display in their sidebar. Or some bloggers do not want to take up a lot of space displaying categories. This option allows them to have a select menu, dropdown menu, for categories. SeeWordPress Codex for details.
<form action="<?php bloginfo('url'); ?>/" method="get">
<?php
$select = wp_dropdown_categories('show_option_none=Select Category&show_count=1&orderby=name&echo=0&selected=6');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><input type="submit" value="View" /></noscript>
</form>
32. Show Category Icons
Show Category Icons
Pictures make a site more lively which is a given which is why blogger might want to associate pictures with their category. This plugin lets you associate a specific icon of your choice to a designated category. You can use this plugin to list categories by icon with or without names, and much more.
Download this Plugin33. Display the most Recent Post from a Specific Category
Did you see sites with a magazine style theme who are displaying posts from a specific category. Sometimes only the most recent post. Well you can do this too easily.
<?php
query_posts('showposts=1&cat=3');
while(have_posts()) : the_post();
?>
<ul>
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>
Add the above code anywhere you like in the template. Make sure you change the category ID and you can change the number of posts displayed as well if you want.
34. Future Post CalendarThis plugin adds a simple month-by-month calendar that shows all the months you have future posts for (and the current month no matter what), it highlights the days you have posts for, and as an added bonus if you click a day the Post Timestamp boxes change to that day, month and year.
Future Post Calendar
35. Modify Excerpt Length and More Tags
WordPress lets you display Excerpts, but up until version 2.9 you could not control the excerpt length. With the following code, you can increase the length from default 55 words to as many words as you like. Open your functions.php file and add the follwowing codes in there:
// Changing excerpt length
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
// Changing excerpt more
function new_excerpt_more($more) {
return '…';
}
add_filter('excerpt_more', 'new_excerpt_more');
Change the 100 word limit to the count that you desire.
36. Display Author’s Twitter and Facebook Information on their Profile Page
WordPress has fields for fields for author contacts, but it has not been updated in ages. So by default you do not have an ability to add author’s twitter or facebook. With this hack you can add this information on their profile page.
First open your functions.php and paste the following code:
<?php
function my_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';
return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);
?>
The above code will add two extra fields on your user-edit page named twitter and Facebook. You can display the code with the following code in your author.phpfile.
<?php echo $curauth->twitter; ?>
Source: Joost De Valk

0 comments:

Post a Comment

About Mikee Rabino:

Mikee Rabino is a 21 yrs old filipino blogger, the founder and editor of Top Blogger. Learn more about him here and connect with him on Twitter and Facebook. and be the 1st to post to his blog, Topblogger