CSTLdesign News

Best WordPress snippets, hacks and tips from 2013

How to crop uploaded images instead of scaling them

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"); }

Source: http://wp-snippet.com/snippets/activate-cropping-for-all-thumbnail-sizes/

Automatically link Twitter usernames in WordPress

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');

Source: http://snipplr.com/view/70977/automatically-link-twitter…

Automatically spam comments with a very long url

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

Source: http://css-tricks.com/snippets/wordpress/spam-comments-with-very-long-urls/

How to clean up wp_head() without a plugin

WordPress adds a lot of stuff through wp_head() hook included in most WordPress themes. Some of this stuff is useful, but some other isn’t. Here’s a quick recipe to clean up the wp_head() easily without using a plugin.

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

remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );

Source: http://www.themelab.com/remove-code-wordpress-header/

WordPress tip: Force specific pages to be SSL secure

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

Source: http://wpsnipp.com/index.php/functions-php/force-specific-pages…

How to run the loop outside of WordPress

Ever needed to be able to access your WordPress data and run a loop OUTSIDE of your WP install? Here’s a code snippet which allow you to run a WordPress loop on any PHP file, even outside of your WordPress install.

Paste the following code on any PHP file where you want to run your WordPress loop. Don’t forget to modify the following:

line 4: Please specify the path to your WordPress wp-blog-header.php file.

line 5: Query posts using the query_posts() function.

<?php // Include WordPress define('WP_USE_THEMES', false); require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php'); query_posts('posts_per_page=1');
?> <?php while (have_posts()): the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> <p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p>
<?php endwhile; ?>

Source: http://css-tricks.com/snippets/wordpress/run-a-loop-outside-of-wordpress/

WordPress function to show a total share counter (FB, Twitter, G+)

Sharedcount.com is a useful website which allow you to get the total likes, shares, tweets, etc for a specific web page. Here’s a super handy function to display how many times a page has been liked/shared/tweeted on your blog.

Simply paste the following function where you want your counter to appear:

function social_shares() { $url = get_permalink( $post_id ); $json = file_get_contents(&quot;http://api.sharedcount.com/?url=" .
rawurlencode($url)); $counts = json_decode($json, true); $totalcounts= $counts[&quot;Twitter&quot;] + $counts[&quot;Facebook&quot;][&quot;total_count&quot;] +
$counts[&quot;GooglePlusOne&quot;]; echo &quot;&lt;div&gt;$totalcounts Share&lt;/div&gt;&quot;;

Source: http://www.wprecipes.com/wordpress-function-to-show-a-total-share-counter-fb-twitter-g

How to easily make WordPress images responsive

Responsive images can be big on wide screens and automatically adapt to smaller screens such as iPad. Making your images responsive is not difficult to do: Here is a simple recipe to achieve it on your blog.

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

function responsive_images($atts, $content = null) { return '<div class="image-resized">' . $content .'</div>';
} add_shortcode('responsive', 'responsive_images');

Once done, open your style.css file and add those CSS rules:

@media only screen and (max-width:767px) { .image-resized img { width:100%; height:auto; }

You can now use the [responsive] shortcode to insert responsive images in your blog:

[responsive]<img src="image_url" alt="alt" title="title" />[/responsive]

Source: http://rockablethemes.com/wordpress-responsive-images/

How to add custom text to WordPress login page

If for some reason you need to display a custom message on WordPress login page, here is a quick and easy recipe to do it.

Nothing complicated, paste the code below in your functions.php file. Message can be customized on line 3.

function wps_login_message( $message ) { if ( empty($message) ){ return "<p class='message'>Welcome to this site. Please log in to continue</p>"; } else { return $message; }
add_filter( 'login_message', 'wps_login_message' );

Source: http://wpsnippy.com/2013/08/how-to-add-custom-text-to…

WordPress shortcode to embed Google trends graphs

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 $h=(int)$h; $w=(int)$w; $q=esc_attr($q); $geo=esc_attr($geo); ob_start();
<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();

Source: http://wpsnipp.com/index.php/functions-php/shortcode-embed-google-trends-graph…