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™

Capture custom registration field during registration proces

s2Member Plugin. A Membership plugin for WordPress®.

Capture custom registration field during registration proces

Postby nashvillegeek » July 6th, 2011, 11:51 am

Hello,

I'm trying to figure out how to capture a custom registration checkbox or radio button entry from the registration form. The functionality would basically give the member the option to appear in the member list generated by the Members List plugin. To do this, I would need to capture the selection and add it to the users WP usermeta.

Thinking this through, I would also need it to update when that field is updated in the edit profile page.

I've created my s2-hacks.php file and have searched through the classes folder for hooks, etc, but am still struggling a bit.

Any direction would be appreciated.

Thanks
User avatar
nashvillegeek
Registered User
Registered User
 
Posts: 36
Joined: June 3, 2011
Location: Nashville TN

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 7th, 2011, 2:15 am

The Member List plugin has a feature to exclude users from the lists, from what I can see in the settings. WP Admin -> Member List -> Edit Members

Now, you could add the s2Member custom registration field to give users the choice whether they'll show up in the list or not, then udate the Member List settings above based on that.

Let me see if I can find a hook that could help...

You could try ws_plugin__s2member_after_handle_profile_modifications:

viewtopic.php?f=40&t=9359&p=19397&hilit=ws_plugin__s2member_after_handle_profile_modifications
viewtopic.php?f=40&t=9605&p=19643&hilit=ws_plugin__s2member_after_handle_profile_modifications#

Some comments from Jason on this topic viewtopic.php?f=4&p=2252#p2252

This may be useful too http://codex.wordpress.org/Plugin_API/A ... ile_update

I hope that helps!
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby nashvillegeek » July 7th, 2011, 1:50 pm

Thanks Cristian! I've got the hook in place in my hack file and it echoes out my alert when a profile is updated. Can you tell me how to get the s2member custom fields into the function. I've been trying with no success. I am able to get $current_user, etc, but can't figure out to get the custom fields in.

Thanks.
User avatar
nashvillegeek
Registered User
Registered User
 
Posts: 36
Joined: June 3, 2011
Location: Nashville TN

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 7th, 2011, 2:11 pm

I'm glad that helped! Does this help with the custom fields?

Code: Select all
$s2_custom_fields = get_user_option('wp_s2member_custom_fields', $user_id);  


viewtopic.php?f=4&t=6546&p=16582#p16582
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby nashvillegeek » July 7th, 2011, 3:27 pm

Thanks so much, Cristian! Below is the code that got the functionality I need. I'm sure it could be cleaner. I'm just glad it works. I separated out the functions so that I could use the 'catch_profile_edit' on some other things, as well.


Code: Select all


function get_s2member_custom_fields($user_id '') {            
            
$values get_user_option('s2member_custom_fields'$user_id);
            foreach ((array)
json_decode($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_fields'], true) as $field)
                
$custom_fields[$field['id']] = array(
                    
'label' => $field['label'],
                    
'value' => isset($values[$field['id']]) ? $values[$field['id']] : '',
                );
            return 
$custom_fields;
        }

function 
update_member_directory_preference($curUser,$custom_fields){
               
//adjust user meta to adapt to preference
             
if(($custom_fields['member_directory_include']['value'])=="1")  {
                
add_user_meta($curUser,'_tern_wp_member_list','My List');
             }
             if((
$custom_fields['member_directory_include']['value'])!=1){
                
delete_user_meta($curUser,'_tern_wp_member_list');     
            }
}
        
function 
catch_profile_edit()
           {
            global 
$getWP,$tern_wp_members_defaults,$current_user,$wpdb,$profileuser;
            
$custom_fields get_s2member_custom_fields();
            
$curUser $current_user->ID;
            
update_member_directory_preference($curUser,$custom_fields);
               
 } 
           
 
add_action("ws_plugin__s2member_after_handle_profile_modifications""catch_profile_edit");
 


Thanks again.
Last edited by Cristián Lávaque on July 7th, 2011, 5:21 pm, edited 1 time in total.
Reason: Improve code readability. http://www.primothemes.com/forums/viewtopic.php?f=36&t=2780
User avatar
nashvillegeek
Registered User
Registered User
 
Posts: 36
Joined: June 3, 2011
Location: Nashville TN

Re: Capture custom registration field during registration pr

Postby nashvillegeek » July 7th, 2011, 4:33 pm

The only problem I see now is that the 'update_member_directory_preference' script isn't updated after registration. Is there a hook that can handle that?
User avatar
nashvillegeek
Registered User
Registered User
 
Posts: 36
Joined: June 3, 2011
Location: Nashville TN

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 7th, 2011, 5:53 pm

Nice! I'm glad it helped you. :)

You could probably simplify that, though, since you don't need all the custom fields labels. Here's a suggestion:

Code: Select all
add_action('ws_plugin__s2member_after_handle_profile_modifications', 'catch_profile_edit');
function catch_profile_edit() {
    global $current_user;
    $s2_custom_fields = get_user_field('s2member_custom_fields', $current_user->ID);
    if ($s2_custom_fields['member_directory_include'] === '1')
        add_user_meta($current_user->ID, '_tern_wp_member_list', 'My List');
    else
        delete_user_meta
($current_user->ID, '_tern_wp_member_list');
 }
 


Let me know if it works.

nashvillegeek wrote:The only problem I see now is that the 'update_member_directory_preference' script isn't updated after registration. Is there a hook that can handle that?


Maybe this one? https://codex.wordpress.org/Plugin_API/ ... r_register
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby nashvillegeek » July 8th, 2011, 11:37 am

That certainly cleans the code up a bit. I did try the user_register hook. I couldn't get it to work. I will probably try to figure out how to hide that field on the registration form.

You've been a great help, Cristian. Thanks!
User avatar
nashvillegeek
Registered User
Registered User
 
Posts: 36
Joined: June 3, 2011
Location: Nashville TN

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 8th, 2011, 8:25 pm

You're welcome! I'm glad I could help a bit. :)

I'll ask Jason what hook he'd recommend after the registration.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby Jason Caldwell » July 8th, 2011, 9:03 pm

Thanks for pointing out this thread.

The only problem I see now is that the 'update_member_directory_preference' script isn't updated after registration. Is there a hook that can handle that?
Sorry, can you please confirm when you're trying to update this data? During/after Registration, or during/after a Profile edit?
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 8th, 2011, 10:39 pm

Both. He wants to update the Member List setting to exclude users from the lists when the s2Member custom registration field he created for this is changed. So it goes after creating the account and modifying it. We already got the modification part working above, he's just missing the new account part.

Thanks for taking a look. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby Jason Caldwell » July 8th, 2011, 11:07 pm

Ah. Thanks for the help.

Something like this might help.
Create directory and file:
/wp-content/mu-plugins/s2-hacks.php
Code: Select all
<?php
add_action 
("ws_plugin__s2member_during_configure_user_registration_front_side", "my_handler");
add_action ("ws_plugin__s2member_during_configure_user_registration_admin_side", "my_handler");
function my_handler ($vars = array ())
    {
        $user_id = $vars["user_id"];
        // Do something here.
    }
?>
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 8th, 2011, 11:11 pm

Thanks! is $vars['user_id'] also available when using the hook 'ws_plugin__s2member_after_handle_profile_modifications'?
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby Jason Caldwell » July 8th, 2011, 11:13 pm

Cristián Lávaque wrote:Thanks! is $vars['user_id'] also available when using the hook 'ws_plugin__s2member_after_handle_profile_modifications'?
Yes, it is also available for the Profile modification Hook.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 8th, 2011, 11:24 pm

Awesome. Thanks! :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 8th, 2011, 11:30 pm

Jason, does 'ws_plugin__s2member_after_handle_profile_modifications' work for both, front and admin modifications?

Geek, here's the updated hack.

Code: Select all
<?php
add_action
('ws_plugin__s2member_during_configure_user_registration_front_side', 'member_directory_include');
add_action('ws_plugin__s2member_during_configure_user_registration_admin_side', 'member_directory_include');
add_action('ws_plugin__s2member_after_handle_profile_modifications', 'member_directory_include');

function member_directory_include($vars = array ()) {
    $s2_custom_fields = get_user_field('s2member_custom_fields', $vars['user_id']);

    if ($s2_custom_fields['member_directory_include'] === '1')
        add_user_meta($vars['user_id'], '_tern_wp_member_list', 'My List');
    else
        delete_user_meta
($vars['user_id'], '_tern_wp_member_list');
 }
?>


I hope it works. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby Jason Caldwell » July 8th, 2011, 11:38 pm

Cristián Lávaque wrote:Jason, does 'ws_plugin__s2member_after_handle_profile_modifications' work for both, front and admin modifications?
No, in order to hit the admin side for Profile modifications, you'll need this additional Hook.
Code: Select all
add_action('ws_plugin__s2member_during_users_list_update_cols', 'member_directory_include'); 
$vars['user_id'] is available with this Hook as well.
I'll see if I can get a Hook in there that covers all of these with just one action Hook in a future release.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 8th, 2011, 11:54 pm

Sounds great! Thanks! :)

Code: Select all
<?php
add_action
('ws_plugin__s2member_during_configure_user_registration_front_side''member_directory_include');
add_action('ws_plugin__s2member_during_configure_user_registration_admin_side''member_directory_include');
add_action('ws_plugin__s2member_after_handle_profile_modifications''member_directory_include');
add_action('ws_plugin__s2member_during_users_list_update_cols''member_directory_include'); 

function 
member_directory_include($vars = array ()) {
    
$s2_custom_fields get_user_field('s2member_custom_fields'$vars['user_id']);

    if (
$s2_custom_fields['member_directory_include'] === '1')
        
add_user_meta($vars['user_id'], '_tern_wp_member_list''My List');
    else
        
delete_user_meta($vars['user_id'], '_tern_wp_member_list');
 }
?>
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby Jason Caldwell » July 9th, 2011, 12:08 am

Nice work. May I suggest the use of "update_user_meta" or "update_user_option" instead of "add_user_meta"? That way if the meta data does not yet exist, it will be created. If it does, it will be updated. See: http://codex.wordpress.org/Function_Ref ... _user_meta
http://codex.wordpress.org/Function_Ref ... ser_option

Also, just a quick FYI here.
The main distinction between *_user_meta() functions and *_user_option() functions, is that *_user_option() stores data in a way that associates the information with a User on a particular instance of WordPress ( i.e. it's NOT global by default ). Whereas *_user_meta() functions store data that should be associated with all instances of WordPress ( i.e. all Child Blogs of a Multisite Network ). Thus, in practice, it is best to store most data on a per-instance basis, with *_user_option() functions.

Neither way is wrong or right though, it just depends on your preference. In the case of s2Member, almost all s2Member Profile fields are stored on a per-instance basis, with *_user_option() functions.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Capture custom registration field during registration pr

Postby nashvillegeek » July 9th, 2011, 1:49 am

Thanks to you both for helping with this. I feel like I've been taken to school. I will try this out tomorrow.

This thread in particular has answered so many looming questions for me. Great information and examples! I imagine others will take alot from this, as well.

Thank you! I'll get back with the results.
User avatar
nashvillegeek
Registered User
Registered User
 
Posts: 36
Joined: June 3, 2011
Location: Nashville TN

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 9th, 2011, 2:41 am

Jason Caldwell wrote:Nice work. May I suggest the use of "update_user_meta" or "update_user_option" instead of "add_user_meta"?


Great tip! I didn't know about those functions, I had just rearranged the code. Thanks for the lesson. :)

Quick question, though: I didn't find a delete_user_option function, just the delete_option one, which doesn't take a user parameter. Should I then, instead, use update_user_option and give it an empty or false value? E.g.

update_user_option($vars['user_id'], '_tern_wp_member_list', '');
or
update_user_option($vars['user_id'], '_tern_wp_member_list', false);

If so, then the hack could be like this

Code: Select all
<?php
add_action
('ws_plugin__s2member_during_configure_user_registration_front_side', 'member_directory_include');
add_action('ws_plugin__s2member_during_configure_user_registration_admin_side', 'member_directory_include');
add_action('ws_plugin__s2member_after_handle_profile_modifications', 'member_directory_include');
add_action('ws_plugin__s2member_during_users_list_update_cols', 'member_directory_include'); 

function member_directory_include
($vars = array ()) {
    $fields = get_user_field('s2member_custom_fields', $vars['user_id']);
    update_user_option($vars['user_id'], '_tern_wp_member_list', ($fields['member_directory_include'] === '1' ? 'My List' : false));
}
?>


nashvillegeek wrote:Thanks to you both for helping with this. I feel like I've been taken to school. I will try this out tomorrow.

This thread in particular has answered so many looming questions for me. Great information and examples! I imagine others will take alot from this, as well.

Thank you! I'll get back with the results.


Cool. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby Jason Caldwell » July 9th, 2011, 2:49 am

Awesome. Thanks for the follow-ups.

Yes, delete_user_option() does exist, and that's what I would use, as opposed to update_user_option() with an empty value. This function is available, it's just not documented in the Codex yet. I remember I had to dig for this one too.

/wp-includes/user.php
Code: Select all
/**
* Delete user option with global blog capability.
*
* User options are just like user metadata except that they have support for
* global blog options. If the 'global' parameter is false, which it is by default
* it will prepend the WordPress table prefix to the option name.
*
* @since 3.0.0
* @uses $wpdb WordPress database object for queries
*
* @param int $user_id User ID
* @param string $option_name User option name.
* @param bool $global Optional. Whether option name is global or blog specific. Default false (blog specific).
* @return unknown
*/
function delete_user_option( $user_id, $option_name, $global = false )
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 9th, 2011, 2:55 am

Oh, thanks! I searched the codex and called it a day, I should have searched the source files... Thanks again!

Code: Select all
<?php
add_action
('ws_plugin__s2member_during_configure_user_registration_front_side''member_directory_include');
add_action('ws_plugin__s2member_during_configure_user_registration_admin_side''member_directory_include');
add_action('ws_plugin__s2member_after_handle_profile_modifications''member_directory_include');
add_action('ws_plugin__s2member_during_users_list_update_cols''member_directory_include'); 

function 
member_directory_include($vars = array ()) {
    
$s2_custom_fields get_user_field('s2member_custom_fields'$vars['user_id']);

    if (
$s2_custom_fields['member_directory_include'] === '1')
        
update_user_option($vars['user_id'], '_tern_wp_member_list''My List');
    else
        
delete_user_option($vars['user_id'], '_tern_wp_member_list');
 }
?>
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Capture custom registration field during registration pr

Postby nashvillegeek » July 9th, 2011, 8:14 am

You guys rock! I had to revert to the *_user_meta method. The *_user_options method added the WP database prefix to the meta key, which was inconsistent with what Member List was looking for. When I switched back to the *_user_method, it worked perfectly.

Here's the code that worked:

Code: Select all
<?php
    add_action
('ws_plugin__s2member_during_configure_user_registration_front_side', 'member_directory_include');
    add_action('ws_plugin__s2member_during_configure_user_registration_admin_side', 'member_directory_include');
    add_action('ws_plugin__s2member_after_handle_profile_modifications', 'member_directory_include');
    add_action('ws_plugin__s2member_during_users_list_update_cols', 'member_directory_include'); 

    function member_directory_include
($vars = array ()) {
        $s2_custom_fields = get_user_field('s2member_custom_fields', $vars['user_id']);

        if ($s2_custom_fields['member_directory_include'] === '1')
            update_user_meta($vars['user_id'], '_tern_wp_member_list', 'Enter List Name');
        else
            delete_user_meta
($vars['user_id'], '_tern_wp_member_list');
     }
?>


I really appreciate your help.

Thank you, both!

Scott
User avatar
nashvillegeek
Registered User
Registered User
 
Posts: 36
Joined: June 3, 2011
Location: Nashville TN

Re: Capture custom registration field during registration pr

Postby Cristián Lávaque » July 9th, 2011, 9:05 pm

:)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010


Return to s2Member Plugin

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron