How to Display Total Pingbacks and Trackbacks Within Admin Post Columns in WordPress

Home » Snippets » How to Display Total Pingbacks and Trackbacks Within Admin Post Columns in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever wanted to see the total number of pingbacks and trackbacks for a specific post directly in the WordPress admin post columns? This information can be useful to track engagement and the reach of your content. In this article, we’ll guide you through the process of adding the total number of pingbacks and trackbacks to the post columns within your WordPress dashboard.

					function wpturbo_add_pingback_column( $columns ) {
    $columns['pingbacks'] = __( 'Pingbacks', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_post_posts_columns', 'wpturbo_add_pingback_column' );

function wpturbo_display_pingback_count( $column_name, $post_id ) {
    if ( $column_name === 'pingbacks' ) {
        $pingbacks = get_comments( array(
            'post_id'   => $post_id,
            'type'      => 'pingback',
            'count'     => true
        ) );
        echo $pingbacks;
    }
}
add_action( 'manage_post_posts_custom_column', 'wpturbo_display_pingback_count', 10, 2 );
				

The purpose of this code snippet is to add a new column to the WordPress admin posts screen that displays the total number of pingbacks received for each post. This is achieved by creating two new functions for WordPress to use in the manage_post_posts_columns and manage_post_posts_custom_column hooks.

Firstly, we define a new column called pingbacks and set its label to "Pingbacks" in the wpturbo_add_pingback_column() function:

function wpturbo_add_pingback_column( $columns ) {
    $columns['pingbacks'] = __( 'Pingbacks', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_post_posts_columns', 'wpturbo_add_pingback_column' );

Then we hook this function into the manage_post_posts_columns filter hook, which is used to add or remove columns from the posts screen. This will allow WordPress to recognise the pingbacks column and display it in the posts table.

Next, we create the wpturbo_display_pingback_count() function which is responsible for outputting the total number of pingbacks for each post in the correct column. This function is hooked into the manage_post_posts_custom_column action, which allows us to render the custom column content for each post row in the table.

Within the wpturbo_display_pingback_count() function, we first check if the current column being rendered is our custom pingbacks column by verifying the $column_name parameter. Then, if the column is our custom pingbacks column, we use the built-in get_comments() function to query the total number of pingbacks for the current post. We pass an array of arguments to the function specifying the post ID, the comment type as pingback, and the count flag set to true to return the total number rather than an array of comments.

Finally, we output the total number of pingbacks for the post using echo. This will display the pingback count in the correct pingbacks column for each post in the posts table on the WordPress admin screen.

Overall, this code snippet demonstrates how to add a custom column to the WordPress admin posts screen and how to populate it with dynamic content using functions hooked into the appropriate actions and filters.

Register an account to save your snippets or go Pro to get more features.