PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Hook/Filter to add field to User List Search

s2Member Plugin. A Membership plugin for WordPress®.

Hook/Filter to add field to User List Search

Postby toddz88 » May 18th, 2011, 2:56 am

s2member's custom registration fields are nicely added as columns to the WP-Admin > Users list. And even better.. they are actually SEARCHABLE in the Search Users box (top right of the page).

But I have a custom user-profile field that is NOT part of s2m, called "twitter". I am able to display a column for it, and populate it with data correctly. But unlike the s2m fields, this one is NOT (yet) searchable.

I found some nice HOOKS in s2member/includes/classes/users-list.inc.php (lines 66-80), that seem intended to allow us to add additional fields to the User Search functionality. Especially line 74:
Code: Select all
$query->query_where .= apply_filters ("ws_plugin__s2member_before_users_list_search_where_or_after", "", get_defined_vars ()) . ")"; /* Leaving room for additional searches here. */


I took a stab at a function for this hook, in my s2-hacks.php:
Code: Select all
add_filter ("ws_plugin__s2member_before_users_list_search_where_or_after", "my_other_field");
function my_other_field(){
   $my_search = " OR (`" . $wpdb->usermeta . "`.`meta_key` = '" . $wpdb->prefix . "twitter' AND `" . $wpdb->usermeta . "`.`meta_value` LIKE '" . $s . "')";
   return $my_search;
}


The query part is based on the other queries in users-list.inc.php, just changing the field name from "s2member_custom" to my "twitter".
Code: Select all
$query->query_where .= " OR (`" . $wpdb->usermeta . "`.`meta_key` = '" . $wpdb->prefix . "s2member_custom' AND `" . $wpdb->usermeta . "`.`meta_value` LIKE '" . $s . "')";


But it's not working. Can anyone say if I'm using the Hook as intended; if my Filter makes sense; or offer any insight about why it's not working?

Thanks!
todd
User avatar
toddz88
Experienced User
Experienced User
 
Posts: 61
Joined: November 19, 2010

Re: Hook/Filter to add field to User List Search

Postby peterhuk » May 18th, 2011, 6:12 am

Hi toddz88 ,

Why not try adding a Twitter Custom Registration Fields:

s2Member General Options -> Custom Registration Fields

This will provide a Twitter input field for your users to complete
at registration and allow existing user to add / update their
Twitter info.

You'll probably save yourself a lot of time :D

Pete..
User avatar
peterhuk
Experienced User
Experienced User
 
Posts: 102
Joined: February 12, 2011

Re: Hook/Filter to add field to User List Search

Postby toddz88 » May 18th, 2011, 10:23 am

Actually "twitter" is not the name of the field we're going to use on the site, it was just the example used in a tutorial from Justin Tadlock related to custom user fields. And it will not really be a user-facing field; it's a private admin field. But i know an s2m field can be entirely hidden from the user; i'm doing that elsewhere.

The reason I am trying to NOT use an s2m Custom Registration Field is that those are stored in a an ARRAY WITHIN a custom user field, which makes the value much harder to access when using a wordpress function to Search / Query / Find users, based on that value.

Ultimately i need to find all Users where "Group" = "Acme", and update them all, setting their "EOT" to a certain value.

The function WP_User_Query works, but not for s2m custom-reg-fields:
Code: Select all
$wp_user_search = new WP_User_Query( array(
   'role' => 'subscriber'
) );
$my_group = $wp_user_search->get_results();


And wp's get_users() also works, but again not for s2m custom-reg-fields:
Code: Select all
$args = array(
   'meta_key' => 'aim',
   'meta_value' => 'Bertha',
   'meta_compare' => '='
);
$my_group = get_users( $args );

And this one also works with a meta_key of "twitter" (which would eventually be "group" for me). That's why I was trying to use a NON s2m custom-reg-field.

But the problem becomes that my custom field (whether 'twitter' or 'group') is not SEARCHABLE on the WP-Admin > Users screen, and I can't use my own hooks there, since s2m is already doing that, so i need to hook into s2m's modification of the search form on that page.

Hope this clarifies the larger situation. And thanks again for any insights.
-Todd
User avatar
toddz88
Experienced User
Experienced User
 
Posts: 61
Joined: November 19, 2010

Re: Hook/Filter to add field to User List Search

Postby peterhuk » May 18th, 2011, 12:57 pm

Hi toddz88 ,

I would probably still recomend using the

s2Member General Options -> Custom Registration Fields

then during the registration process (you will need to find the appropriate hook)
add a seperate user meta for the group you require.

This will allow you to use S2M via the user admin console to do searches and also
using your group user meta that was defined previously search for the relevent
group in a non S2m environment.

Hope this helps

Pete..
User avatar
peterhuk
Experienced User
Experienced User
 
Posts: 102
Joined: February 12, 2011

Re: Hook/Filter to add field to User List Search

Postby peterhuk » May 18th, 2011, 1:07 pm

I just noticed something. Your code above:

Code: Select all
function my_other_field(){
   $my_search = " OR (`" . $wpdb->usermeta . "`.`meta_key` = '" . $wpdb->prefix . "twitter' AND `" . $wpdb->usermeta . "`.`meta_value` LIKE '" . $s . "')";
   return $my_search;
}


includes the $wpdb class but you have not invoked the global class
i.e
Code: Select all
global $wpdb;


prior to calling the class.

hope that helps

Pete..
User avatar
peterhuk
Experienced User
Experienced User
 
Posts: 102
Joined: February 12, 2011

Re: Hook/Filter to add field to User List Search

Postby toddz88 » May 18th, 2011, 1:47 pm

Hi peterhuk,
First, thank you very much.. i really do appreciate the help and attention you're offering.

..then during the registration process..

My "group" value won't be known/determined at the time of registration; it will be set later by the site administrator, and added to the user's profile manually, via wp-admin>users.

..search for the relevent group in a non S2m environment..

s2m stores ALL of its custom-reg-fields in an array, in a single user-meta field called "myTablePrefix_s2member_custom_fields". And the standard wordpress method to SEARCH user meta-data -- get_users() or the WP_User_Query class -- don't seem to be able to reach my Group value since it's nested in that array. So by using my own custom user-meta field, I can have the Group value be a simple string that get_users() could use. Or perhaps there's a way to customize WP_User_Query for that, but it didn't seem easy/obvious to me.

..includes the $wpdb class but you have not invoked the global class..

My function is (trying to) just insert the contents of $my_search" (which yes, uses $wpdb) into s2m's Search Form modification code (line 74 of s2member/includes/classes/users-list.inc.php), using a Hook explicity for the purpose of adding more custom fields to the search query. So $wpdb was already invoked by the code i hooked into. Unless .. as I said in my original post .. i'm just not doing it correctly.

So really, I'm looking for EITHER:

A) help with the Hook/Filter "ws_plugin__s2member_before_users_list_search_where_or_after" (line 74 of s2member/includes/classes/users-list.inc.php). ..(or)..

B) help getting get_users() or WP_User_Query to search for a string (like "Acme") in the Value of an array element who's Key is "Group", all inside an array which is in a user-meta field called "wp_kjk_s2member_custom_fields". (I probably made that sound more complicated than it hopefully is).

Hmm, maybe i'll post a new topic/question for (B), since that's a totally different approach.
User avatar
toddz88
Experienced User
Experienced User
 
Posts: 61
Joined: November 19, 2010

Re: Hook/Filter to add field to User List Search

Postby peterhuk » May 18th, 2011, 2:26 pm

Hi toddz88,

re:

So $wpdb was already invoked by the code i hooked into. Unless .. as I said in my original post .. i'm just not doing it correctly.


Your function does not have an input field and therefore $wpdb will be returned as undefined
(check your error logs).

Second WP stores meta data in serialized form so therefor the only otherway I can suggest
is to do a full text seach of the meta_value colum for something like
Code: Select all
s:XX:"yourgroup";s:YY:"name"


Where XX being the number of chars in yourgroup (9)
and YY being the number of chars in name (5)

i.e
Code: Select all
SELECT * FROM wp_usermeta WHERE meta_key = 'wp__s2member_custom_fields'
AND meta_value LIKE '%s:5:"group";s:7:"Twitter"%'

you'll need to do an inner join to include the wp_users table
Hope this helps

Pete..
Last edited by peterhuk on May 18th, 2011, 2:43 pm, edited 1 time in total.
User avatar
peterhuk
Experienced User
Experienced User
 
Posts: 102
Joined: February 12, 2011

Re: Hook/Filter to add field to User List Search

Postby peterhuk » May 18th, 2011, 2:32 pm

Forgot to mention,

you can easily create your own search function and if you know all the groups you are likely to create the
you can add all the combinations into an array (inc serialised info) i.e

$groups = array(
'twitter' => '%s:5:"group";s:7:"Twitter"%'
'facebook' => '%s:5:"group";s:8:"facebook"%'
)

then the searches can be referenced via the array

Pete..
User avatar
peterhuk
Experienced User
Experienced User
 
Posts: 102
Joined: February 12, 2011

Re: Hook/Filter to add field to User List Search

Postby toddz88 » May 18th, 2011, 4:11 pm

THANK YOU!!!
I didn't realize that s2m stores its CustRegFields as a serialized STRING.. which IS searchable with a " LIKE '%%var%%' " query. I thought it was actually an array in there, since i was looking at my print_r output and seeing it as a structured array. But yes, now i also see instances in the print_r of the serialized string with the same data. So here's what I ended up doing, which works perfectly for me:

Code: Select all
# PREP VARS
// vars for the Query
$group_name = ""; //get from the GroupManager's data, from s2m api Notification.
$group_name_chars = strlen($group_name);
// vars to update in each user (member) of the Group // also get from GroupMaster's data
$group_eot = '';
$group_cancel_date = '';

# FIND USERS IN THE GROUP
$tz_query = sprintf("SELECT * FROM $wpdb->usermeta WHERE meta_value LIKE '%%;s:10:\"group_acct\";s:%d:\"%s\";%%' ",$group_name_chars,$group_name);
$tz_results = $wpdb->get_results($tz_query,OBJECT); // result is an array of objects.

# UPDATE USERS IN THE GROUP
foreach ($tz_results as $group_member){
   //echo $group_member->user_id; //test
   
   # get
   $user_id = $group_member->user_id;
   $option_name = 's2member_custom_fields'; //set var
   $s2m_custom_fields = get_user_option($option_name, $user_id); //get array
   
   # set
   $s2m_custom_fields['eot'] = $group_eot;
   $s2m_custom_fields['cancel_date'] = $group_cancel_date;
   $newvalue = $s2m_custom_fields;
   $s2m_custom_fields_update = update_user_option( $user_id, $option_name, $newvalue );
   
}


Thanks again peterhuk.. you lead me in exactly the right direction.
User avatar
toddz88
Experienced User
Experienced User
 
Posts: 61
Joined: November 19, 2010

Re: Hook/Filter to add field to User List Search

Postby peterhuk » May 18th, 2011, 5:07 pm

Just glad that you were able to get a resolve, by the way it is actually WP that serialises / unserialises
arrays before saving / retrieving from the DB (found that out by accident).

also if you don't add

Code: Select all
AND meta_key = 'wp__s2member_custom_fields'


to limit the scope of the db search, it will end searching every record in the user meta table
which will slow down your search.

Pete..
User avatar
peterhuk
Experienced User
Experienced User
 
Posts: 102
Joined: February 12, 2011

Re: Hook/Filter to add field to User List Search

Postby toddz88 » May 19th, 2011, 12:41 am

Oh you're right! Yes great point, thanks again Pete. You've been super helpful.
User avatar
toddz88
Experienced User
Experienced User
 
Posts: 61
Joined: November 19, 2010


Return to s2Member Plugin

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron