What? What did you said on the title? Well, I am not sure this is an appropriate title. But I am going to explain it to you.
Lets say, you are making a theme or a WordPress plugin and you want to show something in the admin panel like this:
We will see how can you add those columns to the admin panel tables and manipulate data on the table rows. Well, this is very easy to do, because WP has a really nice API to work with.
Now we will add those two columns to our admin panel’s post listing table. Here is the code:
<?php function test_modify_post_table( $column ) { $column['test_budget'] = 'Budget'; $column['test_expires'] = 'Expires'; return $column; } add_filter( 'manage_posts_columns', 'test_modify_post_table' );
We are adding a filter on "manage_posts_columns"
. It passes the column names of the post table as an array. We simple add our custom columns with a key-value pair
in that array and return it. If you run this code, you can see how easy that was.
Now we have the columns, we need to fill those columns with actual data. We’ll use post custom fields to fill those space. Lets look at the code:
<?php function test_modify_post_table_row( $column_name, $post_id ) { $custom_fields = get_post_custom( $post_id ); switch ($column_name) { case 'test_budget' : echo $custom_fields['cf_budget'][0] . ' USD'; break; case 'test_expires' : echo $custom_fields['cf_expires'][0] . ' days'; break; default: } } add_action( 'manage_posts_custom_column', 'test_modify_post_table_row', 10, 2 );
We are adding another action at "manage_posts_custom_column"
with having two arguments. One is the column name and another is the post id. Then we are grabbing all the custom fields of the post and showing them according to the column name. The code is self explanatory. These columns will be visible to all of your "Post Type"
listings.
For Custom Post Type:
For custom post types, the above filter will be manage_{$post_type}_posts_columns
and the action will be manage_{$post->post_type}_posts_custom_column
.
So if you have a custom post type named tutorial
, for registering the column, use this filter manage_tutorial_posts_columns
. To show the value in the column, use this action hook manage_tutorial_posts_custom_column
.
For Pages:
For pages, those filter will be "manage_pages_columns"
and action is “manage_pages_custom_column
“. Simple enough.
User List table?
What if you want to change the user list table? It’s the same process as previous. We can add our custom columns like we did before. But the first filter name will be changed to manage_users_columns
. Here there is a slight change in the second filter. We passed two parameter for the posts, but here we will pass THREE
paramater. Here is the sample code to add a column "URL"
to show their website addresses:
<?php function test_modify_user_table( $column ) { $column['url'] = 'URL'; return $column; } add_filter( 'manage_users_columns', 'test_modify_user_table' ); function test_modify_user_table_row( $val, $column_name, $user_id ) { $user = get_userdata( $user_id ); switch ($column_name) { case 'url' : return $user->user_url; break; default: } return $return; } add_filter( 'manage_users_custom_column', 'test_modify_user_table_row', 10, 3 );
Hope you enjoyed 😀
Did u able to add sorting function for custom col in user ?
I didn’t tried it. But there should be some workaround.
ok I am close, the hook will be like manage_{$screen->id}_sortable_columns here for user listing the screen id is users and the hook name will be manage_users_sortable_columns
this plugin’s code may help http://plugins.svn.wordpress.org/recently-registered/trunk/recently-registered.php
please check working code http://pastebin.com/yFP9paia
Cool 😀
Hi pleas help me
I want to add few fields while posting in admin panel. My fields are like
a text box which has to be filled
a select box which will be populated from the table for example company
What I want is I want to put a column in the post table say for example “Add more” It will direct to a differnet page where I can add my values and the form will be like as I specified above
Please help me I am stucked
Thanks
One more question : Where to write these codes as I am new to wordpress
@SonalSharma You can write the codes in your themes functions.php or by creating a new plugin.
@SonalSharma I think it’s pretty straight forward and my codes are well explained. Just print the fields as I echo’d the budget/price in the codes.
Great information. Looks like just what I was looking for.
I would like to add the url to my user list table but not sure how to do it. I am new to coding and do not want to mess anything up. 🙂
Could you email me some detailed instructions on what, where and how to do this, or direct me to more information.
Thanks
Detailed instruction is already provided here. What do you want more?
If you rather not be coding, I might have a solution for you. I have just released a plugin which let’s you you manage the columns without any coding, hopefully it will be helpfull for you and others.
And great read btw Tareq! I have put the users columns on the todo list for a coming release.
You can find my Admin Column plugin here: http://wordpress.org/extend/plugins/codepress-admin-columns/
Cool plugin indeed, you did a great work 😎
Thanks! I also added User columns to the latest release ( 1.1.1 and up ). You can set the user url as a column now.
Creating interfaces for users is always a tedious job and you did wonderful indeed 😀
My 2 cents: First, the custom column row hook is an action, not a filter and second, to use that action into custom post types you should hook it to ‘manage_{post_type}_posts_custom_column’.
Hope it helps!
This is great but I want to add a column to the links table and not sure how to modify the code to do this. I’ve tried the code below and it doesn’t work, can anyone help?
function test_modify_links_table( $column ) {
$column[‘date’] = ‘Date Added’;
return $column;
}
add_filter( ‘manage_links_columns’, ‘test_modify_links_table’ );
function test_modify_links_table_row( $column_name, $link_id ) {
$custom_fields = get_bookmark( $link_id );
switch ($column_name) {
case ‘date’ :
echo $custom_fields[‘cf_date’][0];
break;
default:
}
}
add_filter( ‘manage_links_custom_column’, ‘test_modify_links_table_row’, 10, 2 );
After checking the `/wp-admin/includes/class-wp-links-list-table.php`, I don’t think that they provide any filter to modify the links list table
Thanks Tareq. That’s a shame.
I have discovered that there is an additional argument to pass to the get_bookmarks() function that supposedly brings back the updated date and time, so that functionality may already be there… http://codex.wordpress.org/Function_Reference/get_bookmarks
thanks for this tutorials.
i want to add custom comment meta in comment listing in admin ……can you suggest me how can i do that.????
Try this plugin
Thanx Tareq it’s working
Hi,
thanks for the code! Works quit nice! Except for one thing:
I use the following code to add the users appartment number to the user table.
function test_modify_user_table ($column) {
$column['hnumber'] = __('House number', 'coolhaven');
return $column;
}
add_filter('manage_users_columns', 'test_modify_user_table');
function test_modify_user_table_row ($val, $column_name, $user_id) {
$user = get_userdata($user_id);
if ($column_name == 'hnumber') {
return $user->number;
}
}
add_filter('manage_users_custom_column', 'test_modify_user_table_row', 10, 3);
The code does create a new collumn, but does not put any text in the appropriate fields. I am sure the right data is collected because when I print_r(); the $user->number, it displays the number somewhere on the page.
Could you please help me figure out what’s wrong?
Best regards,
Irian
They way you used filters, everything seems right. Not sure whats coming wrong.
I am wanting this to display on my 5 custom post types. How can I add more than 1 custom post type to the filter?
Add the filter to each custom post types.
Nice filter. Do you know how to add a new table after users table to show statistics about country of my users?
Thanks
Unfortunately, there isn’t any hook available below the users table.
Hi there, thanks for this tutorial!
Do you know how to add each user’s comment count as a sortable column?
Thank you in advance,
Roselle
Thanks for this post.
I one tables created by plugin I want retrieve data from this table and show in admin column.
Do you know how can I do it?
Great article, thank you!
I don’t know Is that a perfect place to ask or not, but I need to know. Suppose I have a table for logging my_site. And wp has wp-user table. How can I manage so that If user login to my_site , he does not need to log into my wp_site ! Is it possible or not !
I think that’s possible. WordPress uses cookies for logging-in a user, so check
wp_generate_auth_cookie()
in WordPress. You could manipulate a cookie like that and let logging-in the user.Can you help me where is it exactly ?! please.
Thanks, I have one question tho. How to change the order of the columns? Because now the date column is first and my custom columns are after that. I want my custom columns first and the date column as last one (which is the default).
In that case, instead of returning only the added columns via
manage_posts_columns
, you’ve to return the whole array by reordering on your own.So thats the only way?
I have one more question, is there any difference in using hooks like: (Im currently using these first ones:)
add_filter(‘manage_edit-slider_columns’, ‘rm_add_slider_columns’);
add_action(‘manage_posts_custom_column’, ‘rm_slider_columns’, 10, 2);
vs
add_filter(‘manage_posts_columns’, ‘rm_add_slider_columns’);
add_action(‘manage_{custom-post-type}_posts_custom_column’, ‘rm_slider_columns’, 10, 2);
Or which is the correct way to do this?
Seems like
manage_edit-{$post_type}_columns
is gone from core. Since I posted this tutorial, some name has been changed. I’ve just updated the names. Check the posts and custom post type action and filter names again.For me it works well with the manage-edit-….
The action Im using is the same as in your article:
add_action(‘manage_posts_custom_column’, ‘rm_slider_columns’, 10, 2);
But the filter works with manage-edit-.. I was looking for more tutorials and I’ve found that both cases are used. Do you know which is better to use?
It’s always good to look at the source code, here and here.
I would suggest using
manage_posts_columns
andmanage_posts_custom_column
.Thanks for the links, I now edited the code like this and it seems to work well:
add_filter(‘manage_slider_posts_columns’, ‘…’);
add_action(‘manage_slider_posts_custom_column’, ‘…’, 10, 2);
Hi Tareq,
Thx for the tutorial – very helpful.
I ultimately decided to go with the plug in /codepress-admin-columns/ instead of customizing too much on my own – I found it thx to you!
Thanks, great article!
Hi – in the user list table, what does return $return do? Shouldn’t it be something different?
HI ,
i have WordPress 4 multisite configure with woo-commerce plugin . I make admin side all the 4 sites order on my primary site dashboard any one have ans…..???????
i used this WP_List_Table Class Example plugin for fetching data from database. but iam unable to fetch data. My code
__( ‘Certificate’, ‘sp’ ), //singular name of the listed records
‘plural’ => __( ‘Certifications’, ‘sp’ ), //plural name of the listed records
‘ajax’ => false //does this table support ajax?
] );
}
/**
* Retrieve customers data from the database
*
* @param int $per_page
* @param int $page_number
*
* @return mixed
*/
public static function get_customers( $per_page = 5, $page_number = 1 ) {
global $wpdb;
$sql = “SELECT * FROM {$wpdb->prefix}xyz”;
if ( ! empty( $_REQUEST[‘orderby’] ) ) {
$sql .= ‘ ORDER BY ‘ . esc_sql( $_REQUEST[‘orderby’] );
$sql .= ! empty( $_REQUEST[‘order’] ) ? ‘ ‘ . esc_sql( $_REQUEST[‘order’] ) : ‘ ASC’;
}
$sql .= ” LIMIT $per_page”;
$sql .= ‘ OFFSET ‘ . ( $page_number – 1 ) * $per_page;
$result = $wpdb->get_results( $sql, ‘ARRAY_A’ );
//return $result;
}
/**
* Delete a customer record.
*
* @param int $id customer ID
*/
public static function delete_customer( $id ) {
global $wpdb;
$wpdb->delete(
“{$wpdb->prefix}xyz”,
[ ‘ID’ => $id ],
[ ‘%d’ ]
);
}
/**
* Returns the count of records in the database.
*
* @return null|string
*/
public static function record_count() {
global $wpdb;
$sql = “SELECT COUNT(*) FROM {$wpdb->prefix}xyz”;
return $wpdb->get_var( $sql );
}
/** Text displayed when no customer data is available */
public function no_items() {
_e( ‘No customers avaliable.’, ‘sp’ );
}
/**
* Render a column when no column specific method exist.
*
* @param array $item
* @param string $column_name
*
* @return mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case ‘name’:
case ‘middlename’:
return $item[ $column_name ];
default:
return print_r( $item, true ); //Show the whole array for troubleshooting purposes
}
}
/**
* Render the bulk edit checkbox
*
* @param array $item
*
* @return string
*/
function column_cb( $item ) {
return sprintf(
”, $item[‘ID’]
);
}
/**
* Method for name column
*
* @param array $item an array of DB data
*
* @return string
*/
function column_name( $item ) {
$delete_nonce = wp_create_nonce( ‘sp_delete_customer’ );
$title = ‘‘ . $item[‘name’] . ‘‘;
$actions = [
‘delete’ => sprintf( ‘Delete‘, esc_attr( $_REQUEST[‘page’] ), ‘delete’, absint( $item[‘ID’] ), $delete_nonce )
];
return $title . $this->row_actions( $actions );
}
/**
* Associative array of columns
*
* @return array
*/
function get_columns() {
$columns = [
‘cb’ => ”,
‘name’ => __( ‘Name’, ‘sp’ ),
‘middlename’ => __( ‘Middlename’, ‘sp’ ),
‘phone’ => __( ‘Phone’, ‘sp’ ),
‘lastname’ => __( ‘Lastname’, ‘sp’ )
];
return $columns;
}
/**
* Columns to make sortable.
*
* @return array
*/
public function get_sortable_columns() {
$sortable_columns = array(
‘name’ => array( ‘name’, true ),
‘lastname’ => array( ‘lastname’, false )
);
return $sortable_columns;
}
/**
* Returns an associative array containing the bulk action
*
* @return array
*/
public function get_bulk_actions() {
$actions = [
‘bulk-delete’ => ‘Delete’
];
return $actions;
}
/**
* Handles data query and filter, sorting, and pagination.
*/
public function prepare_items() {
$this->_column_headers = $this->get_column_info();
/** Process bulk action */
$this->process_bulk_action();
$per_page = $this->get_items_per_page( ‘customers_per_page’, 5 );
$current_page = $this->get_pagenum();
$total_items = self::record_count();
$this->set_pagination_args( [
‘total_items’ => $total_items, //WE have to calculate the total number of items
‘per_page’ => $per_page //WE have to determine how many items to show on a page
] );
$this->items = self::get_customers( $per_page, $current_page );
}
public function process_bulk_action() {
//Detect when a bulk action is being triggered…
if ( ‘delete’ === $this->current_action() ) {
// In our file that handles the request, verify the nonce.
$nonce = esc_attr( $_REQUEST[‘_wpnonce’] );
if ( ! wp_verify_nonce( $nonce, ‘sp_delete_customer’ ) ) {
die( ‘Go get a life script kiddies’ );
}
else {
self::delete_customer( absint( $_GET[‘customer’] ) );
wp_redirect( esc_url( add_query_arg() ) );
exit;
}
}
// If the delete bulk action is triggered
if ( ( isset( $_POST[‘action’] ) && $_POST[‘action’] == ‘bulk-delete’ )
|| ( isset( $_POST[‘action2’] ) && $_POST[‘action2’] == ‘bulk-delete’ )
) {
$delete_ids = esc_sql( $_POST[‘bulk-delete’] );
// loop over the array of record IDs and delete them
foreach ( $delete_ids as $id ) {
self::delete_customer( $id );
}
wp_redirect( esc_url( add_query_arg() ) );
exit;
}
}
}
class SP_Plugin {
// class instance
static $instance;
// customer WP_List_Table object
public $customers_obj;
// class constructor
public function __construct() {
add_filter( ‘set-screen-option’, [ __CLASS__, ‘set_screen’ ], 10, 3 );
add_action( ‘admin_menu’, [ $this, ‘plugin_menu’ ] );
}
public static function set_screen( $status, $option, $value ) {
return $value;
}
public function plugin_menu() {
$hook = add_menu_page(
‘Sitepoint WP_List_Table Example’,
‘SP WP_List_Table’,
‘manage_options’,
‘wp_list_table_class’,
[ $this, ‘plugin_settings_page’ ]
);
add_action( “load-$hook”, [ $this, ‘screen_option’ ] );
}
/**
* Plugin settings page
*/
public function plugin_settings_page() {
?>
WP_List_Table Class Example
customers_obj->prepare_items();
$this->customers_obj->display(); ?>
‘Certificate’,
‘default’ => 5,
‘option’ => ‘customers_per_page’
];
add_screen_option( $option, $args );
$this->customers_obj = new Customers_List();
}
/** Singleton instance */
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
}
add_action( ‘plugins_loaded’, function () {
SP_Plugin::get_instance();
} );
Hi,
I am currently using the url code in my functions .php but i need to be able to make the link clickable
function test_modify_user_table( $column ) {
$column[‘url’] = ‘RSA’;
return $column;
}
add_filter( ‘manage_users_columns’, ‘test_modify_user_table’ );
function test_modify_user_table_row( $val, $column_name, $user_id ) {
$user = get_userdata( $user_id );
switch ($column_name) {
case ‘url’ :
return $user->pie_upload_35;
break;
default:
}
return $return;
}
add_filter( ‘manage_users_custom_column’, ‘test_modify_user_table_row’, 10, 3 );
HI Nice post!!!
awesome filter. Do you know how to add a new table after users table to show statistics about country of my users? Keep up the good work.
Hi Friends,
I am facing two problem in WordPress admin
1. Load custom CSS for wp-admin on user-role base (editor)
2. Remove some columns as per user-role in Woo-Commerce on products page