While we discussed and talked about different short codes available out there in our first and second part of this three-part series, today we are going to dig more deeper and will find more snippets – the short codes – which will help you ease your job more while handling different things in your WordPress blog.
Exclude specific categories from your blog homepage
There comes time often when you want to limit the numbers of categories to be on display on the homepage. For doing this, just copy the shortcode below in your functions.php file with replacing 5 and 34 – the category IDs – with the ones you want to exclude.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function exclude_category_home( $query ) { if ( $query->is_home ) { $query->set( 'cat', '-5, -34' ); } return $query; } add_filter( 'pre_get_posts', 'exclude_category_home' ); |
Source: WP Mayor
Redirect your author archive link to your “About” page
By default, on clicking on your author posts URL, a user gets redirected to your author archive page. It is useful on multi-author blogs, but when you are alone an author on the blog, it’s useless. Adding the shortcode below in your functions.php file will take your readers will take your users to “About” page but also make sure you do replace the slug you used for “About” page on line 4.
1 2 3 4 5 6 7 |
add_filter( 'author_link', 'my_author_link' ); function my_author_link() { return home_url( 'about' ); } |
Source: WP Recipes
Prevent WordPress to display login errors
By default, when one fail to login on WP-Admin, WordPress displays an error message. It might works well for you as a blog owner but it gives a progress track for hackers. With using the short code into your functions.php file, you will be able to prevent WordPress from displaying login errors.
1 |
add_filter('login_errors', create_function('$a', "return null;")); |
Source: WP Tuts
Redirect WordPress frontend to the backend
Using the following short code will let you automatically redirect all the frontend part of the site to your WP-admin/login page. Just simply copy and paste the code in your theme’s header.php file.
1 2 3 |
<?php header("Location: ".get_admin_url()); exit(); ?> |
Source: Vladimir Prelovac
Shortcode to display external files on your posts
Adding the following shortcode to your functions.php file will let you display external files such as text or image on your posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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, add the following shortcode and you are done:
1 |
[show_file file="http://www.example.com/test.html"] |
Source: Vladimir Prelovac
Getting the first link in posts
Using the following snippet will allow you to link a post to somewhere else instead of providing content. Just copy and paste the code into your functions.php file and you are done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function get_content_link( $content = false, $echo = false ){ if ( $content === false ) $content = get_the_content(); $content = preg_match_all( '/hrefs*=s*["']([^"']+)/', $content, $links ); $content = $links[1][0]; if ( empty($content) ) { $content = false; } return $content; } |
Source: WP Snippets
Remove WordPress version number from pages and feeds
Using the shortcode in your theme’s functions.php file will let you remove WordPress version number from pages and feeds.
1 2 3 4 5 6 7 |
add_filter('the_generator', 'digwp_complete_version_removal'); function digwp_complete_version_removal() { return ''; } |
Source: Jeff Starr
Load jQuery in the footer
By default, WordPress loads the jQuery JavaScript framework at the top of your files, but using the following shortcode in your functions.php file, will let force WordPress to load jQuery in the theme footer.
1 2 3 4 5 6 7 8 9 |
function ds_print_jquery_in_footer( &$scripts) { if ( ! is_admin() ) $scripts->add_data( 'jquery', 'group', 1 ); } add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' ); |
Source: Dominik Schilling
Photo Credit: Flickr/Niels Heidenreich