Do you want to display total number of published words from posts and pages in your admin dashboard? While there’s probably a plugin for this, we have created a quick code snippet that you can use display total published words in “Right Now” admin dashboard in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
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');
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: How to create stunning WordPress optin forms and 7 best WordPress poll plugins to grow onsite engagement.
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 🙂
Thanks Kevin, now works very well, thank you for this code, it help me.
No problem Andre glad that I could help,
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
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,