ThemeFuse giveaway! 3 premium themes to win

February 5, 2013 at 5:07 pm

Do you want to win a free premium theme from our friends at ThemeFuse? Well, today’s might be your lucky day as ThemeFuse will give away 3 premium themes to WPRecipes readers.

A word about ThemeFuse

ThemeFuse is one of the most popular actors in the premium WordPress themes market. They provide lots of themes with a great design and solid code.

They have lots of different designs for lots of different needs, so you should definitely have a look to their website!

How to enter the contest?

To enter, simply leave a comment on this article to let me know which theme from ThemeFuse WordPress themes you’d like to win. In one week (February 13, 2013), I’ll randomly pick up 3 winners. Each winner will receive the theme of their choice from ThemeFuse.

Good luck everybody!

How to redirect your author archive link to your “About” page

January 31, 2013 at 5:01 pm

By default, when someone click on your author posts URL, he is redirected to your author archive page. It is useful on multi-author blogs, but if there’s only one author on your blog, author archives are pointless. Here is a quick tip to redirect your author archive link to your “About” page.

This code have to be pasted into your functions.php file. Don’t forget to replace about on line 4 by the slug used by your “About” page.

add_filter( 'author_link', 'my_author_link' );

function my_author_link() {
	return home_url( 'about' );
}

Thanks to Justin Tadlock for the cool tip!

WordPress shortcode to easily integrate a Google Map on your blog

January 28, 2013 at 5:13 pm

Google Maps are very useful to show locations on your blog, but they can be a bit tricky to integrate, especially for client or non tech-savvy users. Here is a shortcode that will make Google Maps integration super easy.

To create the shortcode, paste the code below into your functions.php file:

function rockable_googlemap($atts, $content = null) {
   extract(shortcode_atts(array(
               "width" => '940',
               "height" => '300',
               "src" => ''
   ), $atts));
 
return '<div>
         <iframe src="'.$src.'&output=embed" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="'.$width.'" height="'.$height.'"></iframe>
        </div>
       ';
}
 
add_shortcode("googlemap", "rockable_googlemap");

Once you saved your function.php file, you can start integrating Google Maps into your posts and pages. To do so, get the url of the map you’d like to display, and use the shortcode as shown below:

[googlemap src="google_map_url"]

or with custom width and height parameters:

[googlemap width="600" height="250" src="google_map_url"]

Thanks to Rockable Themes for the tip!

How to prevent WordPress to display login errors

January 24, 2013 at 6:50 pm

By default, when you (or someone else) fail to login on your wp-admin, WordPress displays an error message. While those messages might be useful, they can also be used by hackers to track their progress. Here is a nice tip to prevent WordPress from displaying login errors.

Paste the following line of code into your functions.php file:

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

Once you saved the file, WordPress will not display any login error messages anymore.

Thanks to WP Tuts for the tip!

How to display post attachment count in admin post list

January 14, 2013 at 5:21 pm

If you’re using many attachments in your posts, you might like to be able to visualize how many attachments a post have just from the admin post list. Here is a quick recipe to force WordPress to display post attachment count in admin post list.

Just paste the code below into your function.php file. Once you saved the file, a new column will display the post attachment count on your admin post list.

add_filter('manage_posts_columns', 'posts_columns_attachment_count', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns_attachment_count', 5, 2);
function posts_columns_attachment_count($defaults){
    $defaults['wps_post_attachments'] = __('Att');
    return $defaults;
}
function posts_custom_columns_attachment_count($column_name, $id){
        if($column_name === 'wps_post_attachments'){
        $attachments = get_children(array('post_parent'=>$id));
        $count = count($attachments);
        if($count !=0){echo $count;}
    }
}

Thanks to Kevin Chard for the tip!

Automatically add classes to links generated by next_posts_link and previous_posts_link

January 9, 2013 at 4:37 pm

By default, the next_posts_link() and previous_posts_link() are generated with no custom class. If you need one for your CSS styling or anything else, here’s a pretty simple way to add the needed class by using a hook.

Paste the following code in your functions.php file:

add_filter('next_posts_link_attributes', 'posts_link_attributes');
add_filter('previous_posts_link_attributes', 'posts_link_attributes');

function posts_link_attributes() {
    return 'class="myclass"';
}

Thanks to CSS Tricks for the function!

How to redirect WordPress frontend to the backend

January 4, 2013 at 8:18 pm

It may seems a bit weird, but in some projects, you may only need WordPress backend. This super simple trick allows you to automatically redirect all the frontend part of the site to your wp-admin/login form.

The first thing to do is to open your theme header.php file, and paste the following lines at the top of it:

<?php header("Location: ".get_admin_url());
exit(); ?>

Once done, your whole site is now redirected to your backend.

This recipe is based on this trick by Vladimir Prelovac.

Shortcode to display external files on your posts

January 2, 2013 at 7:51 pm

When blogging, you may need to include an external file, such as text or image, into your own posts. Here is a very useful shortcode to embed and display any external files in your blog posts.

Open your functions.php file and paste the following function in it:

function show_file_func( $atts ) {
  extract( shortcode_atts( array(
    'file' => ''
  ), $atts ) );
 
  if ($file!='')
    return @file_get_contents($file);
}
 
add_shortcode( 'show_file', 'show_file_func' );

Once done, you can use the shortcode. It’s pretty simple:

[show_file file="http://www.test.com/test.html"]

Thanks to Vladimir Prelovac for the handy tip!

Mighty Deals Christmas giveaway

December 12, 2012 at 11:04 pm

Christmas is soon, and I thought this is definitely the right time of the year to organize a contest here at WPRecipes. This time, our friends at Mightydeals are giving away the deal of their choice to 3 of WPR readers.

A word about Mighty Deals

Mightydeals offers unbelievable deals and discounts for creative professionals.The deals include products and services that are heavily discounted, exclusively for Mighty Deals customers, usually from 50% to 90% off. Each deal stays on the site for a very limited time and is available exclusively for purchase directly through the site.

How to enter the contest?

To enter, simply leave a comment to let me know which deal you’d like to win from the available mighty deals. On wednesday 19, I’ll pick 3 winners using random.org. Winners will receive the deal they chose directly from Mighty Deals.

WordPress tip: Add a custom field to all posts

December 6, 2012 at 7:28 pm

For some reason you sometimes need to add a custom field to all your existing posts. You can do it manually, but if you have lots of posts, the process will take a while. Here is a simple way to add a custom field to all posts quickly and easily.

Simply run the following SQL query on your WordPress database, using the command line client or PhpMyAdmin. This will automatically ad a custom field named MyCustomField to all your existing posts.
Don’t forget to backup your database before using this query.

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'MyCustomField'
AS meta_key 'myvalue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'MyCustomField')
`` AND post_type = 'post';

Thanks to Abhishek for the tip!