Opened 4 years ago
Closed 3 years ago
#10781 closed enhancement (fixed)
Make WP_User_Search plugin friendly
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 3.0 |
| Component: | Administration | Version: | 2.9 |
| Severity: | normal | Keywords: | has-patch commit early |
| Cc: | westi, batmoo@… |
Description
Currently, there is no way to modify the query done when displaying wp-admin/users.php
This can be easily changed by allowing plugins to modify query components in the WP_User_Search class.
Attachments (5)
Change History (22)
- Keywords early added
- Milestone changed from 2.9 to 3.0
Let's do this early 3.0. Locking 2.9 down.
I'll supply a diff shortly but if this is going into 3.0 - let's stretch it just a little bit to allow search on any meta key - assuming capabilities by default by adding a optional fourth parameter.
e.g. function WP_User_Search ($search_term = , $page = , $meta_value = , $meta_key=)
Generalizes WP_User_Search for any meta key/value pair while defaulting to capabilities
- Cc westi added
- Keywords commit removed
Replying to transom:
I'll supply a diff shortly but if this is going into 3.0 - let's stretch it just a little bit to allow search on any meta key - assuming capabilities by default by adding a optional fourth parameter.
e.g. function WP_User_Search ($search_term = , $page = , $meta_value = , $meta_key=)
Patch looks good.
Could you put together a simple example plugin which shows how this is used and attach it to the ticket.
This will make the changes easy to test and mean that we can get them committed.
To have WP_User_Search become more plugin friendly would be if we can make it extendable and provide a pointer (via filter?) to the actual implementation. Then plugin authors only need to little extend the class to their needs. Or is this already possible?
I think that's a little too much. Not even WP_Query has that option, yet it's still pretty extensible.
comment:10
scribu — 3 years ago
Added an example plugin that sorts the users by number of posts (only when viewing a single role).
Requires user.php.2.diff
comment:11
batmoo — 3 years ago
- Cc batmoo@… added
I was going to create a new ticket for this, but figured that since the class is being worked on, it makes sense just to bring this up here. One issue to watch for with the role search (which is tricky because we're doing a LIKE search through a serialized array). If I run the following code:
add_role('edit', 'edit');
$u = new WP_User_Search('', '', 'edit');
print_r($u->get_results());
...it will return all users with the "Editor" role (and any user with an edit_* cap). It's sort of an edge case, but likely should still be patched (until the role system is revamped, at least). Best option I see (for now) is to wrap the role term in double quotes (since that's how strings vars in arrays seem to be serialized).
comment:12
batmoo — 3 years ago
I actually ended up creating a User Query function (in the same vein as WP_Query) for one of my plugins, that let's you query for users based a number of criteria. Basically you can look up users based on a search_term (as with WP_User_Search), specific user fields, role, usermeta, and so on.
It's completely barebones right now (i.e. not really finished/polished), but if there's interest, I can look into fleshing it out and submitting a patch.
See it here: http://plugins.trac.wordpress.org/browser/edit-flow/trunk/php/util.php
comment:13
scribu — 3 years ago
Thanks for the sample, batmoo. It wouldn't hurt to have a more powerful user search class.
But the main point of this ticket is to allow plugins a way in. After that, it can be extended indefinetly.
comment:14
westi — 3 years ago
- Owner set to westi
- Status changed from new to reviewing
Ok this looks good.
Could you update the patch so it applies to current trunk please and then I will finish the review and commit - I think some of the merge changes have made it no longer apply.
comment:15
scribu — 3 years ago
- Keywords commit added
Refreshed patch: user.php.4.diff, compatible with MS.
comment:16
westi — 3 years ago
Works well for me.
The role check is the example code causes it not to run when no role is specified which confused my testing for a little while.
comment:17
westi — 3 years ago
- Resolution set to fixed
- Status changed from reviewing to closed

Proposed patch separates query_from_where into 2, so that plugins can more easily add or remove specific bits.
Also, it adds a do_action_ref_array() on $this.