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.

Add custom capabilities by getting a drop down value

  1. I am trying to add custom capabilities to user according to a value submitted from gravity form selected value.

    I have tried gfrom_pre_render and gform_after_submission without any success.

    Here is an example that I tried.

    add_action("gform_after_submission_1", "after_submission", 10, 2);
    
    function after_submission($entry, $form){
         $user = wp_get_current_user();
         $capabilities = $entry["17"] . ', '. $entry["18"] .', '. $entry["19"];
    
    if ($capabilities = "1") {$user -> add_cap('1');}
    if ($capabilities = "2") {$user -> add_cap('2');}
    if ($capabilities = "3") {$user -> add_cap('3');}
    }

    It seems the code above add all values that field ID 17, 18, 19 has to user capabilities.

    So I tried another with gform_pre_render

    '
    add_filter("gform_pre_render", "dropdown_custom_capabilities");
    add_filter("gform_admin_pre_render", "dropdown_custom_capabilities");
    function dropdown_custom_capabilities($form){
    if($form["id"] != 1)

    return $form;

    $terms = get_terms("custom-capabilities");
    $items = array();
    $items[] = array( "text" => __('Select capabilities...', "value" => 0 );

    foreach($terms as $term)
    $items[] = array( "text" => $term->name, "value" => $term->slug );

    foreach($form["fields"] as &$field)

    if($field["id"] == 17 OR 18 OR 19){

    $field["cssClass"] = 'custom-capabilities';
    $field["choices"] = $items;
    }

    return $form

    if ($form = "1") {$user -> add_cap('1');}
    if ($form = "2") {$user -> add_cap('2');}
    if ($form = "3") {$user -> add_cap('3');}

    }

    '

    Code above does not work.

    Would you possibly to point me a right direction for this matter?

    Posted 12 years ago on Thursday February 14, 2013 | Permalink
  2. You need to debug.

    One line 6 in your first example, what does $capabilities contain? We can't see your form or values to know what field ID 17, 18 and 19 contain, but you're concatenating them all, so $capabilities could look like:

    1, 2, 3
    or
    , , 3
    or
    , ,
    or even
    1, ,
    or
    , 2, 3

    If you print the $capabilities variable or log it somewhere, that will go a long way to seeing why your logic on lines 7, 8 and 9 does not work properly. One thing immediately wrong with it is that your string will always contain a comma, so I'm not sure how any of that could work, because it should never be equal to 1, 2 or 3.

    I'm not sure how your second example is supposed to work with gform_pre_render but we should focus on getting just one of these to work.

    Posted 12 years ago on Sunday February 17, 2013 | Permalink
  3. Thank you for reply!
    I guess I need to make it simple.

    Let's assume that field ID 17 of Form ID 1 has dropdown values such as "A", "B" and "C"
    and we modified code on line 5 to $capabilities = $entry['17];

    With this $capabilities contain all dropdown values of Field ID 17 of Form ID 1, which are "A", "B" and "C", thus add custom capabilities "a", "b" and "c" to the current user.

    What I would like to know is to add only "B", if a user selected "B"

    Posted 12 years ago on Sunday February 17, 2013 | Permalink
  4. What code would you use to accomplish this? Please show us how you are going about this.

    Posted 12 years ago on Monday February 18, 2013 | Permalink
  5. I have somehow managed to have it work. So blind I was unable to recognise a solution right in front of myself.

    Here is my code:

    [php]
    add_action('gform_after_submission_(your form ID)', 'add_new_capabilities', 10, 2);
    function add_new_capabilities($entry, $form){
    
    $current_user = wp_get_current_user();
            $custom_cap1 = $entry["(field_ID)"];
    	$custom_cap2 = $entry["(field_ID)"];
    
    $current_user -> add_cap($custom_cap1);
    $current_user -> add_cap($custom_cap2);
    }

    This code will add data to your datatable wp_usermeta - wp_capabilities without any arraying problem.

    As for the code I referred previously, It seems $entry returns all values contained in the dropdown as it is used in 'if phrase.' I don't know why.

    Any simplification and suggestion are more than welcomed.

    Posted 12 years ago on Tuesday February 19, 2013 | Permalink
  6. Your code here looks pretty simple. If it works for you, that's the important thing.

    Posted 12 years ago on Tuesday February 19, 2013 | Permalink