I am running a membership site and wanting to add an new column at my order listing table (WordPress Dashboard). I shall show the member’s age at that column. Here is my steps for this functionality:
Creating Custom Field
I’m saving the Date of Birth of my customers as a custom field data. I’m using WordPress’s native custom field option. You can also use the free third party plugin like ACF/Pods/CMB2 etc.
Now I shall add some PHP snippets into my functions.php file of my active theme.
Adding New Column into the Order List Table
The first step is to hook into load-edit.php, which will fire whenever shop order list table is loading. I’ll use get_current_screen to grab the screen object and make sure I’m on the order list table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
add_action('load-edit.php', 'wco_load', 20); function wco_load() { $screen = get_current_screen(); if (!isset($screen->post_type) || 'shop_order' != $screen->post_type) { return; } //* Adding the custom column into the existing List Table Column add_filter("manage_{$screen->id}_columns", 'wco_add_columns'); } function wco_add_columns( $columns ) { $actions = $columns['order_actions']; $order_total = $columns['order_total']; $order_date = $columns['order_date']; unset($columns['order_date']); unset($columns['order_total']); unset($columns['order_actions']); $columns["dob_field"] = __( "Age", "themeprefix"); $columns['order_date'] = $order_date; $columns['order_total'] = $order_total; $columns['order_actions'] = $actions; return $columns; } |
Displaying Content in the Custom Column
To display content, we hook into manage_{$screen->post_type}_posts_custom_column. I’ll modify my load function a bit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
add_action('load-edit.php', 'wco_load', 20); function wco_load() { $screen = get_current_screen(); if (!isset($screen->post_type) || 'shop_order' != $screen->post_type) { return; } //* Adding the custom column into the existing List Table Column add_filter("manage_{$screen->id}_columns", 'wco_add_columns'); //* Displaying Content in the Custom Column add_action( "manage_{$screen->post_type}_posts_custom_column", 'wco_column_cb_data', 10, 2 ); } function wco_column_cb_data( $colname, $cptid ) { if ( $colname == 'dob_field') { $dob = get_post_meta( $cptid, 'dob_field', true ); if( ! empty( $dob ) ) { $age = date('Y') - date( 'Y', strtotime($dob) ); echo $age; } } } |
Here is the complete code. You will put this code into your functions.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php //* Do not add this line /** * Adding the custom column into WooCommerce shop order list table * * @author Paul * @version 1.0 * @license GPL2.0+ */ add_action('load-edit.php', 'wco_load', 20); function wco_load() { $screen = get_current_screen(); if (!isset($screen->post_type) || 'shop_order' != $screen->post_type) { return; } //* Adding the custom column into the existing List Table Column add_filter("manage_{$screen->id}_columns", 'wco_add_columns'); //* Displaying Content in the Custom Column add_action( "manage_{$screen->post_type}_posts_custom_column", 'wco_column_cb_data', 10, 2 ); } function wco_add_columns( $columns ) { $actions = $columns['order_actions']; $order_total = $columns['order_total']; $order_date = $columns['order_date']; unset($columns['order_date']); unset($columns['order_total']); unset($columns['order_actions']); $columns["dob_field"] = __( "Age", "themeprefix"); $columns['order_date'] = $order_date; $columns['order_total'] = $order_total; $columns['order_actions'] = $actions; return $columns; } function wco_column_cb_data( $colname, $cptid ) { if ( $colname == 'dob_field') { $dob = get_post_meta( $cptid, 'dob_field', true ); if( ! empty( $dob ) ) { $age = date('Y') - date( 'Y', strtotime($dob) ); echo $age; } } } |
Feel free comments at below. If you need any advanced help, please fill up the form at below.
Matthew J Darnell says
Hello … I’m trying to use this code to add a column to the “Orders” list in the dashboard of WooCommerce. Instead of “age”, as you’ve done, though, I’m trying to get “Company Name”. Here’s the order page on the site:
https://www.wvenergyexpo.com/product/exhibit-at-the-west-virginia-energy-expo/
The “Company Name” field is the one I want to show up on the “Orders” page in WordPress. But I don’t know PHP very well, and I can’t figure out how to edit your code to make it show the Company Name.
It would be awesome if you could help me figure that out. The plug-in that I’m using to collect the Company Name is WooCommerce Product Add-ons, if that helps. Thank you so much!
Paul says
Can you share the wp-admin access via contact page? I shall check it for you.