PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Adding Custom Capability Issues

  1. travin
    Member

    Ok. I am going out of my mind here. I think I have tried everything I know to do and still am unable to get this function to work. Here is what i want to do:

    I am using s2member for my membership plugin. When I sign a person up using the userregistration plugin, I select s2member_level1 as the role. If I just sign up as is, the following is in the wp_capabilities metakey field:

    a:1:{s:15:"s2member_level1";s:1:"1";}

    Now, if I add the custom capability manually, I get this in the wp_capabilities metakey field:

    a:2:{s:15:"s2member_level1";s:1:"1";s:26:"access_s2member_ccap_test";s:1:"1";}

    So, I have tried the following code in the functions.php field:

    add_action("gform_user_registered", "add_custom_user_meta", 10, 3);
    function add_custom_user_meta($user_id, $config, $entry){
    add_user_meta($user_id, 'wp_capabilities', $entry[22]);
    }

    I have also tried:

    add_user_meta($user_id, 'wp_capabilities', 'access_s2member_ccap_test');
    add_user_meta($user_id, 'wp_capabilities', "access_s2member_ccap_test");
    
    $custom_cap = 'access_s2member_ccap_test';
    add_user_meta($user_id, 'wp_capabilities', $custom_cap);
    
    $user = $user_id;
    $user->add_cap("access_s2member_ccap_test");

    And I have even tried adding a custom meta in the user registration field and naming it "wp_capabilities". No go on that either.

    Every thing I have tried has not resulted in adding the custom capability to the user profile. The user registers just fine, but only at the role level.

    Also, how do I only target specific registration forms as using "gform_user_registered_6" does not work.

    Thanks for all your help. The form I have been working on is form id 6, website is https://www.freeclassifiedproducts.com/sign-up-for-a-credit-repair-membership?s2-ssl=yes

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  2. To clarify, we're talking about the "wp_usermeta" table?

    And, what should the meta_key and meta_value be when it's working properly? I can't tell from your examples above which one is good and which one is bad.

    If you can show me one record from the wp_usermeta table, with the meta_key and meta_value, I think we can figure out what needs to happen.

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  3. travin
    Member

    Yes, wp_usermeta. Metakey is "wp_capabilities". The value for a normal level 1 user after registration is:

    a:1:{s:15:"s2member_level1";s:1:"1";}

    The value for a level 1 user with a custom capability "access_s2member_ccap_test" is:

    a:2:{s:15:"s2member_level1";s:1:"1";s:26:"access_s2member_ccap_test";s:1:"1";}

    Thanks

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  4. Hi Travin,

    Instead of assigning the capability to each user, you might want to consider assigning the capability to the role. This way all users with that role will also have that capability which will makes things easier to manage.

    Here is a sample code snippet: http://pastie.org/2316170

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  5. travin
    Member

    Unfortunately, that won't work for my set up. I have a free membership and a product that uses the custom capability for registration purposes. I have to be able to add the custom capability some way other than manually.

    Thanks for the help, but I gotta do it the way we have previously discussed.

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  6. Be aware if you are applying capabilities to users rather than roles you may run into future issues with WordPress as they may be deprecating user capabilities in favor of role capabilities only.

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  7. travin
    Member

    Thanks. So is what I am asking to do just not possible or what? I tried using the instructions on wordpress.org and this site to no avail.

    Thanks again for your support.

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  8. travin
    Member

    Been doing a little more testing with the following code:

    add_action("gform_user_registered", "add_custom_user_meta", 10, 3);

    function add_custom_user_meta($user_id, $config, $entry){

    $user_id->add_cap ("access_s2member_ccap_test");

    }

    I get the following error:

    Fatal error: Call to a member function add_cap() on a non-object in /home3/freecla3/public_html/wp-content/themes/Superb/functions.php on line 502

    This code is what s2member uses in their code to add custom capabilities:

    $user->add_cap ("access_s2member_ccap_" . $ccap);

    Does this help at all?

    Posted 12 years ago on Wednesday August 3, 2011 | Permalink
  9. travin
    Member

    I finally got it. Here is the solution I used:

    add_action("gform_user_registered", "add_custom_user_meta", 10, 3);
    function add_custom_user_meta($user_id, $config, $entry) {

    wp_set_current_user( $user_id );
    $user = wp_get_current_user();
    $user->add_cap ("access_s2member_ccap_test");
    }

    i know the wp_set_current_user fuction returns the WP_user object, I just don't know how to access the return value as I really don't know PHP.

    Now, my last question here, how do I perform this routine on select registration forms? For instance, only form 6, but not on form 8?

    Posted 12 years ago on Thursday August 4, 2011 | Permalink
  10. For now you can retrieve the form ID from the $entry.

    [php]
    if($entry['form_id'] != 6)
        return;
    Posted 12 years ago on Friday August 5, 2011 | Permalink
  11. travin
    Member

    I hate to show my ignorance here but how do I use this code snippet?

    Do I put my function between the if and return, or do I add this code in my function.

    I know, NOOB. Thanks for the excellent support.

    Posted 12 years ago on Friday August 5, 2011 | Permalink
  12. Hi Travin,

    It'd go something like this:

    [php]
    add_action("gform_user_registered", "add_custom_user_meta", 10, 3);
    function add_custom_user_meta($user_id, $config, $entry) {
    
    if($entry['form_id'] != 6)
        return;
    
    wp_set_current_user( $user_id );
    $user = wp_get_current_user();
    $user->add_cap ("access_s2member_ccap_test");
    }
    Posted 12 years ago on Friday August 5, 2011 | Permalink
  13. travin
    Member

    Got it, thanks for all your help.

    Posted 12 years ago on Saturday August 6, 2011 | Permalink
  14. Thank you for posting back. Glad you got it worked out.

    Posted 12 years ago on Saturday August 6, 2011 | Permalink

This topic has been resolved and has been closed to new replies.