Halloween Sale 15% off Ends in See the plans
PHP

Your Writing Stats Widget

Adds a personal dashboard widget showing how many posts and words you published this month and this year, plus your current number of drafts, a small motivation boost for writers.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'wp_dashboard_setup', function () {
    if ( ! current_user_can( 'edit_posts' ) ) {
        return;
    }

    wp_add_dashboard_widget( 'wpide_writing_stats', 'Your writing stats', function () {
        global $wpdb;

        $user_id = get_current_user_id();
        $stats   = get_transient( 'wpide_writing_stats_' . $user_id );

        if ( false === $stats ) {
            $year_start  = wp_date( 'Y-01-01 00:00:00' );
            $month_start = wp_date( 'Y-m-01 00:00:00' );
            $rows        = $wpdb->get_results( $wpdb->prepare(
                "SELECT post_date, post_content FROM {$wpdb->posts} WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' AND post_date >= %s",
                $user_id,
                $year_start
            ) );
            $stats = array( 'month_posts' => 0, 'month_words' => 0, 'year_posts' => 0, 'year_words' => 0 );
            foreach ( $rows as $row ) {
                $words = str_word_count( wp_strip_all_tags( strip_shortcodes( $row->post_content ) ) );
                $stats['year_posts']++;
                $stats['year_words'] += $words;
                if ( $row->post_date >= $month_start ) {
                    $stats['month_posts']++;
                    $stats['month_words'] += $words;
                }
            }
            $stats['drafts'] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_author = %d AND post_type = 'post' AND post_status = 'draft'", $user_id ) );
            set_transient( 'wpide_writing_stats_' . $user_id, $stats, HOUR_IN_SECONDS );
        }

        $cells = array(
            'Posts this month' => $stats['month_posts'],
            'Words this month' => $stats['month_words'],
            'Posts this year'  => $stats['year_posts'],
            'Words this year'  => $stats['year_words'],
            'Drafts waiting'   => $stats['drafts'],
        );
        echo '<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(110px,1fr));gap:8px">';
        foreach ( $cells as $label => $value ) {
            printf( '<div style="border:1px solid #dcdcde;border-radius:4px;padding:10px;text-align:center"><div style="font-size:20px;font-weight:600">%s</div><div style="color:#646970">%s</div></div>', esc_html( number_format_i18n( $value ) ), esc_html( $label ) );
        }
        echo '</div>';
    } );
} );

Notes

Words are counted from published post content; figures are cached per user for an hour.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.