latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

HOWTO: Automating Custom Capability Creation

Home Forums Community Forum Unofficial Extensions/Hacks HOWTO: Automating Custom Capability Creation

This topic contains 2 replies, has 2 voices. Last updated by  aly ielts 4 years, 5 months ago.

Topic Author Topic
Posted: Monday Jul 16th, 2012 at 4:59 pm #19414
aly ielts
Username: klc

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(); 
*/

}

List Of Topic Replies

Viewing 2 replies - 1 through 2 (of 2 total)
Author Replies
Author Replies
Posted: Tuesday Jul 17th, 2012 at 9:12 am #19501
Eduan
Username: Eduan
Moderator

Why don’t you use the option under Dashboard -› s2Member® -› API / Notifications -› Registration Notifications, and make it so that when that notification calls the mu-plugin, it addes those fields to the user.

I believe you can customize it so that it sends you the level the user had, not sure though.

Just a suggestion, hope this helps. :)

Posted: Tuesday Jul 17th, 2012 at 11:59 am #19530
aly ielts
Username: klc

Dashboard -› s2Member® -› API / Notifications -› Registration Notifications

I wasn’t able to figure out how to create a new page using the system above and worried that i couldn’t create a dynamically generated custom capability.

Thanks :)

Viewing 2 replies - 1 through 2 (of 2 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.