Short Codes – the way to solve small issues in your WordPress blog. While there are plenty of plugins available out there to help you out with numerous small and big WordPress roles but sometimes and sometimes even more often using the short codes will help you out more. In this article, we present the second part of three-part series of WordPress Snippets Collection.
Suggested Reading: WordPress Snippets Collection Part – 1
How to automatically insert a list of related articles below the post
Copy and paste the following code into the functions.php file and once you are done, open your single.php file and call the function using the second code below.
Code 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
// "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; } |
Code 2:
1 |
<?php echo wptuts_more_from_cat( 'More From This Category:' ); ?> |
Source: WP Tuts
Adding nofollow attributes to all links in a specific category
Copy the code below and paste it on your functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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'); |
Source: Sagive
Automatically spam comments with a very long url
Adding the code to your functions.php file will automatically mark as spam all comments with an url longer than 50 chars.
1 2 3 4 5 6 7 8 9 10 11 |
<?php 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: CSS Tricks
Translatable JavaScript strings on your WordPress theme
Simply paste the following code into your function.php file, where you generally enqueue scripts and styles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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' ) ) ); }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' ) ) ); } |
Source: WP Theming
Adding SVG upload support to your WordPress blog
Simply add the code below to functions.php in your WordPress theme.
1 2 3 4 5 6 7 8 9 |
add_filter('upload_mimes', 'my_upload_mimes'); function my_upload_mimes($mimes = array()) { $mimes['svg'] = 'image/svg+xml'; return $mimes; } |
Source: dbushell
Create a directory within the uploads folder
Adding this code snippet on your functions.php file (or plugin file if you’re creating a plugin)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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' ); |
Source: WP Mayor
Display an author bio excerpt
Paste the following code into your functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php 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; } ?> |
Source: SEO Review Tools
Detect a comments page
Adding the following code anywhere on your theme files will return true if you’re on a comment page.
1 2 3 4 5 6 7 |
$cpage = get_query_var( 'cpage' ); if ( is_singular() && $cpage > 0 ){ // Your code here } |
Source: SEOMix
Force your WordPress blog to break out of frames
Just paste the code below into your functions.php file, save it, and you’re done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// 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'); |
Source: WP Mix
Redirect to post if search results only returns one post
Paste the following code snippet into your functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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 ) ); } } } |
Source: Paulund.co.uk
Display the author posts in the admin post list
Adding the code into your functions.php file will let authors only see their own posts in the admin post list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php function mypo_parse_query_useronly( $wp_query ) { if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) { if ( !current_user_can( 'level_10' ) ) { global $current_user; $wp_query->set( 'author', $current_user->id ); } } } add_filter('parse_query', 'mypo_parse_query_useronly' ); ?> |
Source: WP Snippets
To run the loop outside of WordPress
Paste the following code on any PHP file where you want to run your WordPress loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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: CSS Tricks
Allow more HTML tags in WordPress comments
Simply paste the following code in your functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Create function which allows more tags within comments function allow_pres() { global $allowedtags; $allowedtags['pre'] = array('class'=>array()); } // Add WordPress hook to use the function add_action('comment_post', 'allow_pres'); |
Source: David Walsh
Function to show a total share counter (FB, Twitter, G+)
Simply paste the following function where you want your counter to appear.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function social_shares() { $url = get_permalink( $post_id ); $json = file_get_contents("http://api.sharedcount.com/?url=" . rawurlencode($url)); $counts = json_decode($json, true); $totalcounts= $counts["Twitter"] + $counts["Facebook"]["total_count"] + $counts["GooglePlusOne"]; echo "<div>$totalcounts Share</div>"; } |
Source: Gleenk
Photo Credit: Flickr/Chris Hsia
Note: Like the post, then make sure you do follow and like us on both Twitter and Facebook.