Adding Search Field To Your Navigation Menu Automatically
Simply copy and pasting the WordPress snippets below in your theme’s functions.php file, you will be able to add search field to navigation menu automatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
add_filter('wp_nav_menu_items','add_search_box', 10, 2); function add_search_box($items, $args) { ob_start(); get_search_form(); $searchform = ob_get_contents(); ob_end_clean(); $items .= '<li>' . $searchform . '</li>'; return $items; } |
Snippet Source/Credit: Vanweerd.com
Display A Thumbnail Of Any Website
Simply copy and paste the following snippet in your theme’s functions.php file, you will be able to display a thumbnail of any website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function wpr_snap($atts, $content = null) { extract(shortcode_atts(array( "snap" => 'http://s.wordpress.com/mshots/v1/', "url" => 'http://www.catswhocode.com', "alt" => 'My image', "w" => '400', // width "h" => '300' // height ), $atts)); $img = '<img src="' . $snap . '' . urlencode($url) . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '"/>'; return $img; } add_shortcode("snap", "wpr_snap"); |
Once done, you can use the snap shortcode:
1 |
[snap url="http://www.justwp.org" alt="My description" w="400" h="300"] |
Snippet Source/Credit: Greekeries.fr
Displaying A Thumbnail From A YouTube Video Using A shortcode
If you often add YouTube video to your website, then definitely you wish to display a thumbnail from a YouTube video. This can be done simply by using the snippet below in your theme’s 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 26 27 28 29 30 31 32 33 34 35 |
/* Shortcode to display youtube thumbnail on your wordpress blog. Usage: [youtube_thumb id="VIDEO_ID" img="0" align="left"] VIDEO_ID= Youtube video id img=0,1,2 or 3 align= left,right,center */ function wp_youtube_video_thumbnail($atts) { extract(shortcode_atts(array( 'id' => '', 'img' => '0', 'align'=>'left' ), $atts)); $align_class='align'.$align; return '<img src="<a href="http://img.youtube.com/vi/'.$id.'/'.$img.'.jpg"" rel="nofollow">http://img.youtube.com/vi/'.$id.'/'.$img.'.jpg"</a>; alt="" class="'.$align_class.'" />'; } add_shortcode('youtube_thumb', 'wp_youtube_video_thumbnail'); |
Once done, all you have to do is to use the shortcode below:
1 |
[youtube_thumb id="rNWeBVBqo2c" img="0" align="center"] |
Snippet Source/Credit: Gunay
How To Add An Extra Contact Methods To User Profiles
By default, WordPress lets you input some of your contact information on your user profile page but what if, you want to have some more fields there. Well, it is not a difficult thing to accomplish if you want to have it. Just paste the following snippet to your theme’s functions.php file but remember when you add the snippet, you do edit lines 4 and 5 as per your requirements.
1 2 3 4 5 6 7 8 9 10 11 |
add_filter('user_contactmethods', 'my_user_contactmethods'); function my_user_contactmethods($user_contactmethods){ $user_contactmethods['twitter'] = 'Twitter Username'; $user_contactmethods['facebook'] = 'Facebook Username'; return $user_contactmethods; } |
Snippet Source/Credit: TutsPlus
How To Automatically Add Post Name To The Body Class
In order to automatically add post name to the body class, all you have to do is to copy the following theme and paste it on your theme functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function wpprogrammer_post_name_in_body_class( $classes ){ if( is_singular() ) { global $post; array_push( $classes, "{$post->post_type}-{$post->post_name}" ); } return $classes; } add_filter( 'body_class', 'wpprogrammer_post_name_in_body_class' ); |
Snippet source/Credit: WPProgrammer.com
How To Automatically notify your members on new posts
Whenever you publish a new post in your blog, you do wish to notify your members. Well this can be done automatically by simply copying and pasting the snippet below to your theme’s functions.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function email_members($post_ID) { global $wpdb; $usersarray = $wpdb->get_results("SELECT user_email FROM $wpdb->users;"); $users = implode(",", $usersarray); mail($users, "New WordPress recipe online!", 'A new recipe have been published on http://www.wprecipes.com'); return $post_ID; } add_action('publish_post', 'email_members'); |
Snippet Source/Credit: WordPress Codex