Important Notice: Anyone who tried to download the Notification Bubbles PSD yesterday will have noticed it was empty. It's been fixed now!
The Finished Box

Digital Resource Magazine

Subscribe
Options
About tfbox
Share

10 Extremely Useful WordPress Hacks to enhance your blog

Loading...
1
1758
10 Extremely Useful WordPress Hacks to enhance your blog

Some great tips and tricks for bettering your wordpress blog, it’s a definite must see if you’re a serious blogger.

Love it? 9

Over the years WordPress has become an extremely powerful tool and in this time many, many killer hacks have been discovered which make yours and my life a lot easier. Most of us at some stage of developing a WordPress theme find yourself pulling your hair out thinking iHow do i go about doing thisi. Well this is why I’ve decided to show you 10 useful WordPress Hacks to enhance your blog.

This article will feature what I believe to be some of the best methods out there to better your blog, some methods you may be aware of some not. If you feel that I missed out something or you are curious of how to do something just leave a comment and i’ll get back to you.

1

Loading JavaScript

Load JavaScript into a WordPress generated page or plugins if it hasn’t already been included using the enqueue_script function.

Before using any of these methods it’s important you have the following code in your header.php file:

<?php wp_head(); ?>

Load the jQuery Library

<?php wp_enqueue_script('jquery'); ?>

Load a new script on a separate page

This is dependant on you having jQuery loaded already
<?php wp_enqueue_script('yourscriptname', get_bloginfo('stylesheet_directory') . 'jsyourscript.js'); ?>
If you don’t have jQuery loaded already simply use an array to load it onto the page
<?php wp_enqueue_script('yourscriptname', get_bloginfo('stylesheet_directory') . 'jsyourscript.js', array('jquery')); ?>

Also note

The get_bloginfo(‘stylesheet_directory’) will simply go to the root of your theme file, you don’t have to use that you could simply just use the direct url path to the file but I feel this is a better way of doing it.

2

Exclude multiple categories

Excluding multiple categories is pretty simple with this hack and can be proven to be pretty effective used in some cases.

Exclude from the loop

Put the following code in your functions.php:

function excludeCategoriesLoop($query) {
    if ( $query->is_home ) {
    $query->set('cat', '-17 -19');
    }
    return $query;
    }
add_filter('pre_get_posts', 'excludeCategoriesLoop');

This will only effect the post’s on the home page. All you need to edit are the -17 -19 to the category id’s.

Exclude from RSS Feed

If you feel that some post categories aren’t really necessary for your RSS Followers attention then all you have to do is change a little bit of code from the snippet above.

function excludeCategoriesFeed($query) {
    if ( $query->is_feed ) {
    $query->set('cat', '-17 -19');
    }
    return $query;
    }
add_filter('pre_get_posts', 'excludeCategoriesFeed');

All that’s changed is is_home to is_feed.

RSS only category

Why not give your RSS readers a little exclusive post now and then, it can also be a good way to get more readers.

Simply create a new category for example RSS and note down somewhere the ID for this category. Now add the following code to your functions.php:

function rssCategory($query) 
    if ( $query->is_home || $query->is_archive || $query->is_search ) {
    $query->set('cat', '-17');
    }
    return $query;
    }
add_filter('pre_get_posts', 'rssCategory');

Change that number to the ID of your category, this will exclude the category from all the pages except the RSS Feed.

3

Exclude Pages from search results

Excluding pages from search results is always something I’ve done, to me it just doesn’t seem relevant to have them in there in the first place, but that’s just me.

Exclude certain pages

Add the following code to your functions.php to exclude pages by ID:


function excludePagesFromSearch($query)  {  
    if ($query->is_search) {  
        $query->set('cat','4,5,9');  
    }  
    return $query;  
}  
add_filter('pre_get_posts','excludePagesFromSearch'); 

Exclude all pages

Add the following code to your functions.php to exclude all pages:

function excludeAllPagesFromSearch($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','excludeAllPagesFromSearch');
4

Insert an Advertisement within your WordPress Loop

A lot of blogs out there tend to have advertisements in the sidebar, sometimes that’s just not enough. With this method you will be able to insert an advertisement anywhere you wish inside your WordPress loop.

Locate your WordPress loop

In the index.php somewhere you should have code that looks like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; endif; ?>

This is your WordPress loop reconstruct it to look like this:

<?php if (have_posts()) : ?>
<?php $loopCounter = 0; ?>
<?php while (have_posts()) : the_post(); ?>
    <?php $loopCounter++; ?>
    <?php if ($loopCounter == 4) { ?>
        //Insert your ad code here
    <?php }  ?> 
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php the_excerpt(); ?> 
<?php endwhile; ?> 
<?php endif; ?> 

The variable $loopCounter is set to 0 just before the loop begins. Each time a post is returned this number will increment by 1 by adding the $loopCounter++ within the loop. Whatever number you choose will be the number of posts that have been returned so far.

5

Limit the excerpt word count

Want to gain more control over your excerpts Limit the amount of words displayed in the excerpt with this script.

Add the function

Open up your functions.php file and add the following code:

function excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt).'...';
  } else {
    $excerpt = implode(" ",$excerpt);
  }	
  $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  return $excerpt;
}
 
function content($limit) {
  $content = explode(' ', get_the_content(), $limit);
  if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content).'...';
  } else {
    $content = implode(" ",$content);
  }	
  $content = preg_replace('/\[.+\]/','', $content);
  $content = apply_filters('the_content', $content); 
  $content = str_replace(']]>', ']]&gt;', $content);
  return $content;
}

Wrap it up

Now open your index.php file, assuming you’re using excerpts already locate the following code:

<?php the_excerpt(); ?> 

Replace this with:

<?php echo excerpt(30); ?> 

Change the number to whatever the amount of words you wish to display is.

6

Highlight Searched text in results

One of the let downs I find with the WordPress search is that it doesn’t highlight the searched text in the search results, there’s a simple solution to all of this. Firstly open your search.php

Solution

Inside the search.php locate the following:

the_title();

Replace that with:

echo $title;

Right above that modified line, Add the following:

<?php
	$title = get_the_title();
	$keys= explode(" ",$s);
	$title = preg_replace('/('.implode('|', $keys) .')/iu',
		'<strong class="search-excerpt">\0</strong>',
		$title);
?>

Add the css

strong.search-excerpt { background yellow; }
7

Protect your blog from hotlinking

There are quite a few sites out there that hotlink images, with this script you’ll be able to stop it by inserting a small snippet of code into your .htaccess file so make sure that’s handy. It’s usually kept in the root of your web files.

Hotlinking is the use of an image from one site into a web page belonging to another site.

Solution

Put this code into your .htaccess file:

RewriteEngine On
#Replace ?mysite\.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/nohotlink.jpg with your "don't hotlink" image url
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]
8

Create A Maintenance Page For Your WordPress Blog

Ever came into a problem with your site and you need to make some drastic changes but at the same time you don’t want to be messing around with things while you have regular visitors Especially if it’s tweaking of the design or code. Well since WordPress doesn’t have a feature to temporarily redirect visitors to a maintenance page we will have to dive into the .htaccess file which is normally found at the root of your web files.

Solution

Put this code into your .htaccess file

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

In this example any I.P Address which is different to 888.888.888.888 will be redirected to maintenance.html. You will need to change that I.P Address to your own, if you don’t know your I.P Address you can find out here.

Once you are done making your adjustments simply open up the .htaccess file again and delete it.

9

Clean up your MySQL Database

Every WordPress blog is driven by a MySQL Database and without even realising your database is constantly getting filled up with information that isn’t necessarily needed. Here are 2 quick ways you can clean up thousands of rows in seconds to boost your blog performance.

Deleting Post Revisions

While Post Revisions are great they also increase the size of your MySQL Database vastly which is not good so a simple method to clear all that data in one go is to run the following SQL Query in phpMyAdmin.

DELETE FROM wp_posts WHERE post_type = 'revision';

Deleting Spam Comments

Just like Post Revisions spam comments will bulk up your database size so it’s good to clear them out too. Run the following SQL Query in phpMyAdmin

DELETE from wp_comments WHERE comment_approved = '0';

This is a very useful hack if only Askimet is installed, else otherwise you may end up deleting valid unapproved comments awaiting approval.

10

Control when your published posts are on your RSS Feed

Everyone is human, we all make mistakes from time to time. So when you’re running a blog and you want to maintain that professional vibe you give off and you publish your post only to notice several minutes later you’ve made a mistake what can you do It’s already in your RSS Feed. Well here is a brilliant solution to that problem.

Paste the following code into your functions.php

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 = '5'; // 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');

This is a great little piece of code and quite self explanatory too. In this example it will add a 5 minute delay between your post being published and it being on your RSS Feed. Simply edit the value for wait and device to your desire.

Connect to the author

I am the founder of The Finished Box, a Digital Resource Magazine. Founded in October 2010 and launched in February 2011 my focus is to bring you articles, inspiration, freebies and more focusing on quality more than quantity.

  • Anonymous

    This information is really helpful. People are like to secure and protect the blog. I many time sow in social website people are saying his blog is hacked. I think this is helpful for wordpress blogger user spicily for me. Thanks for giving this useful information.

    psd to wordpress

blog comments powered by Disqus