wordpress snippets

This is this week’s round up of handy WordPress snippets. With these WordPress snippets, you can add useful functions to your WordPress theme in just a couple of minutes.

Limit Post Title Length

If you have guest posters or multiple people write for your blog, you can limit the number of words in the title. This is great for handling seo problems  by having titles cut off. Add the following code to your functions.php file to limit post title lengths.

 
function maxWord($title){ 

global $post;
$title = $post->post_title; 

if (str_word_count($title) >= 10 ) //set this to the maximum number of words
wp_die( __('Error: your post title is over the maximum word count.') ); 
} 
add_action('publish_post', 'maxWord');

Display Random Posts

This is super useful for breathing life into old posts. When you implement this code, absolutely random posts will be listed in the area you specify. This is great for getting traffic to older posts and some of your more evergreen content. To display random posts, place the following snippet.

<ul><li><h2>A random selection of my writing</h2> 
<ul> 
<?php 
$rand_posts = get_posts('numberposts=5&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;orderby=rand'); 

foreach( $rand_posts as $post ) : 
?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endforeach; ?> 
</ul> 
</li></ul>

Don’t use a plugin! Show related posts in any theme by adding this to your functions.php file:

<?php 
$tags = wp_get_post_tags($post->ID); 

if ($tags) { 
echo 'Related Posts'; 
$first_tag = $tags[0]->term_id; 
$args=array( 
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>1,
'caller_get_posts'=>1 
); 
$my_query = new WP_Query($args); 

if( $my_query->have_posts() ) { 

while ($my_query->have_posts()) : $my_query->the_post(); ?> 
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 
<?php 

endwhile; wp_reset(); 
} 
} 
?>

Redirect a Commenter to a Thank You Page

This can be extremely useful for those who want to promote interaction on their site. After someone comments, they’ll be taken to a thank you page, where you can encourage more interaction, such as liking your facebook page or following you on Twitter. Just add this snippet to your functions.php file:

add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment(){
      wp_redirect('/thank-you-page/');
      exit();
}

Add a Paypal Donation Link

Believe it or not, I have gotten many requests for a Paypal donation link, so people could give me a tip for my information. There’s nothing to lose by adding this donation link on your site. All i takes is a little code:

function donate_shortcode( $atts ) {
   			 extract(shortcode_atts(array(
       			 'text' => 'Make a donation',
       			 'account' => 'REPLACE ME',
       			 'for' => '',
  			  ), $atts));

   			 global $post;

    		if (!$for) $for = str_replace(" ","+",$post->post_title);

   			return '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation+for+'.$for.'">'.$text.'</a>';

}
add_shortcode('donate', 'donate_shortcode');

What do you think about these snippets? They are extremely handy, and you’ll find them extremely useful for adding functionality to your site. If you’ve found an awesome snippet, feel free to leave it in the comments section below.

Select Language