Skip to content

Instantly share code, notes, and snippets.

@imelgrat
Created February 15, 2019 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imelgrat/9ce9d97ad2dbfb7ebccd42a3fb586903 to your computer and use it in GitHub Desktop.
Save imelgrat/9ce9d97ad2dbfb7ebccd42a3fb586903 to your computer and use it in GitHub Desktop.
Reorder WordPress post-list columns, adding a word-count column after the title column.
<?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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment