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™

Custom Registration form w/ custom capabilities/custom field

s2Member Plugin. A Membership plugin for WordPress®.

Custom Registration form w/ custom capabilities/custom field

Postby kenduret » September 2nd, 2010, 12:44 am

Thanks for the great plugin. Loving it!

I have set up a registration form in my theme for free users to sign up with that works great. I have a second type of free subscriber that I want to add a custom capability and custom registration field to their registration form (a second custom form specifically for them).

This second type of user is one where a corporation has licensed the site to their members and has paid in bulk. Therefore the user needs a free level with a custom cap of "view" and a registration field of "Licensee" with a value of "Big Corp".

I've been struggling to get this working and have tried several methods from the forum with no luck. Here's a bit of the form I'm using that works for free subscribers.


Code: Select all
/* Load registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );

/* Check if users can register. */
$registration = get_option( 'users_can_register' );

/* If user registered, input info. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'adduser' ) {

    $userdata = array(
        'user_pass' => esc_attr( $_POST['pass1'] ),
        'user_login' => esc_attr( $_POST['user-name'] ),
        'user_email' => esc_attr( $_POST['email'] ),
        'first_name' => esc_attr( $_POST['first-name'] ),
        'last_name' => esc_attr( $_POST['last-name'] ),
        'role' => get_option( 'default_role' ),
    );

if ( ($_POST['user-name'] ) && ( $_POST['email'] ) && ( $_POST['pass1'] ) && ( $_POST['pass1'] == $_POST['pass2'] ) ) {
        $new_user = wp_insert_user( $userdata );
    }
 


Any tips on how to add the custom field and value and the custom capability when the form is submitted? I want these to be hidden from the new user. Any help would be greatly appreciated.
User avatar
kenduret
Registered User
Registered User
 
Posts: 13
Joined: September 2, 2010
Location: Santa Cruz CA

Re: Custom Registration form w/ custom capabilities/custom f

Postby Jason Caldwell » September 2nd, 2010, 2:20 pm

Hi there. Thanks for the excellent question.

Yes, here is how I would handle it.
Code: Select all
add_action("ws_plugin__s2member_during_configure_user_registration_front_side", "my_function");
function my_function($vars)
    {
        $user = new WP_User($vars["user_id"]);
        $user->add_cap("access_s2member_ccap_view");
        update_user_option($user->ID, "licensee", "Big Corp");
        // print_r($vars); for a bigger picture during development.
        // You can also see: /s2member/includes/functions/register-access.inc.php
        // - look for Hook: ws_plugin__s2member_during_configure_user_registration_front_side
    } 
~ 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: Custom Registration form w/ custom capabilities/custom f

Postby kenduret » September 3rd, 2010, 4:12 pm

Excellent. Works great. The above code created a new usermeta field named wp_licensee. I modified it to the following to have the key/value show up for users in the admin as an s2member custom field:

Code: Select all

add_action
("ws_plugin__s2member_during_configure_user_registration_front_side", "my_function");
function my_function($vars) {
            $user = new WP_User($vars['user_id']);
            $user->add_cap('access_s2member_ccap_view');
            $custom_fields = get_user_option("s2member_custom_fields");
            $custom_fields["licensee"] = "Big Corp";
            update_user_option($user->ID, "s2member_custom_fields", $custom_fields);
        }
 


After some initial testing this seems like a great solution to registering a free user that has capabilities to view the site. I can also filter them if a licensee cancels their contract so I can remove the capabilities for all users of a specific organization.

My next step is an admin page to manage licensees. Can you point me to where s2member hooks into the user admin screen for reference?

I have to say again- I really like s2. I have been wrestling with Wishlist member for the last 6 months and s2 has made this a whole new game. Say goodbye, Wishlist.

Thank you.
User avatar
kenduret
Registered User
Registered User
 
Posts: 13
Joined: September 2, 2010
Location: Santa Cruz CA

Re: Custom Registration form w/ custom capabilities/custom f

Postby Jason Caldwell » September 7th, 2010, 6:03 pm

Thank you VERY much for reporting back with that code sample.
~ Much appreciated.

Dealing with admin panels is just a bit trickier at the moment.
This will change in a future release of s2Member, but for now, here is how it's done:

- Relevant file:
/s2member/includes/functions/users-lists.inc.php

- Suggested Hooks:
ws_plugin__s2member_during_users_list_edit_cols_after
ws_plugin__s2member_during_users_list_update_cols

Sample code snippet:
Code: Select all
// This is where we'll add the HTML code for the form field(s).
add_action("ws_plugin__s2member_during_users_list_edit_cols_after", "my_special_custom_fields");
function my_special_custom_fields($vars){
    
    $user 
= $vars["user"];
    $fields = get_user_option ("s2member_custom_fields", $user->ID);
    
    echo 
'<tr>';
    echo '<th><label>Licensee:</label></th>';
    echo '<td><input type="text" name="ws_plugin__s2member_profile_licensee" value="' . format_to_edit ($fields["licensee"]) . '" class="regular-text" /></td>';
    echo '</tr>';
}

// Now we'll dynamically update the fields through this Hook.
// This uses the __refs array ( which are variables - by reference ).
add_action("ws_plugin__s2member_during_users_list_update_cols", "store_my_special_custom_fields");
function store_my_special_custom_fields($vars){
    $fields =& $vars["__refs"]["fields"];
    $fields["licensee"] = $_POST["ws_plugin__s2member_profile_licensee"];
}
 

For the benefit of other readers.

s2Member is currently undergoing serious changes ( improvements ) to its support for Custom Fields. Some of these code samples are likely to become outdated with the release of s2Member v3.2.6+. However, as these code samples become outdated, the functionality needed, will be replaced by more powerful controls within the s2Member option panels ( in your Dashboard ).

Every attempt will be made to preserve Hooks/Filters and the functionality they provide. However, should we find it necessary to change them for the benefit of future development, we will not hesitate to do so.
~ 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


Return to s2Member Plugin

Who is online

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

cron