Public Ticket #4189557

Custom Registration Field showing on Users page

Closed
Comments
2,762
KingsPlugins replied 1 year ago on July 28, 2025 at 10:14pm

Hi Lindsey,

I understand that you have a B2BKing Registration field and you wish to show that on the main Users page.

You can use the following code snippet for that:

// Add custom column header to Users page
function add_b2bking_custom_field_column($columns) {
    $columns['b2bking_custom_field_1234'] = 'Employee ID'; 
    return $columns;
}
add_filter('manage_users_columns', 'add_b2bking_custom_field_column');

// Display the custom field value in the column
function show_b2bking_custom_field_column_content($value, $column_name, $user_id) {
    if ($column_name == 'b2bking_custom_field_1234') {
        $custom_field_value = get_user_meta($user_id, 'b2bking_custom_field_1234', true);
        return !empty($custom_field_value) ? esc_html($custom_field_value) : '—';
    }
    return $value;
}
add_filter('manage_users_custom_column', 'show_b2bking_custom_field_column_content', 10, 3)

Before using this code, you'll need to replace '1234' with your actual field ID (it appears 3 times in the code). To find your field ID:
1. Go to B2BKing -> Registration Fields
2. Edit the Employee ID field
3. Look at the URL - you'll see something like "?post=123" - that number is your field ID

 

You can add this code to your theme's functions.php file or using a code snippets plugin.

Let me know if you need any clarification!

Kind regards,
Stefan

4 Tickets 2
Lindsey replied 1 year ago on July 29, 2025 at 2:04pm

Hi Stefan, this is great!

I'm getting an error from my code snippets plugin: syntax error, unexpected end of file

I have the registration field ID: 1779.

2,762
KingsPlugins replied 1 year ago on July 29, 2025 at 4:31pm

Hi Lindsey,

In that case the code should be:

// Add custom column header to Users page
function add_b2bking_custom_field_column($columns) {
    $columns['b2bking_custom_field_1779'] = 'Employee ID'; 
    return $columns;
}
add_filter('manage_users_columns', 'add_b2bking_custom_field_column');

// Display the custom field value in the column
function show_b2bking_custom_field_column_content($value, $column_name, $user_id) {
    if ($column_name == 'b2bking_custom_field_1779') {
        $custom_field_value = get_user_meta($user_id, 'b2bking_custom_field_1779', true);
        return !empty($custom_field_value) ? esc_html($custom_field_value) : '—';
    }
    return $value;
}
add_filter('manage_users_custom_column', 'show_b2bking_custom_field_column_content', 10, 3)

I did test that successfully on my local test site before sharing the snippet, so I believe it should work. Make sure it is saved as a PHP snippet and that there's no other <?php ?> tag in there somewhere.

 

I can also check this on your site directly if that would help. I would need a backend login to the site,

 

Kind regards,

Stefan

1
2,762
KingsPlugins replied 1 year ago on September 18, 2025 at 12:18pm

Hi Lindsey,

Great to hear that worked,

Regarding making that searchable, I tried writing a code snippet to achieve it. Please give this code a try:

// Make custom field searchable in Users page
function make_b2bking_custom_field_searchable($query) {
    global $pagenow;
    
    if ($pagenow === 'users.php' && !empty($_GET['s'])) {
        $search_term = sanitize_text_field($_GET['s']);
        
        // Get users with matching custom field values
        $user_query = new WP_User_Query(array(
            'meta_query' => array(
                array(
                    'key' => 'b2bking_custom_field_1779',
                    'value' => $search_term,
                    'compare' => 'LIKE'
                )
            ),
            'fields' => 'ID'
        ));
        
        $user_ids = $user_query->get_results();
        
        if (!empty($user_ids)) {
            $query->query_vars['include'] = array_merge(
                isset($query->query_vars['include']) ? $query->query_vars['include'] : array(),
                $user_ids
            );
        }
    }
}
add_action('pre_user_query', 'make_b2bking_custom_field_searchable');

This should allow you to search for users by typing their Employee ID directly in the standard search box on the Users page.

Let me know if that works for you,
Kind regards,
Stefan

2,762
KingsPlugins replied 1 year ago on September 19, 2025 at 7:48pm

Hi Lindsey,

I looked further into that and I think I found where that was likely glitching. Please try using this improve code snippet instead: https://pastecode.io/s/4r1k2bj3

If you continue to get a 500 error with this as well, it would be great if I could troubleshoot directly as the problem may be more complex. For that, I would need a backend login to the site or a staging site,

Kind regards,
Stefan

Sign In