WordPress tip: Force specific pages to be SSL secure

July 2, 2013 at 3:26 pm

If SSL is enabled on your webserver, you should definitely use it to protect your blog. Activating SSL on your specific pages on a WordPress blog is definitely easy: Just read on.

Just add the following snippet to the functions.php file of your WordPress theme and specify the post or page ID desired.

function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
    if ( $post_id == 25 ) {
        return true
    return $force_ssl;
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);

Thanks to Kevin Chard for this recipe!

How to crop uploaded images instead of scaling them

June 26, 2013 at 5:59 pm

Would you like to crop your thumbnails instead of scaling them? If yes, I have a very handy snippet for you today. Just read on and enjoy!

Just add the code below to your functions.php file:

// Standard Size Thumbnail
if(false === get_option("thumbnail_crop")) {
     add_option("thumbnail_crop", "1"); }
     else {
          update_option("thumbnail_crop", "1");

// Medium Size Thumbnail
if(false === get_option("medium_crop")) {
     add_option("medium_crop", "1"); }
     else {
          update_option("medium_crop", "1");

// Large Size Thumbnail
if(false === get_option("large_crop")) {
     add_option("large_crop", "1"); }
     else {
          update_option("large_crop", "1");

Thanks to wp-snippet.com for the tip!

WordPress shortcode to embed Google trends graphs

June 24, 2013 at 5:45 pm

Google trends is a service which allow you to track the popularity of specific keywords. Here’s the code to create a WordPress shortcode that will embed a Google trends graph of any comma separated query on your blog.

The first step is to create the shortcode. To do so, open your functions.php file and paste the code below in it:

function wps_trend($atts){
        extract( shortcode_atts( array(
                'w' => '500',           // width
                'h' => '330',           // height
                'q' => '',              // query
                'geo' => 'US',          // geolocation
        ), $atts ) );
        //format input
<script type="text/javascript" src="http://www.google.com/trends/embed.js?hl=en-US&q=<?php echo $q;?>&geo=<?php echo $geo;?>&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=<?php echo $w;?>&h=<?php echo $h;?>"></script>
return ob_get_clean();

Once you saved your functions.php file, you can now use the shortocode in your posts and pages. Here is an example of usage:

[trends h="450" w="500" q="wpsnipp,wordpress,+wordpress+theme,+wordpress+plugin,+wordpress+snippets" geo="US"]

Thanks to Kevin Chard for the shortcode!

How to setup different admin and theme languages on your WordPress blog

June 21, 2013 at 11:25 am

I like me, you’re blogging in a language which is not your mother tongue, you might like to have the dashboard in your language. Here is a quick tip to use different languages on your WordPress blog or website.

Simply set the desired locale (on line 6) then add the code to your functions.php file.

     // setup one language for admin and the other for theme
     // must be called before load_theme_textdomain()

     function set_my_locale($locale) {
          $locale = ( is_admin() ) ? "en_US" : "it_IT";
          setlocale(LC_ALL, $local );
          return $locale;
     add_filter( 'locale', 'set_my_locale' );

Thanks to wp-snippet for the cool tip!

How to use WordPress shortcodes in theme files

June 6, 2013 at 7:22 pm

WordPress shortcodes are very useful and easy to use. But unfortunately, you can only shortcodes in post/page editor, not theme files. But there’s a little trick to do it easily. Just read on!

To use a shortcode in a theme file, simply use the do_shortcode() function as shown below:

<?php echo do_shortcode("[your_shortcode]"); ?>

That’s it. Super simple, and really useful at times!

Automatically link Twitter usernames in WordPress

June 4, 2013 at 6:02 pm

Are you using Twitter a lot? Today’s recipe is a cool piece of code to automatically link Twitter usernames on your posts, pages, and comments.

Paste the code below into your functions.php file:

function twtreplace($content) {
	$twtreplace = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/',"$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>",$content);
	return $twtreplace;

add_filter('the_content', 'twtreplace');   
add_filter('comment_text', 'twtreplace');

Once you saved the file all twitter usernames in posts and comments will automatically be linked to their Twitter profiles. Usernames have to be written under the form @username.

Thanks to ederwp for the tip!

Themes4all giveaway: LOTS of prizes to win!!

May 24, 2013 at 4:00 pm

Here’s our new contest! This time, our partners Themes4All are giving away lots of premium themes and plugins to 5 lucky winners. Read on to know how you can join the contest and try to win one of the packages gave by Themes4All.com!

A word about Themes4All

Our sponsor is Themes4All.com, which is a newcomer in the WordPress premium theme business. They specialize in low cost/high quality themes.
They already released 20+ themes, each priced $10. Click here to view the available themes.

How to join the giveaway?

Joining the giveaway is super easy and free as usual. First, go to Themes4All.com and create a free account. Registration grant you access to a free theme each month. Once done, simply leave a comment on this post to join.
In two weeks (June 7, 2013) I’ll pick 5 winners using random.org.

Winners will get:
1. 4 template of your choice + premium beta responsive parallax slider
2. 3 template of your choice + premium beta responsive parallax slider
3. 2 template of your choice + premium beta responsive parallax slider
4. 1 template of your choice + premium beta responsive parallax slider
5. premium beta responsive parallax slider

If you won, you’ll receive your prize by email, directly by Themes4All staff. Now, good luck everyone!

How to automatically insert a list of related articles below the post

May 23, 2013 at 4:32 pm

When a reader finished reading one of your blog posts, why not suggesting him other article he might like as well? Here’s a quick tip to automatically display related posts (based on category) below the current post.

First, paste the code below into the functions.php file from your theme.

// "More from This Category" list by Barış Ünver @ Wptuts+
function wptuts_more_from_cat( $title = "More From This Category:" ) {
    global $post;
    // We should get the first category of the post
    $categories = get_the_category( $post->ID );
    $first_cat = $categories[0]->cat_ID;
    // Let's start the $output by displaying the title and opening the <ul>
    $output = '<div id="more-from-cat"><h3>' . $title . '</h3>';
    // The arguments of the post list!
    $args = array(
        // It should be in the first category of our post:
        'category__in' => array( $first_cat ),
        // Our post should NOT be in the list:
        'post__not_in' => array( $post->ID ),
        // ...And it should fetch 5 posts - you can change this number if you like:
        'posts_per_page' => 5
    // The get_posts() function
    $posts = get_posts( $args );
    if( $posts ) {
        $output .= '<ul>';
        // Let's start the loop!
        foreach( $posts as $post ) {
            setup_postdata( $post );
            $post_title = get_the_title();
            $permalink = get_permalink();
            $output .= '<li><a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</a></li>';
        $output .= '</ul>';
    } else {
        // If there are no posts, we should return something, too!
        $output .= '<p>Sorry, this category has just one post and you just read it!</p>';
    // Let's close the <div> and return the $output:
    $output .= '</div>';
    return $output;

Once done, open your single.php file and call the function as shown below, where you’d like to display the related posts:

<?php echo wptuts_more_from_cat( 'More From This Category:' ); ?>

Thanks to WP Tuts for the cool tip!

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!