How to Customizing Admin Columns In WordPress

How to Customizing Admin Columns In WordPress
96 Views

One filter for the column’s placement and heading and one action for the column’s output for each post are the two hooks to take into account. The names of the hooks include the post type. Let’s examine each of them individually, starting with the filter.

Column filter

The administration of columns for admin panels comes first, including adding columns suited to your content type, eliminating unused columns, and rearranging columns. WordPress provides the Columns API to achieve this. Although it is also available for users and taxonomies, we’ll concentrate on the posts panel.

Adding, removing, and rearranging columns are all possible with the help of the two WordPress hooks manage_[post_type]_posts_columns and manage_[post_type]_posts_custom_column. Enter the post type you want to target in place of [post type]. Use manage page posts columns and manage_page_posts_columns, respectively, when dealing with pages.

<?php
// Add the custom columns to the post in post type:
add_filter( 'manage_post_posts_columns', 'cxc_custom_edit_post_columns' );
function cxc_custom_edit_post_columns($columns) {
	unset( $columns['author'] );
	$columns['image'] = __( 'Image', 'cxc-codexcoach' );
	$columns['post_author'] = __( 'Author', 'cxc-codexcoach' );
	$columns['publisher'] = __( 'Publisher', 'cxc-codexcoach' );
	$columns['verified'] = __( 'Verified', 'cxc-codexcoach' );

	return $columns;
}
?>
<?php
// Add the data to the custom columns for the post in post type:
add_action( 'manage_post_posts_custom_column' , 'cxc_custom_post_custom_column', 10, 2 );
function cxc_custom_post_custom_column( $column, $post_id ) {
	switch ( $column ) {

		case 'image' :
		echo get_the_post_thumbnail( $post_id, array(80, 80) ); 
		break;

		case 'post_author' :
		$terms = get_the_term_list( $post_id , 'post_author' , '' , ',' , '' );
		if ( is_string( $terms ) ){
			echo $terms;
		} else {
			_e( 'Unable to get author(s)', 'cxc-codexcoach' );
		}
		break;

		case 'publisher' :
		echo get_post_meta( $post_id , 'publisher' , true ); 
		break;

		case 'verified' :
		$verified = get_post_meta( $post_id, 'verified', true );
		if ( $verified ) {
			echo '<span style="color:green;">'; _e('Yes', 'cxc-codexcoach'); echo '</span>';
		} else {
			echo '<span style="color:red;">'; _e('No', 'cxc-codexcoach'); echo '</span>';
		}
		break;

	}
}
?>

Removing a column

Simple steps to delete a column from a post type include filtering posts columns, removing the element from the array, and returning the element. The column output hook is not necessary for you to use. For instance, deleting the default “author” column from the “post” post type.

<?php
add_filter( 'manage_post_posts_columns', 'cxc_custom_post_remove_column' ) ;
function cxc_custom_post_remove_column( $columns ) {
	unset( $columns['author'] );
	return $columns;
}
?>

Changing default columns names or position

Let’s say we want to change the default column name “Author” to “Publisher” and we have a custom post type called “post”. We merely apply a filter to the columns and change the value of the key “author”:

<?php
add_filter( 'manage_post_posts_columns', 'cxc_custom_post_change_label_column' ) ;
function cxc_custom_post_change_label_column( $columns ) {
	$columns['author'] = __( 'Publisher', 'cxc-codexcoach' );
	return $columns;
};
?>

Reordering columns

Using PHP array functions, you may rearrange columns. Remember that the final array needs to be an associative array with the column “IDs” serving as keys and the labels serving as values. Here is a straightforward illustration of how to rearrange the columns by removing the “author” column and adding it at the end:

<?php
add_filter( 'manage_post_posts_columns', 'cxc_custom_post_change_reordering_column' );
function cxc_custom_post_change_reordering_column( $columns ) {
	$reordering_column = $columns['author'];
	unset( $columns['author'] );
	$columns['author'] = $reordering_column;
	return $columns;
}
?>

custom column as sortable

The post title, the number of comments, and the date are just a few of the columns in WordPress that can be sorted by default. You can make your own column sortable, but doing so necessitates writing a little bit extra code and hooking into WordPress’ post query hook to instruct it how to order by your post meta.

Assuming we have a custom post type called “post,” we can create a custom column using the following code that shows the custom post meta “author.”

<?php
add_filter( 'manage_edit-post_sortable_columns', 'cxc_manage_post_sortable_columns'  );
function cxc_manage_post_sortable_columns( $columns ) {
	$columns['author'] = 'author';
	return $columns;
}

add_action( 'pre_get_posts', 'cxc_pre_get_posts_author' );
function cxc_pre_get_posts_author( $query ) {
	if (!is_admin()) {
		return;
	}

	$orderby = $query->get('orderby');
	if ( $orderby == 'author' ) {
		$query->set('meta_key', 'author');
		$query->set('orderby', 'meta_value_num');
	}
}
?>

Disable sorting for default columns

It’s not too difficult to disable sorting for a default column. All we need to do is delete the column from the array that we don’t want to be sorted on by hooking into the filter manage_edit-{$post_type}_sortable_columns. For the post type “post,” for instance, this eliminates sorting for the date column.

<?php
add_filter( 'manage_edit-post_sortable_columns', 'cxc_manage_remove_post_sortable_columns' );
function cxc_manage_remove_post_sortable_columns( $columns ) {	
	unset( $columns['author'] );
	return $columns;
}
?>

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.