Share it!

The posts list and management page in WordPress is where you see the list of posts, pages, etc. along with some basic info: the post’s title, author, categories, etc. These details give blog managers a quick overview of the posts and lets them manage them (add, edit, trash, etc.). Under normal circumstances, this information is enough to locate and manage posts.

However, you might want to add new information to this page, such as related posts (when using custom posts this can be very handy), statistics, etc. This article will show you how to do that and a few more tricks.

Let’s say you want to know the number of words in each WordPress post without having to click on every single one on them. Being able to see that piece of information on the post list page could be very useful, wouldn’t it? Now, here’s how.

First things first! Before editing your theme files, make sure you have a backup in a safe place. Just to avoid any unpleasant surprises.

Count the words

In order to know how many words are in a given post, you can use str_word_count() function. This function takes a string and returns the number of words in it. We can apply that function to the post content and get the word count. However, as posts contain HTML tags, we need to strip them out to get a more accurate result. For that, we use WordPress’ strip_shortcodes() and PHP’s strip_tags(). For that, we can develop a custom function that takes a post’s ID and returns the number of words in the post (you should paste it on your theme’s functions.php file).

<?php
/**
* Count Post Words.
*
* Custom function that takes a WordPress post ID and returns the number of words in the post.
* You should paste it on your theme's functions.php file.
*
* @link https://imelgrat.me/wordpress/customize-wordpress-post-management-page/
*
* @param int $post_id The Post ID
*
* @return int The word count.
*/
function post_word_count($post_id) {
$content = get_post_field( 'post_content', $post_id );
$word_count = str_word_count( strip_tags( strip_shortcodes($content) ) );
return $word_count;
}
?>

Adding the custom column to WordPress

Now that we know how to count words, we need to add a column to the posts list page. To accomplish that, we must register a new column by creating a ‘filter’ and an ‘action’ to register and then display the new column’s info. If you are wondering about the difference between WordPress actions and filters, you can read about them here. The full code (including the word-count function) would be like this:

<?php
/**
* Add a word-count column to WordPress Posts List (Dashboard view).
*
* Custom code for adding a new column to the posts list.
* The new column will contain the number of words in the post
*
* @link https://imelgrat.me/wordpress/customize-wordpress-post-management-page/
*/
function post_word_count($post_id)
{
$content = get_post_field('post_content', $post_id);
$word_count = str_word_count(strip_tags(strip_shortcodes($content)));
return $word_count;
}
add_filter('manage_posts_columns', 'wordcount_column');
function wordcount_column($columns)
{
$columns['wordcount'] = 'Word count';
return $columns;
}
add_action('manage_posts_custom_column', 'show_wordcount');
function show_wordcount($name)
{
global $post;
switch ($name) {
case 'wordcount':
$wordcount = post_word_count($post->ID);
echo $wordcount;
}
}

This first function is the word-count function. The second part of the code creates a filter for the manage_posts_columns, a filter applied to the columns shown on the manage posts screen. We use it to add a column named “wordcount” and then return the updated column array.

The last piece of code is an action triggered when you are on the posts management page and columns are displayed. Here’s where we actually display the word count for the post. Because this action is triggered for every column in each post, we need to check that we’re actually on the right column before displaying anything. Otherwise, it would show the word-count in each column, messing up the page’s content.

That’s it! We now have the word-count for each post on the blog.

Reordering columns

Adding a column like this would add the column at the end of the line, which in some cases might not be what’s required. In that case, what can be done is to re-create the columns list and insert the new column where we need it. For instance, let’s suppose we want the word-count column after the title column. In that case, the filter function would change to (the action function remains unchanged).

<?php
/**
* Reorder post-list columns, adding a word-count column after the title column.
*
* This code assumes a new column named "wordcount" was already.
* Check the link for the whole process.
*
* @link https://imelgrat.me/wordpress/customize-wordpress-post-management-page/
*
* @param array $defaults List of default post-list columns
*
* @return array The list of original columns plus the wordcount column
*/
function add_wordcount_column($defaults)
{
$new_columns = array(); // Create empty array to store new column order
foreach ($defaults as $key => $value)
{
$new_columns[$key] = $value; // Add columns to new array
if ($key == 'title')
{
// when we find the title column
$new_columns['wordcount'] = 'Word count'; // Add the word-count column after title column
}
}
return $new_columns;
}
add_filter('manage_posts_columns', 'add_wordcount_column'); // Register filter

 

 


Share it!