• Here is some code that adds a sortable referral source column to the user listing. I think this is pretty handy and thought it might be helpful to other folks or even considered for inclusion in a future version of the plugin.

    /**
     * Add referral source column to user listing in admin.
     * @link https://www.webtechideas.in/how-to-add-sortable-custom-column-on-users-listing-screen-in-wordpress/
     *
     * The referral source data is collected using the WooCommerce Hear About Us Plugin
     * @link https://www.ads-software.com/plugins/woocommerce-hear-about-us/
     */
    function hab_manage_users_referral_source_column( $columns ) {
    	$columns['source'] = 'Source';
    	return $columns;
    }
    add_filter( 'manage_users_columns', 'hab_manage_users_referral_source_column' );
    add_filter( 'manage_users_sortable_columns', 'hab_manage_users_referral_source_column' );
    
    /**
     * Get the value for the referral source user meta column.
     *
     */
    function hab_manage_users_custom_column_referral_source_value( $value, $column_name, $user_id ) {
    	if ( 'source' === $column_name ) {
    		return get_user_meta( $user_id, '_wchau_source', true );
    	}
    
    	return $value;
    }
    add_filter( 'manage_users_custom_column', 'hab_manage_users_custom_column_referral_source_value', 10, 3 );
    
    /**
     * Make the referral source column sortable.
     *
     * @link https://wordpress.stackexchange.com/questions/27518/sortable-custom-columns-in-user-panel-users-php
     */
    function hab_manage_users_custom_column_referral_source_sortable( $user_search ) {
    	global $wpdb, $current_screen;
    
    	if ( 'users' != $current_screen->id ) {
    		return;
    	}
    
    	$vars = $user_search->query_vars;
    
    	if ( 'Source' == $vars['orderby'] ) {
    		$user_search->query_from .= " INNER JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID=m1.user_id AND (m1.meta_key='_wchau_source')";
    		$user_search->query_orderby = ' ORDER BY UPPER(m1.meta_value) '. $vars['order'];
    	}
    }
    add_action( 'pre_user_query', 'hab_manage_users_custom_column_referral_source_sortable' );
    /**
     * Below are some additional snippets that will modify the user listing in the admin area so that
     * the most recent users are at the top of the list.
     *
     * Also, a sortable ID column is added. This code combined with the new column for the referral source above,
     * allows me to see at a glance where my customers are coming from.
     */
    
    /**
     * Sort user list in admin by by date created in descending order (most recent first).
     *
     */
    function hab_order_users_by_date_registered( $query ) {
    	global $pagenow;
    
    	// Bail if user has specified sort order.
    	$orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : false;
    	if ( ! $orderby ) {
    		$query->query_orderby = 'ORDER BY user_registered DESC';
    	}
    
    	if ( ! is_admin() || 'users.php' !== $pagenow ) {
    		return;
    	}
    
    	return;
    }
    add_action( 'pre_user_query', 'hab_order_users_by_date_registered' );
    
    /**
     * Adds the User ID column to the first position after the checkbox in the admin user listing.
     *
     * @link https://www.webtechideas.in/how-to-add-sortable-custom-column-on-users-listing-screen-in-wordpress/
     */
    function hab_add_user_custom_column_id( $columns ) {
    	$new_columns = array();
    	$columns_1   = array_slice( $columns, 0, 1 );
    	$columns_2   = array_slice( $columns, 1 );
    	$new_columns = $columns_1 + array( 'user_id' => 'ID' ) + $columns_2;
    
    	return $new_columns;
    }
    add_filter( 'manage_users_columns', 'hab_add_user_custom_column_id' );
    add_filter( 'manage_users_sortable_columns', 'hab_add_user_custom_column_id' );
    
    /**
     * Adds the value for the User ID column in the admin user listing.
     *
     * @link https://www.webtechideas.in/how-to-add-sortable-custom-column-on-users-listing-screen-in-wordpress/
     */
    function hab_add_user_custom_column_id_value( $value, $column_name, $user_id ) {
    	if ( 'user_id' == $column_name ) {
    		return $user_id;
    	}
    
    	return $value;
    }
    add_action( 'manage_users_custom_column', 'hab_add_user_custom_column_id_value', 10, 3);
    
    /**
     * Add some CSS to admin_head to keep the ID field size reasonably small.
     *
     */
    function hab_user_table_user_id_column_admin_css() { ?>
    	<style>
    	.users-php .users.wp-list-table .column-user_id {
    		width: 64px;
     }
     </style>
    <?php
    }
    add_action( 'admin_head', 'hab_user_table_user_id_column_admin_css' );
Viewing 5 replies - 1 through 5 (of 5 total)
  • This is what I need but I’m not sure where I need to put the code? New to WordPress ??

    Thread Starter Dave Romsey (goto10)

    (@goto10)

    The best place to put this code is in a custom plugin. They are super easy to create. Check it out!

    Alternatively, you can add that code to your theme’s functions.php file. Good luck!

    Plugin Author PieterC

    (@siteoptimo)

    Hi,

    Just to let you know we’re considering to add this feature to a next release. Will be probably within a few weeks.

    Regards,

    Pieter

    Plugin Author PieterC

    (@siteoptimo)

    Hello,

    Just to let you know: we’ve just released version 1.4.2 which includes the extra column and sorting capabilities in the user as well as the order listing.

    Hope you like it.

    Regards,

    Koen
    SiteOptimo

    Thread Starter Dave Romsey (goto10)

    (@goto10)

    Thanks for the update, Koen. You guys rock!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add sortable Referral Source column to user the listing’ is closed to new replies.