While to help you out in numerous issues revolving all around your WordPress blog, there are hundreds of thousands of plugins available out there, but still what, one definitely need something more. If you have a little (fine) or intermediate (good) or an expert (perfect) knowledge of playing with codes, then the short codes (also known as snippets) will help you ease your job while dealing with issues surrounding your blog. In this article, we have collected such ‘must’ required WordPress snippets. Hope they will be useful for you.
Prevent posts from a particular category to be displayed in WordPress loop
Often times come when you want to exclude certain category of posts from display on the WordPress loop. For excluding such categories, all you have to do is just copy and paste the code below into your functions.php file. But make sure you need to update the code with category ID you want to exclude on line 4.
1 2 3 4 5 6 7 8 9 10 11 |
function preventHomepageTips($query) { if($query->is_home() && $query->is_main_query()) { $query->set('cat', '-40'); // 40 is Quick Tips's category ID } } add_action('pre_get_posts', 'preventHomepageTips'); |
Source: David Walsh
Search within a specific post type only
The following code will allow you to search only within a specific post type. Just copy and paste the snippet in your theme functions.php file and simply update the code with your post type name on line 4 and you are done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function SearchFilter($query) { if ($query->is_search) { // Insert the specific post type you want to search $query->set('post_type', 'feeds'); } return $query; } // This filter will jump into the loop and arrange our results before they're returned add_filter('pre_get_posts','SearchFilter'); |
Source: Hongkiat
Prevent WordPress from asking FTP credentials
Paste the following line in your wp-config.php file and you will be able to prevent WordPress from asking you yours site FTP credentials. The wp-config.php file is located in the root of your WordPress install.
1 |
define('FS_METHOD', 'direct'); |
Source: WP Tuts+
Secure your WordPress blog uploads directory
Adding the following code to your newly created file named .htaccess will let you secure your WordPress blog upload directory. The code will let only accept images files, while if in case you want to add any other file type, then make sure you add the file extension to the list on line 5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Files ~ ".*..*"> Order Allow,Deny Deny from all </Files> <FilesMatch ".(jpg|jpeg|jpe|gif|png|tif|tiff)$"> Order Deny,Allow Allow from all </FilesMatch> |
Source: Jeff Starr
Easily get post content by ID
You often need to get the content or title from a specific post. The following code will display the content of the post with id $post_id.
1 |
echo get_post_field('post_content', $post_id); |
Source: WordPress Codex
Display a custom message on WordPress registration page
You often like to display a custom message on your registration page. For doing this, all you have to do is paste the code below in your functions.php file with updating your message on line 6.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
add_action('register_form', 'register_message'); function register_message() { $html = ' <div style="margin:10px 0;border:1px solid #e5e5e5;padding:10px"> <p style="margin:5px 0;"> Joining this site you agree to the following terms. Do no harm! </p> </div>'; echo $html; } |
Source: Kevin Chard
Empty trash on a daily basis automatically
Simply open your wp-config.php file and add the following line of code and you will notice that WordPress automatically empty the trash on daily basis.
1 |
define('EMPTY_TRASH_DAYS', 1); |
Photo Credit: Flickr/Lindsey Bieda