0

What I am trying to do

I am trying to add a filter option by adding a dropdown menu that i have hooked onto restrict_manage_users, so i can filter users by a specific meta value.

My problem

When i choose an option from the dropdown and click submit, the data that is being sent (what i can see in the url) is only the first option even if i select another option it is still the first option that is being sent. If i then try to hardcode selected into the second option, then it changes so that it is only the second option that is being sent every time.

Code

I have removed the foreach loop that loops through the companies and echoes them to the dropdown.

add_action( 'restrict_manage_users' , 'restrict_to_company' );

function restrict_to_company()
{
  echo '<select name="company_sorting" id="company_sorting">';
  echo '<option value="0">Select a company</option>';
  echo '<option value="1">Company 1</option>';
  echo '<option value="2">Company 2</option>';
  echo '</select>';
  echo '<input type="submit" class="button" value="Sort" />';
}

Result

This is what i get in the URL after the domain name.
/wp-admin/users.php?s&action=-1&company_sorting=0&paged=1&action2=-1

0

So I found the solution to my problem.

It is because WordPress inserts the dropdown both at the top of the list of users and on the bottom. So when i submit the form, it is the dropdown at the bottom of the page that is being used in the GET and overwriting the one at the top.
To solve this issue I just removed the bottom dropdown with a line of javascript that looks like this.

document.getElementsByClassName("companyDropdown")[1].innerHTML = "";

And then wrapped the dropdown in a div with the class companyDropdown.

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.