How to add nofollow attributes to all links in a specific category

May 21, 2013 at 5:55 pm

Sometimes you may want to have an entire category of your blog with nofollow links, for exemple if you’re linking to a lot of external resources. Here is a quick and easy recipe to do so. Enjoy!

Simply copy the code below and paste it on your functions.php file. Don’t forget to set the desired category ID on line 3.

function nofollow_cat_posts($text) {
global $post;
        if( in_category(1) ) { // SET CATEGORY ID HERE
                $text = stripslashes(wp_rel_nofollow($text));
        return $text;
add_filter('the_content', 'nofollow_cat_posts');

Thanks to Sagive for submitting this function!

Automatically spam comments with a very long url

May 14, 2013 at 6:21 pm

Spam is definitely a problem for bloggers and most of you probably receive more than 100 spam comments per hour. Here is a simple recipe to automatically mark as spam all comments with an url longer than 50 characters.

Open your functions.php file and paste the code below in it. This code will automatically mark as spam all comments with an url longer than 50 chars. This can be changed on line 4.


  function rkv_url_spamcheck( $approved , $commentdata ) {
    return ( strlen( $commentdata['comment_author_url'] ) > 50 ) ? 'spam' : $approved;

  add_filter( 'pre_comment_approved', 'rkv_url_spamcheck', 99, 2 );


Thanks to CSS Tricks for the code snippet!

Deal of the week: SpyBar (3 licences to win!!)

May 3, 2013 at 3:01 pm

Today, I’m glad to introduce a new giveaway on WPRecipes, sponsored by our partners Mighty Deals. At the same time, I’m going to introduce SpyBar, an amazing tool for bloggers and website owners.

A word about Spybar

The Internet is one huge swap meet. So many sites “borrow” code or plugins from other sites to make their own that much better. And then someone else borrows from their site, and so on. Keeping up with the latest and greatest Web features is an exhausting experience, though. You can check out tons of forums, blogs, newsletters and more just to read up on the latest releases. You can also pay a ton of money to various online services to pull in stats and suggestions.

The best way, though? Just browse. Check out what your competition’s doing. If you find something you like, copy it. If only it were that easy to look beneath the hood. Well, it is. Thanks to SpyBar, you can pull in tons of useful data about any site, and you can do it all right from your Firefox or Chrome browser!

SpyBar is sold for only $7 for a limited time by Mighty Deals.

Here is a video demo of Spybar in action:

How to win a free Spybar licence!

Our partners from Mighty Deals are happy to give away 3 Spybar licences to WPRecipes readers. To enter the contest, simply leave a comment to this post to let me know you’d like to win a free Spybar licence. I’ll randomly pick 3 lucky winners on sunday using Winners will receive their prize directly in their email.

How to make translatable JavaScript strings on your WordPress theme

April 29, 2013 at 10:17 pm

Do you know that WordPress have a function called wp_localize_script(), which allow you to localize JavaScript strings? Here’s a practical example on how to use this little known but very useful function.

Simply paste the following code into your function.php file, where you generally enqueue scripts and styles. Line 4 shows how to use the wp_localize_script() function.

function prefix_enqueue_custom_script(){
	wp_register_script( 'prefix_custom_script', plugin_dir_url( __FILE__ ) .'js/custom-script.js', array( 'jquery' ) );
        wp_enqueue_script( 'prefix_custom_script' );
        wp_localize_script( 'prefix_custom_script', 'prefix_object_name', array(
		'upload' => __( 'upload', 'textdomain' ),
		'remove' => __( 'remove', 'textdomain' )
	) );

Thanks to Devin for this code!

How to add SVG upload support to your WordPress blog

April 25, 2013 at 4:21 pm

By default, WordPress uploader do not support the SVG format. As this file format is becoming quite popular theses days, here is a simple recipe to add SVG upload to your WordPress install.

Simply add the code below to functions.php in your WordPress theme. SVG upload will be supported once the file is saved.

add_filter('upload_mimes', 'my_upload_mimes');
function my_upload_mimes($mimes = array()) {
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;

Thanks to dbushell for the snippet!

How to create a directory within the uploads folder

April 23, 2013 at 6:23 pm

When developing a plugin or theme, it can be useful to be able to programmatically create a directory within the wp-content/uploads folder. Here is a handy piece of code to do it.

Simply paste this code snippet on your functions.php file (or plugin file if you’re creating a plugin)

function myplugin_activate() {
    $upload = wp_upload_dir();
    $upload_dir = $upload['basedir'];
    $upload_dir = $upload_dir . '/mypluginfiles';
    if (! is_dir($upload_dir)) {
       mkdir( $upload_dir, 0700 );
register_activation_hook( __FILE__, 'myplugin_activate' );

Thanks to Jean Galea for the snippet!

How to display an author bio excerpt on your WordPress blog

April 16, 2013 at 3:52 pm

WordPress offers users the option to add biographical info for each author. This bio can be displayed on your theme using the the_author_meta( ‘description’ ) function. But what if you’d like to display only a bio excerpt? Here’s a function to do it.

The first thing to do is to create the function. To do so, paste the following code into your functions.php file:

	function author_excerpt (){	                     					
		$word_limit = 20; // Limit the number of words
		$more_txt = 'read more about:'; // The read more text
		$txt_end = '...'; // Display text end 
		$authorName = get_the_author();
		$authorUrl = get_author_posts_url( get_the_author_meta('ID'));
		$authorDescription = explode(" ", get_the_author_meta('description'));
		$displayAuthorPageLink = count($authorDescription) > $word_limit ? $txt_end.'<br /> '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>' : '' ;
		$authorDescriptionShort = array_slice($authorDescription, 0, ($word_limit));
		return (implode($authorDescriptionShort, ' ')).$displayAuthorPageLink; 		

Once done, simply call the function when needed, as shown below:

<?php  if (function_exists('author_excerpt')){echo author_excerpt();} ?>

Thanks to Tim Marcher for the tip!

How to detect a comments page on your WordPress blog

April 15, 2013 at 4:04 pm

On WordPress dashboard, there’s an option to divide comments lists in sub pages. Unfortunately, there’s no built-in conditional tag to know if you’re currently on a comment page. So let’s built one!

Simply put the following code anywhere on your theme files. If you’re on a comment page, the conditional statement will return true, so any code within brackets will be executed.

$cpage = get_query_var( 'cpage' );
if ( is_singular() && $cpage > 0 ){
    // Your code here

This code works on posts, pages, attachments as well as all custom post_types.

Thanks to Daniel Roch for the cool tip!

How to force your WordPress blog to break out of frames

April 11, 2013 at 4:54 pm

Lots of scrapers are framing, defaming, and/or stealing your precious content. Here is a super useful snippet to force your blog to break out of frames so your pages will be served only from the original domain, not some Google adsense site created by a random scraper.

Nothing complicated, just paste the code below into your functions.php file, save it, and you’re done.

// Break Out of Frames for WordPress
function break_out_of_frames() {
	if (!is_preview()) {
		echo "\n<script type=\"text/javascript\">";
		echo "\n<!--";
		echo "\nif (parent.frames.length > 0) { parent.location.href = location.href; }";
		echo "\n-->";
		echo "\n</script>\n\n";
add_action('wp_head', 'break_out_of_frames');

Thanks to WP Mix for this very handy tip!

How to redirect to post if search results only returns one post

April 4, 2013 at 4:41 pm

When a reader search your site using WordPress built-in search engine, the results are displayed as a list. Here is a snippet to improve the search engine by automatically redirecting the reader to the post if only one post is found by WordPress search engine.

Just paste the following code snippet into your functions.php file:

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );

Thanks to for the snippet!