Home / admin / Display total published words in “Right Now” admin dashboard
  • How to Display total published words in “Right Now” admin dashboard

    display-total-published-words-in-right-now-admin-dashboard

    Adding this snippet to the functions.php of your wordpress theme will display the total number of published words from posts and pages. This is displayed at the bottom of the “Right Now” box in your wordpress admin dashboard.

    ( example screenshot ) ( click code to copy )

    wordpress snippet : PHP

    <>
    function post_word_count() {
        $count = 0;
        $posts = get_posts( array(
            'numberposts' => -1,
            'post_type' => array( 'post', 'page' )
        ));
        foreach( $posts as $post ) {
            $count += str_word_count( strip_tags( get_post_field( 'post_content', $post->ID )));
        }
        $num =  number_format_i18n( $count );
        $text = _n( 'Word', 'Words', $num );
        echo "<tr><td class='first b'>{$num}</td><td class='t'>{$text}</td></tr>";
    }
    add_action( 'right_now_content_table_end', 'post_word_count');
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_post, get_post_field, get_posts, add_action, _n, _e, wp,
  • Andre23

    Kevin, if I want to display the words in a custom widgets, how can I do?

    • Hi Andre23 well the reason this snippet puts the information within “right now” is because of the add_action. eg

      add_action( ‘right_now_content_table_end’, ‘post_word_count’);

      So if you remove that last line you could write your widget and call the same function to display within your widget. You will want to update the html so that it will work with your widget,

  • Andre23

    Hi Kevin, I don’t understand what is the last line, I’ve this situation:

    // Create a custom widget function welcome() { echo ‘…………….’; } function register_widgets(){ wp_add_dashboard_widget(‘dashboard_widget’, ‘Hello’, ‘welcome’); } add_action(‘wp_dashboard_setup’, ‘register_widgets’);

    • Hi Andre23, I posted something that should work for you, this will add the word count within a custom dashboard widget. http://pastebin.com/hDnereaS

      Just copy and paste within the function.php

  • Andre23

    Thanks Kevin, now works very well, thank you for this code, it help me.

  • Ben

    Can you show us how to display how many media images are in the libary in right now too please :( i tried mixing both snipps on this website and broke my website at least 6 times. Please help :)