• CoBu1

    (@cobu1)


    I’m trying to restrict the users in the post editor’s “authors” dropdown to users with the custom role of “welder”.

    But the wp_dropdown_users() function seems to break my site (the post editor never loads). When I remove the function, everything goes back to behaving normally.

    function ls_authors_dropdown($output) {
    
    	$welders = get_users( array(
    		'role' => 'welder',
    		'fields' => 'ID'
    	));
    
    	$args = array(
    		'include' => $welders,
    	);
    
    	$output = wp_dropdown_users($args);
    
    	return $output;
    
    }
    
    add_filter( 'wp_dropdown_users', 'ls_authors_dropdown');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Oddly, the ‘include’ argument for wp_dropdown_users() requires a comma delimited string of IDs, you are supplying an array with $welders. If you use implode() on the array and pass that result it should work.

    IMO the function is need of updating, arrays should always work in this situation. sigh

    Thread Starter CoBu1

    (@cobu1)

    Thank you so much for your help, bcworkz.

    Unfortunately that didn’t work. Even if I remove all my arguments from the wp_dropdown_users function, it still results in indefinite page loading. My code now:

    function ls_authors_dropdown($output) {
    
    	$welders = get_users( array(
    		'role' => 'welder',
    		'fields' => 'ID'
    	));
    
    	$welders_comma_separated = implode(",", $welders);
    
    	$args = array(
    		'include' => $welders_comma_separated,
    	);
    
    	$output = wp_dropdown_users($args);
    
    	return $output;
    
    }
    
    add_filter( 'wp_dropdown_users', 'ls_authors_dropdown');
    Moderator bcworkz

    (@bcworkz)

    Oh! I see now, sorry, not paying enough attention. You cannot hook into a filter and then call the function that uses that filter without special handling. You introduce an infinite loop condition.

    You may either filter the output by using string substitution, or call the function with custom arguments without using the filter, but not both!

    And that special handling? Don’t go there, you don’t need it ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘The function wp_dropdown_users() seems to break my site.’ is closed to new replies.