How can I add columns to the post edit listing to show my custom post data?

When you click on “Posts” or “Pages”, you get a paged listing of your posts or pages with the following columns:

Title | Author | Categories | Tags | Date

I have a plugin which gives an SEO score for each post and page. I would like to add two columns to the list view when viewing posts or pages, one for the post’s “seo score” and one for the posts “seo keyword” so that the column listing becomes:

Title | Author | Categories | Tags | Date | SEO Score | SEO Keywords

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You add the column using the manage_posts_column filter, where you add two new array elements with a custom key name and the header name as the value.

add_filter('manage_posts_columns', 'wpse_3531_add_seo_columns', 10, 2);
function wpse_3531_add_seo_columns($posts_columns, $post_type)
{
    $posts_columns['seo_score'] = 'SEO score';
    $posts_columns['seo_keyword'] = 'SEO keyword';
    return $posts_columns;
}

The function that displays each row, _post_row(), then fires the manage_posts_custom_column action for each column that it does not know. You hook into this function to display your own data.

add_action('manage_posts_custom_column', 'wpse_3531_display_seo_columns', 10, 2);
function wpse_3531_display_seo_columns($column_name, $post_id)
{
    if ('seo_score' == $column_name) {
        echo 'SEO score for post with ID ' . $post_id;
    }
    if ('seo_keyword' == $column_name) {
        echo 'SEO keyword for post with ID ' . $post_id;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x