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.

Else If PHP Statement

  1. travin
    Member

    I need just a little help here with some php syntax. I am trying to add custom capabilities and need different actions based on different forms. I tried the following but got a syntax error. Hoping you can tell me what I did wrong. Thanks in advance.

    if ($entry['form_id'] = 11)
    
      wp_set_current_user( $user_id );
      $user = wp_get_current_user();
      $user->add_cap("access_s2member_ccap_creditexpertpro");
    
    elseif ($entry['form_id'] = 12)
    
      $user = wp_get_current_user();
      $user->add_cap("access_s2member_ccap_creditexpertpro");
    
    else
    
      return;
    Posted 12 years ago on Sunday September 4, 2011 | Permalink
  2. Try this:

    [php]
    if ($entry['form_id'] == 11) {
      wp_set_current_user( $user_id );
      $user = wp_get_current_user();
      $user->add_cap("access_s2member_ccap_creditexpertpro");
    }
    
    elseif ($entry['form_id'] == 12) {
      $user = wp_get_current_user();
      $user->add_cap("access_s2member_ccap_creditexpertpro");
    }
    else
      return;

    Just a couple things. If you have multiple statements to execute after the "if", they need be be wrapped in curly braces. And to test for equality, you need to use == (or ===) rather than a single =, which just assigns the value on the right to the variable on the left.

    I have no idea if the code will work, but the syntax should be correct. If you need help accomplishing something, please post the question and we'll help you out.

    Posted 12 years ago on Monday September 5, 2011 | Permalink
  3. travin
    Member

    Thanks Chris. I am just trying to put a little logic in the functions.php file. I am using gravity forms for user registration to a membership site plus purchasing a software product. Before, I only had 1 form to deal with when adding the custom capability. Now, I have multiple forms to deal with. Now that I think about it, my code design won't work the way I had envisioned it. Form 11 is used for purchasing a product, registering a user, and setting a custom capability in the user meta. Form 12 is purchasing the same product and setting the custom capability, but the user is already registered, so no registration is needed.

    I currently use this code to set the custom capability when a user is registered:

    add_action("gform_user_registered", "add_custom_user_meta", 10, 3);  
    
    function add_custom_user_meta($user_id, $config, $entry) {  
    
    if($entry['form_id'] != 11)
    	return;  
    
    wp_set_current_user( $user_id );
    $user = wp_get_current_user();
    $user->add_cap("access_s2member_ccap_creditexpertpro");
    }

    This works perfectly as long as the user is using form 11. So how would I fire the code when form 12 is complete? The user will be logged in when this form is used. It should happen after the paypal transaction is complete. I am thinking this should work:

    add_action("gform_paypal_fulfillment", "process_order", 10, 4);
    function process_order($entry, $config, $transaction_id, $amount) {
    
    if($entry['form_id'] != 12)
    	return;  
    
    $user = wp_get_current_user();
    $user->add_cap("access_s2member_ccap_creditexpertpro");
    }

    Last question. Lets say I need to add different capabilities based on the form id after a successful registration. How would I use the else - if statement to accomplish this (which my original thought when making this post). I tried to use the if elseif statement but obvisouly didn't format it correctly. I was going to add the the if statement into the function to it would always check during user registration. During registration, if one of the form numbers is use, it would set the custom capability as needed.

    Hope this makes sense. Thanks for all your help.

    Posted 12 years ago on Monday September 5, 2011 | Permalink
  4. Sounds like the code in your second block there should work for form 12. You just need to find a hook to use that is fired when your requirement is satisfied. If that's gform_paypal_fulfillment in your case, it's perfectly OK to hook your function to it. Try it and let us know if it works for you.

    In your first block of code which uses gform_user_registered, can you set the custom capabilities for any user who registers? Is that what you're asking? If so, I think you should be able to do this:

    http://pastebin.com/zHVhE2qC

    That code will run whenever a member is registered, so that's a good place to set your capabilities. The capabilities are tied to the registration, not the PayPal payment (I think?)

    Posted 12 years ago on Tuesday September 6, 2011 | Permalink