How to Display Total Published Words in the Right Now Admin Dashboard in WordPress

Home » Snippets » How to Display Total Published Words in the Right Now Admin Dashboard in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

As a content creator or website owner, it’s important to keep track of your publishing progress and milestones. One way to do this is by monitoring the total number of words published on your website. This can help you set goals, track growth, and even motivate you to keep creating content. In this article, we’ll show you how to display the total published words on your website in the Right Now dashboard widget, providing you with an easy way to keep track of your progress.

					function wpturbo_display_published_words_dashboard() {
    $args = array(
        'post_status' => 'publish',
        'post_type' => 'post',
        'posts_per_page' => -1,
        'fields' => 'ids',
    );
    $query = new WP_Query($args);
    $words = 0;
    foreach($query->posts as $id) {
        $content = get_post_field('post_content', $id);
        $words += str_word_count(strip_tags($content), 0);
    }
    ?>
    <div class="wpturbo-published-words">
        <p><?php echo $words . __( ' words published', 'wpturbo' ); ?></p>
    </div>
    <?php
}
add_action( 'right_now_content_table_end', 'wpturbo_display_published_words_dashboard' );
				

This code snippet allows you to display the total number of words published on your WordPress site in the Right Now section of your admin dashboard.

The wpturbo_display_published_words_dashboard() function initializes a new WP_Query object with the appropriate arguments to find all published posts.

Then, the function initializes a words variable to 0 before looping through each post. The str_word_count() function is used to count the number of words in each post. Since the HTML tags are removed from the post content using the strip_tags() function, the resulting word count only includes the text contents of each post.

Finally, the total word count is echoed out in an HTML div element with a class of wpturbo-published-words.

The add_action() function adds the wpturbo_display_published_words_dashboard() function to the right_now_content_table_end action hook, which ensures that the word count is displayed in the correct section of the admin dashboard.

By adding this code snippet to your functions.php file, you can now keep track of the total number of words published on your WordPress site in real-time.

Register an account to save your snippets or go Pro to get more features.