Problem: Want to create custom capabilities after a new user has signed up.
Options: Paypal button can add custom capabilities but not good for variable named custom capabilities or if site allows open registration (no payment necessary on registration).
More specifically i was looking to automate the features in this howto video:
http://www.s2member.com/videos/716DC24E7E347DC2/
http://www.s2member.com/forums/topic/hacking-s2member-via-hooks-filters/
The code in s2Hacks.php:
add_action( 'user_register', 'mtq_newUserCustomizations' );
/*
* Listening for new user registrations so can make new users:
* o Create a custom page for user.
* o Add custom capabilities to user and page.
*/
function mtq_newUserCustomizations($user_id)
{
//Get user and add custom cap 'username' for s2Member, necessary for protecting page from other users
$user = get_userdata($user_id);
$user_name = strtolower($user->user_login); //custom caps will default to lowercase anyway
$ccap = "access_s2member_ccap_".$user_name;
$user->add_cap($ccap);
//create page for user
//slurp up content from other predefined page
$templatePageId = 4400; //Template page
$templatePage = get_page($templatePageId);
$templatePageContent = $templatePage->post_content;
$my_page = array(
'post_title' => $user->display_name . ' Page',
'post_name' => $user_name, // The name (slug) for your post
'post_content' => $templatePageContent ,
'post_status' => 'publish',
'post_author' => 2, //Some user id
'post_parent' => 3979, //parent page
'post_type' => 'page'
);
// Insert the post into the database
$my_post_id = wp_insert_post( $my_page );
//error check incase post failed.
if ($my_post_id == 0){
error_log("Unable to create my notebook page for new user: ". $user->user_login, 0);
//error_log("You messed up!", 3, "/var/tmp/my-errors.log");
}
//Post success add meta info to page
else{
add_post_meta($my_post_id, "s2member_ccaps_req", $user_name, true);
//TODO Need to set page level to "0 and up". Below code fails to do so.
// Doesn't affect user access to page but nice for completion...
/*
//doesn't work
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_pages] = $GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_pages] . "," . $my_post_id;
error_log("Level 0 pages: " . $GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_pages]);
ws_plugin__s2member_configure_options_and_their_defaults();
*/
}