Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.8 kB
4
Indexable
Never

add_filter('wp_dropdown_users', function($output){
    return modify_user_dropdown($output);
}, 10, 1);
function modify_user_dropdown($html_output) {
    // Use DOMDocument to parse the HTML
    $dom = new DOMDocument();
    libxml_use_internal_errors(true); // Suppress warnings/errors due to HTML5 or malformed HTML
    $dom->loadHTML($html_output, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); // Load HTML without adding doctype, html, and body tags
    libxml_clear_errors();
    
    // Find the select element
    $select = $dom->getElementById('b2bking_all_users_dropdown');
    if (!$select) {
        return $html_output; // If select element not found, return original HTML
    }
    
    // Process each option element in the select
    foreach ($select->getElementsByTagName('option') as $option) {
        $userId = $option->getAttribute('value');
        if (!empty($userId)) {
            $userData = get_userdata($userId);
            if ($userData) {
                // Get username and usermeta for billing_company
                $username = $userData->user_login; // user_login is the username
                $company = get_user_meta($userId, 'billing_company', true);
                
                if (!empty($company)) {
                    // Update the option text to show user ID and company, but set the value to username
                    $option->nodeValue = $userId . ' - ' . $company;
                    $option->setAttribute('value', $username);
                } else {
                    // If no company found, display user ID and indicate no company
                    $option->nodeValue = $userId . ' - No Company';
                    $option->setAttribute('value', $username);
                }
            }
        }
    }
    
    // Save the updated HTML and return it
    return $dom->saveHTML();
}


add_action('admin_head', function(){
    ?>
    <script>
        jQuery(document).ready(function(){
            setTimeout(function(){
                jQuery('#b2bking_category_add_user').off('click');

                jQuery("#b2bking_category_add_user").on("click",function(){
                    // Get username
                    let username = jQuery("#b2bking_all_users_dropdown").children("option:selected").val();
                    // Get content and check if username already exists
                    let content = jQuery("#b2bking_category_users_textarea").val();
                    let usersarray = content.split(',');
                    let exists = 0;

                    jQuery.each( usersarray, function( i, val ) {
                        if (val.trim() === username){
                            exists = 1;
                        }
                    });

                    if (exists === 1){
                        // Show "Username already in the list" for 3 seconds
                        jQuery("#b2bking_category_add_user").text(b2bking.username_already_list);
                        setTimeout(function(){
                            jQuery("#b2bking_category_add_user").text(b2bking.add_user);
                        }, 2000);

                    } else {
                        // remove last comma and whitespace after
                        content = content.replace(/,\s*$/, "");
                        // if list is not empty, add comma
                        if (content.length > 0){
                            content = content + ', ';
                        }
                        // add username
                        content = content + username;
                        jQuery("#b2bking_category_users_textarea").val(content);
                    }
                });
            }, 300);
        });

        
    </script>
    <?php
});

Leave a Comment