0

I want to reorder the Company column to be the first column in the table, the code is working fine but it's position is the last one. How can I get it to be in the first before the username?

function add_company_column($defaults) {
    $defaults['company'] = __('Company');
    return $defaults;
}

function view_company_column($value, $column_name, $id) {
    if ($column_name == 'company') {
        global $wpdb;
        $companyID = get_usermeta($id, 'company');
        $company = $wpdb->get_row("SELECT com_name FROM " . $wpdb->prefix . "companies WHERE com_id = " . $companyID);
        return $company->com_name;
    }
}

add_filter('manage_users_columns', 'add_company_column', 15, 1);
add_action('manage_users_custom_column', 'view_company_column', 15, 3);

Thanks

0

Ok, I solved it by unsetting $defaults and reconstructing it after adding the company

function add_company_column($defaults) {
    unset($defaults);
    $defaults['company'] = __('Company');
    $defaults['username'] = __('username');
    $defaults['name'] = __('Name');
    $defaults['email'] = __('Email');
    return $defaults;
}
0

There is no need to unset all the columns and build the columns all-together. What you can do is split the default columns array after the 1st column, then merge the new custom column array with the other 2 arrays. Have a look at the following code.

function add_company_column( $defaults ) {
    $new_columns = array();

    $columns_1 = array_slice( $defaults, 0, 1 );
    $columns_2 = array_slice( $defaults, 1 );

    $new_columns = $columns_1 + array( 'company' => __('Company') ) + $columns_2;
    return $new_columns;
}

With the above code, the company column will be shown before username column.

You can have a look at a complete article on this including sortable custom column on Webtechideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.