WordPress Posts Counter Widget

I have created a small widget to show total no of posts on your blog.You just need to copy this code and save the file in plugins folder and activate plugin.

After that you can drag and drop widget in your theme.

Plugin Name: Posts Counter
Description: A plugin which allows you to show total number of Posts on your blog.
Author: Kapil Chugh
Author URI: https://kapilchugh.wordpress.com/
Plugin URI: http://mediologysoftware.com/
Version: 0.1
add_action( 'widgets_init', 'add_posts_counter' );
function add_posts_counter() {
register_widget( 'WP_Widget_Posts_Counter' );
class WP_Widget_Posts_Counter extends WP_Widget {
function WP_Widget_Posts_Counter() {
$widget_ops = array('classname' => 'widget_featured_entries', 'description' => __( "This widget will show total no of posts on your site") );
$this->WP_Widget('posts_counter', __('Posts Counter'), $widget_ops);
$this->alt_option_name = 'posts_counter_widget';
function widget($args, $instance) {
$title = apply_filters('widget_title', empty($instance['title']) ? __('Posts Counter') : $instance['title'], $instance, $this->id_base);
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
$num_posts = wp_count_posts( 'post' );
echo 'Total Posts ' . $num_posts->publish;
echo $after_widget;
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
function form( $instance ) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : ''; ?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>

About Kapil Chugh

In love with PHP and tweaking WordPress. Hungry for Open Source and new technologies. Honest. God-fearing. Happiest when solving problems.
This entry was posted in WordPress and tagged , . Bookmark the permalink.

13 Responses to WordPress Posts Counter Widget

  1. thebigjay says:

    Silly question, do we save the file as what? .php I’m assuming???

  2. Marek says:


    I don’t know much about coding, maybe you can give me an example, if i wanna add one field to widget, which lets me to insert some number. And prints out finally something like that.

    Post count: xx Totalling xx(changeable number on widget) offers.


  3. Cool, it’s working. visit my site, http://amarexcepana.uni.me the site use your plugin. Thanks

  4. hey how to learn start plugin developing in word press.

  5. Alex Hoffman says:

    correct change to
    echo ” . ‘Total Posts ‘ . $num_posts->publish . “”;

  6. Alex Hoffman says:

    This is fantastic! I have to keep a blog for my graphic design thesis and this is a great way to show off how many posts I’m doing. I really wanted to assign an ID to the actual “Total Posts #” element so I could give it some fancy styling, I used the paragragh tag but feel free to change it to whatever you’d like. I edited this tid bit of code on line 103..


    echo ‘Total Posts ‘ . $num_posts->publish;


    echo ” . ‘Total Posts ‘ . $num_posts->publish . “”;

    and then just call out #posts-num in your css and go crazy.

  7. jonathan says:

    hi, might sound stupid, but where can i find this plugin to downliad, can’t find it, thanks

  8. It worked fine for me… Thanks for this…

  9. Kapil Chugh says:

    Please check is there any extra space before <?php.

    More information can be found here

  10. Andre says:

    cool, but I get errors whem trying to run your widget:

    Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/example.com/httpdocs/site/wp-content/plugins/posts_counter.php:83) in /var/www/vhosts/example.com/httpdocs/site/wp-content/plugins/qtranslate/qtranslate_core.php on line 71

    So the plugin is not working nicely with the qTranslate plugin. Would there be a way of fixing this?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s