Some very useful wordpress hacks

Today i organize some very useful WordPress hacks.Hope this may help you in your future projects

Display number of Facebook fans in full text

If you want to display the number of your Facebook Fans then use this code.

Replace “YOUR PAGE-ID” with your own Page-ID.


$page_id = "YOUR PAGE-ID"; $xml = @simplexml_load_file("".$page_id."") or die ("a lot"); $fans = $xml--->page->fan_count;
echo $fans;


Display search terms from Google users

If a visitor reached your site through Google’s search, this script will display the terms they searched for in order to find your site. Put this code where you want to display the message.

$refer = $_SERVER["HTTP_REFERER"];
if (strpos($refer, "google")) {
	$refer_string = parse_url($refer, PHP_URL_QUERY);
	parse_str($refer_string, $vars);
	$search_terms = $vars['q'];
	echo 'Welcome Google visitor! You searched for the following terms to get here: ';
	echo $search_terms;


How to automatically email contributors when their posts are published

If you’re running a multi-authored blog, it can be very cool to let your contributors know when their post are online. Today’s recipe will show you how to do this automatically each time a post is published.

function wpr_authorNotification($post_id) { $post = get_post($post_id); $author = get_userdata($post->post_author); $message = " Hi ".$author->display_name.", Your post, ".$post->post_title." has just been published. Well done! "; wp_mail($author->user_email, "Your article is online", $message); } add_action('publish_post', 'wpr_authorNotification');


How to define a minimum word count per post

If you want to be able to keep a minimum word count for your posts, then just read this recipe. Applying it to your blog can be useful to maintain a clean layout, or ensure your guest writers will not post too short articles on your blog.

function minWord($content){ global $post; $num = 100; //set this to the minimum number of words $content = $post->post_content; if (str_word_count($content) < $num) wp_die( __('Error: your post is below the minimum word count.') ); } add_action('publish_post', 'minWord');


Automatically add a Google+ button to your posts

This simple piece of code to automatically add a Google+ button to your posts.

Open your functions.php file and paste the following code in it:

add_filter('the_content', 'wpr_google_plusone'); function wpr_google_plusone($content) { $content = $content.'<div><g:plusone size="tall" href="'.get_permalink().'"></g:plusone></div>'; return $content; } add_action ('wp_enqueue_scripts','wpr_google_plusone_script'); function wpr_google_plusone_script() { wp_enqueue_script('google-plusone', '', array(), null); }


Add favicon icon dynamically in your wordpress theme

You can easily add favicon icon dynamically in your WordPress theme.
This may be very useful for child theme where you don’t want to edit the parent theme header.php.
Paste this code in your theme function.php to load the favicon dynamically and also don’t forget to change the favicon icon url.

function dynamic_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="" />';
add_action('wp_head', 'dynamic_favicon');

Hope this piece of code may help you on your next project

How to add facebook likebox in wordpress post or page using shortcode

If you want to show your facebook fanpage likebox in wordpress post or page then copy and past the code below in your theme function.php

function fb_likebox($atts) {

        "fb_link" => ''
    ), $atts));

	return '<h2 class="join-fb">Like Them On Facebook</h2>
<fb:like-box href="'.$fb_link.'" width="595" height="100" show_faces="false" stream="false" header="false"></fb:like-box>';
add_shortcode('fb_box', 'fb_likebox');

You can change the attribute of the facebook like box

  • width – the width of the plugin in pixels. Default width: 300px.
  • height – the height of the plugin in pixels.
  • colorscheme – the color scheme for the plugin. Options: ‘light’, ‘dark’
  • show_faces – specifies whether or not to display profile photos in the plugin. Default value: true.
  • stream – specifies whether to display a stream of the latest posts from the Page’s wall
  • header – specifies whether to display the Facebook header at the top of the plugin.
  • border_color – the border color of the plugin.

And also past this code after the tag in your header.php theme file

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); = id;
  js.src = "//";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

Then use this shortcode to show facebook fanpage on page or post

[fb_box fb_link=""][/fb_box]