Okay
  Public Ticket #4189557
Custom Registration Field showing on Users page
Closed

Comments

  •  2
    Lindsey started the conversation

    Hi! Is there anyway to add a customer user field to the Users page? We'd love to be able to see "Employee ID" next to their last name so we don't have to jump through hoops to find out who they are when it's been years since they've registered. Code snippets are great, or I have access to the templates.

    Attached files:  Users-‹-Ward-Gear-—-WordPress-07-24-2025_11_21_AM.png

  •  2,535
    WebWizards replied

    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

  •  2
    Lindsey replied

    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,535
    WebWizards replied

    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

  •  2
    Lindsey replied

    Thanks Stefan! I got it to work.

    Would there be a way to code it so that it's searchable within the Users page?

  •  2,535
    WebWizards replied

    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
    Lindsey replied

    Hi Stefan! Yes, thank you!

    This search code is throwing a 500 error for me.

  •  2,535
    WebWizards replied

    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

  •  2
    Lindsey replied

    You're an angel Stefan, thank you!

  •   WebWizards replied privately