1

A client wants the following:

  • User clicks on a specific button which takes them to a partner's site.
  • If this is their first time clicking the link, they need to accept terms and conditions.
  • If they've already accepted, button takes them directly to link.
  • The client insists on being able to update everything (URL, button text) themselves, so I'm can't hard code those values into the function.

In order to pull this off, I wrote a shortcode with attributes, and I'm using Gravity Forms hooks (gform_after_submission and gform_confirmation). I want these hooks to reference the shortcode attributes.

So if we use: [big_data_EULA url="http://www.example.com"] then gform_confirmation can use $url.

— —

I've tried two, things:

  1. Including the add_action and add_filter within the shortcode function
  2. Passing the variables from the shortcode to those hooks.

Could anyone help me figure out the right way to get this working?

Here's my code:


function big_data_EULA_function($atts){
   extract(shortcode_atts(array(
      'url' => 'http://www.google.com',
      'confirm_text' => 'ACA Big Data Resource',
      'click_thru_text' => 'ACA Big Data Resource',
      'form' => '22',

   ), $atts));

       /// Some of these variables aren't used in this iteration but I would like to use in final form.
       $EULA_url = $url;
       $EULA_confirm_text = $confirm_text;
       $EULA_click_thru_text = $click_thru_text;
       $EULA_gravity_form = $form;
       $EULA_user_ID = get_current_user_id();
       $EULA_meta_key = "_bigdata_EULA";
       $check_meta = get_user_meta( $EULA_user_ID, $EULA_meta_key, true );

       if ($check_meta) {

         $button ='<div class="box-button white-button optimize-button"><a href="' . $EULA_url . '">' . $click_thru_text . '</a></div>';
         return $button;

       } else {

         ob_start(); // start a buffer
           echo '<div style="display: none; max-width: 60%;" id="hidden-content">';
           echo gravity_form( 22, false, false, false, '', true );
           echo '</div><div class="box-button white-button optimize-button"><a data-fancybox data-src="#hidden-content" href="javascript:;">' . $EULA_confirm_text . '</a></div>';
         $output_string = ob_get_contents();
         ob_end_clean();
         return $output_string;

     };


};

// Update the user_meta flag only if they've actually accepted the EULA, ie, submitted the form.
add_action( 'gform_after_submission_22', 'bigdata_EULA',10, 1 );
  function bigdata_EULA() {
    $EULA_user_ID = get_current_user_id();
    $EULA_meta_key = "_bigdata_EULA";
    $current_date = date("F j, Y");

    update_user_meta( $EULA_user_ID, $EULA_meta_key, $current_date);
  };


// Redirect to the partner site. The redirect URL should come from the shortcode.
add_filter( 'gform_confirmation_22', 'custom_confirmation', 10,1 );
  function custom_confirmation( $confirmation) {
    $EULA_url = 'http://www.google.com';

    $confirmation = array( 'redirect' => $EULA_url );
  return $confirmation;
}


add_shortcode('big_data_EULA', 'big_data_EULA_function');
4
  • 1
    Is the user logged in? If not, you can only save the (first click) value only as cookie.
    – Mike Aron
    Jan 3, 2021 at 20:39
  • 1
    You shouldn't use extract().
    – Pat J
    Jan 3, 2021 at 21:09
  • @MikeAron — The page for this is behind a login. We only need them to agree once. Cookies will expire eventually which is why I wanted to write it to the user_meta
    – Tomasch
    Jan 4, 2021 at 16:54
  • 1
    @PatJ — Thanks!
    – Tomasch
    Jan 4, 2021 at 16:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.