sinanisler logo

Calculate all WordPress Posts How Many Words there are

add this to your functions.php and it will add Word Count named page to the dashboard and it will show the total word count for all posts.

 

// Add a custom page to the WordPress admin menu
function word_count_admin_menu() {
    add_menu_page(
        'Word Count',
        'Word Count',
        'manage_options',
        'word-count',
        'word_count_admin_page',
        'dashicons-editor-spellcheck',
        20
    );
}
add_action('admin_menu', 'word_count_admin_menu');

// Display the total word count of all posts in the custom admin page
function word_count_admin_page() {
    global $wpdb;
    
    $total_word_count = 0;
    $results = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'");

    foreach ($results as $result) {
        $word_count = str_word_count(strip_tags($result->post_content));
        $total_word_count += $word_count;
    }
    ?>
    <div class="wrap">
        <h1>Total Word Count</h1>
        <p>The total number of words in all posts: <?php echo esc_html($total_word_count); ?></p>
    </div>
    <?php
}

 

Leave the first comment