0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
// Add custom columns to the Posts list table.
add_filter( 'manage_posts_columns', 'wpturbo_add_custom_post_columns' );
function wpturbo_add_custom_post_columns( $columns ) {
$columns['word_count'] = __( 'Word Count', 'wpturbo' );
$columns['char_count'] = __( 'Character Count', 'wpturbo' );
return $columns;
}
// Populate custom columns with data.
add_action( 'manage_posts_custom_column', 'wpturbo_populate_custom_post_columns', 10, 2 );
function wpturbo_populate_custom_post_columns( $column, $post_id ) {
if ( 'word_count' === $column ) {
// Calculate the post's word count and output it.
$content = get_post_field( 'post_content', $post_id );
$word_count = str_word_count( strip_tags( $content ) );
echo esc_html( $word_count );
} elseif ( 'char_count' === $column ) {
// Calculate the post's character count and output it.
$content = get_post_field( 'post_content', $post_id );
$char_count = mb_strlen( strip_tags( $content ) );
echo esc_html( $char_count );
}
}
This code adds two custom columns to the Posts list table: 'Word Count' and 'Character Count', and then populates them with the number of words and characters, respectively, for each post.
You can add this code to your theme's functions.php file or create a custom plugin to add the functionality.
Note that this will only work for non-Gutenberg post types. If you are using Gutenberg, you'll need to create a separate block to display the word count and character count for each post.