No More Hassle: The WordPress Hacks to an Easy Life

Posted in HowTos, WordPress • Posted on 1 Comment

There is no doubt. People love hacks about WordPress. Being the most popular CMS in the planet, WordPress offers more flexibility now than it was first released. There is no better way to make your WordPress life easier than to avail of these WordPress hacks.

Modifying WordPress blogs gives more freedom to the owners of the sites. It allows the owners to do things they can’t find on their default themes, thus, adding values to their sites.

In this article, you will learn some of the most outstanding WP hacks that you will definitely find useful.

1. Display External RSS Feed on Your Site

If you are wondering how you can display blog feeds that are coming from other sites for some extra promotions and traffic, you might want to check the codes below.

Just paste the following code anywhere in your theme and it will pull out and loop the blog feeds.


<?php include_once(ABSPATH.WPINC.'/feed.php');
$rss = fetch_feed('http://feeds.feedburner.com/1stwebdesigner');
$maxitems = $rss->get_item_quantity(3);
$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>

2. Display the Number of Twitter Followers in Your Site

Although there are a lot of WordPress plugins out there that you can use to show your Twitter follower count. But sometimes a plugin is not necessary.

Here’s how this easily be done.

A. Create twitter-count.php file on the root directory of your theme.
B. Paste the code in the file:

<?php
$twitter_count = get_option("twitterfollowerscount");
if ($twitter_count ['lastcheck'] < ( mktime() – 3600 ) )
{
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=1stwebdesigner');
if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
$twitter_count['count'] = $match[1];
}
$twitter_count ['lastcheck'] = mktime();
update_option("twitterfollowerscount",$twitter_count);
}
echo $twitter_count['count'];
?>

Note: Replace the 1stwebdesigner title on this line.

$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=1stwebdesigner');
 [/javscript]

<p>C. Include the file on any page template you want it to appear and it will display on any pages you include it on.</p>
 1
<?php include("twitter-count.php"); ?>
 

3. Change the Default Gravatar Image

By default, there is this mystery man image as the default gravatar of a WordPress theme. This might be fine but if you want to brand your site, changing it will add more value to your site.

Here’s how you do it.

A. Open functions.php
B. Add a branded image gravatar (preferably 80 X 80px gif image)
C Add the following codes on it:

add_filter( 'avatar_defaults', 'branded_gravatar' );

function branded_gravatar ($avatar_defaults) {
$brandedavatar = get_bloginfo('template_directory') . '/images/branded_gravatar.gif';
$avatar_defaults[$brandedavatar] = &quot;1WD&quot;;
return $avatar_defaults;
}

This code will add a new gravatar at the WordPress admin panel. To check, simply go to Settings -> Discussion and you’ll see a new gravatar has been created and can be selected as the official gravatar of an anonymous user.

4. Retrieve Your Most Popular Posts Without a Plugin

You can easily retrieve the most recent posts on your blog; however, for the most popular posts, you might need a plugin.

Although there are a lot of plugins available out there to achieve that effect, you simply use this blocks of codes to retrieve your most popular posts in the blog.

Just open your sidebar.php file and paste the following codes on it.

<h2>Popular Posts</h2>
<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 4");
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 $wpdb object will get a list of the 4 posts that have the most of the comments. The popular posts will be set in an unordered list.

5. Set an Expiration Date for Your Posts

If you want your posts to be removed on your blog within a specific time or date, this WordPress hack will be very useful to you.

Instead of manually deleting it on the posts list, you can use the following codes. All you need to do is manipulate your WordPress loop with the following 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;
?>

6. Disable HTML in WordPress Comments

Most of the user who posts comments in your blogs place hyperlinks in their comments, which sends your site a lot of spams. Most of these spammers use HTML to parse links and add it to your blog posts. There is an easy way to disable this in your site.

You just need to copy the following codes to the functions.php.

// 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;

7. Modify Excerpt Length

If you are developing a WordPress theme or you are just a site owner trying to modify the length of the excerpt of the blog list, you might wonder if there is a way you can set up the number characters to display on your homepage or blog page.

You can do it by opening your functions.php file and putting the following codes below.


// Changing excerpt length
function new_excerpt_length($length) {
return 80;
}
add_filter('excerpt_length', 'new_excerpt_length');

 

You can change the return value to your preferred length of characters.

8. Set Default Post and Page Editor

If you prefer to use HTML editor than the Visual editor or vice versa when writing posts and pages, you can do that using the following snippets.

First, open your functions.php file and then copy the code below on it.


/* Display Visual Editor as default */

add_filter( 'wp_default_editor', create_function('', 'return "tinymce";') );

/* Display HTML Editor as default */
add_filter( 'wp_default_editor', create_function('', 'return "html";') );

9. Add Class to a Featured Image

By default, you simply add the featured image functionality in your WordPress blog using the the_post_thumbnail; however, if you want to style it you might need to parse some arguments on it.

The code below will let you add a class in your featured image.

<?php the_post_thumbnail('post-thumbnail', array( 'class'=> "featured-image-style")); ?>

10. Display Your Recent Tweets on Your WordPress Site

Although there are plenty of WordPress plugins available on the Web that can display your recent tweets, you can display them without the plugin.

Simply copy the following snippet anywhere from your blog and it will display your most recent tweets.

<?php
$username = "1stwebdesigner "; // Place your twitter username here
$prefix = ""; // Prefix – Place some text before you tweets.
$suffix = ""; // Suffix – Place some text after you tweets.
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";

function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("&lt;", "<", $tweet);
$tweet = str_replace("&gt;", ">", $tweet);
return $tweet;
}

$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>

11. Customize Your WordPress Dashboard Logo

If you want to personalize your Dashboard logo, the following snippet will be helpful to you.

Just copy the following code in your functions.php and add your logo in gif format inside your theme’s image directory.

add_action('admin_head', 'custom_dashboard_logo');

function custom_dashboard_logo() {
echo '

<style type="text/css"><!--
#header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
--></style>';
}

12. Remove Error Message on the Login Page

Some hackers might dig in when they see some error on your WordPress login page. This means that if the hacker got some details like the path of your theme through the error message, it can then be used to hack your site.

By adding the following snippet on your functions.php, you can disable the error message from displaying in your login page.

add_filter('login_errors',create_function('$a', "return null;"));

Conclusion

And that’s really it folks! These will really help a lot when you want to customize your WordPress site the way you want it to be.

There might be some other WordPress hacks out there but be careful enough when inserting them on your code because you might break some other codes on your site.

46 Written ArticlesGoogle+

is a web designer and a front web developer with over 2 years of experience in the industry. Have a passion for designing detailed, creative and modern websites & graphics. He spend most of his time practically every day, experimenting with HTML, CSS and Wordpress.

Join Discussion Now!

One Comment
  • Jamie

    Sunday, September 7th, 2014 14:40

    1

    This seems fundamentally wrong. Wouldn’t it be better to wrap these “hacks” into plugins, rather than modifying source files? That’s actually a major problem with WordPress – nobody really understands how it all works, so they just hack all their files. It is really frustrating being the one to take over a project developed like that. Clutter – lots of clutter – and absolutely no structure.

    +2 Reply
  • Jamie

    Sunday, September 7th, 2014 14:40

    1

    This seems fundamentally wrong. Wouldn’t it be better to wrap these “hacks” into plugins, rather than modifying source files? That’s actually a major problem with WordPress – nobody really understands how it all works, so they just hack all their files. It is really frustrating being the one to take over a project developed like that. Clutter – lots of clutter – and absolutely no structure.

    +2 Reply
54.91.120.148 - unknown - unknown - US