Creating Gravity Form Parameters And Regular Shortcodes From Site Session

Greetings Gurus.
I am so so stumped.
I am probably going to cause a few giggles as I sure I am in a "cant see the forest for the trees" scenario here.
It's been about 3 hours.
I give up and hope that someone out there can point out the error of my ways.

I have successfully captured a session, and echo'd the results. yay.

The last echo (line 32) however, is as far as my variables seem to want to travel.

What is it that I have to do to get the $gformxml->xxxxxxxx part of my code to travel into the add_filter and add_shortcode blocks?

The error I get on each of these blocks is

Notice: Undefined variable: gformxml in [PATH REMOVED FOR SECURITY] on line (each line defining the variable)

I dont get it. I can echo this, but I can't assign it to a shortcode!?

Help!

Here is my code (of course, once it works, I want to get rid of the echo's)

<?php
	if(class_exists('WP_Session'))
	{
		$wp_session = WP_Session::get_instance();				

		if ($wp_session['socialbug_name'] != null && strlen($wp_session['socialbug_name']) > 0)
		{
		?>
		<?php
		$gformid = $wp_session['socialbug_affiliate_id'];
		?>
		<?php
		$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
		$url = '[REMOVED FOR SECURITY]' . $gformid;
		$gformxml = file_get_contents($url, false, $context);
		$gformxml = simplexml_load_string($gformxml);

		$gformFN = $gformxml->FirstName;
		$gformLN = $gformxml->LastName;
		$gformEMAIL = $gformxml->Email;
		$gformPH = $gformxml->PhoneNumber;
		$gformPIC = $gformxml->AffiliatePictureURL;

echo $gformxml->FirstName;
echo '<br>';
echo $gformxml->LastName;
echo '<br>';
echo $gformxml->Email;
echo '<br>';
echo $gformxml->PhoneNumber;
echo '<br>';
echo $gformxml->AffiliatePictureURL;

		// Add Gravity Forms Consultants Information
		// Email Address
		add_filter('gform_field_value_consultant_gform', 'fn_get_consultant_webemail');
		function fn_get_consultant_webemail($value){
		$bizemail = $gformxml->Email;
		return $bizemail;
		}
		// Name
		add_filter('gform_field_value_consultantname_gform', 'fn_get_consultant_gfname');
		function fn_get_consultant_gfname($value){
		$bizname = $gformxml->FirstName;
		return $bizname;
		}
		// Phone
		add_filter('gform_field_value_consultantphone_gform', 'fn_get_consultant_gfphone');
		function fn_get_consultant_gfphone($value){
		$bizphone = $gformxml->PhoneNumber;
		return $bizphone;
		}

		// Add Shortcodes
		// Email Address
		add_shortcode( 'consultant_email_addr' , 'fn_get_consultant_emailaddr' );
		function fn_get_consultant_emailaddr(){
		$consultantemailaddr = $gformxml->Email;
		return $consultantemailaddr;
		}

		} else {
		// Email Address
		add_filter('gform_field_value_consultant_gform', 'fn_get_consultant_webemail');
		function fn_get_consultant_webemail($value){
		return 'cs@intimatetickles.com';
		}
		// Add Shortcode Consultants Email
		add_shortcode( 'consultant_emailaddr' , 'fn_get_consultant_emailaddr' );
		function fn_get_consultant_emailaddr(){
		$consultantemailaddr = 'cs@intimatetickles.com';
		return $consultantemailaddr;
		}

}
	}

?>
  • Milan

    Hello Deryk ,

    Hope you are well today and thanks for asking us. :slight_smile:

    I can see that you are trying to access variables in filters and shortcode hooks which are initiated at a time of page loading. I am not sure how you are initiating those variables but based on your code I can say that you are initiating those to current session using some hook or filter. But that's not the area where we need to focus right now. :slight_smile:

    Here are point of your interest. :slight_smile:

    #1) Call back functions of filters and shortcodes can't access variable defined and initiated in outside scope of a function. Please note here that this is tricky concept of WordPress so traditional scope concept of vanilla php won't work here. WordPress has its own way of handling things. So thing is that

    You can't really access variables into shortcodes and filters callbacks unless they are global by its nature or you are accessing them directly using session mechanism or any other direct access mechanism ( transient, option api )

    So to fix this, please try to access those variables by using $wp_session object. This way you should not have issue to access those. :slight_smile:

    Example Snippet.

    add_filter('gform_field_value_consultant_gform', 'fn_get_consultant_webemail');
    		function fn_get_consultant_webemail($value){
    			// $bizemail = $gformxml->Email;
    			// return $bizemail;
    			$gfxml = get_the_gformxml();
    			if( !empty( $gfxml ) ){
    				$bizemail = $gformxml->Email;
    				return $bizemail;
    			}
    
    			return '';
    		}
    
    		function get_the_gformxml(){
    			$gformxml = "";
    			if ( $wp_session['socialbug_name'] != null
    					&& strlen($wp_session['socialbug_name']) > 0 ){
    				$gformid = $wp_session['socialbug_affiliate_id'];
    				$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
    				$url = '[REMOVED FOR SECURITY]' . $gformid;
    				$gformxml = file_get_contents($url, false, $context);
    				$gformxml = simplexml_load_string($gformxml);
    			}
    			return $gformxml;
    		}

    If by any way this explanation seems confusing to you, please ping me back with your confusions. :slight_smile:

    Have a nice day. :slight_smile:
    Best Regards,
    Milan

  • Deryk

    My bad. I had the screen scrolled to the right, so didnt see the add filter.

    Unfortunately, No Joy.

    I pulled your code in exactly as you see below. The error I get is
    Notice: Undefined variable: wp_session in /***/shopping-with.php on line 33

    <?php
    	if(class_exists('WP_Session'))
    	{
    		$wp_session = WP_Session::get_instance();				
    
    		if ($wp_session['socialbug_name'] != null && strlen($wp_session['socialbug_name']) > 0)
    		{
    		?>
    			<div style="text-align:center; font-weight: normal;">
    				<b>Shopping With:</b> <?php echo $wp_session['socialbug_name']; ?> |
    				<b>City:</b> <?php echo $wp_session['socialbug_city']; ?> |
    				<b>Phone:</b> <?php echo $wp_session['socialbug_phone']; ?> |
    				<b>Email:</b> <?php echo $wp_session['socialbug_email']; ?>
    			</div>
    
    		<?php
    	// ADD A SHORTCODE
    add_filter('gform_field_value_consultant_gform', 'fn_get_consultant_webemail');
    		function fn_get_consultant_webemail($value){
    			// $bizemail = $gformxml->Email;
    			// return $bizemail;
    			$gfxml = get_the_gformxml();
    			if( !empty( $gfxml ) ){
    				$bizemail = $gformxml->Email;
    				return $bizemail;
    			}
    
    			return '';
    		}
    
    		function get_the_gformxml(){
    			$gformxml = "";
    			if ( $wp_session['socialbug_name'] != null
    					&& strlen($wp_session['socialbug_name']) > 0 ){
    				$gformid = $wp_session['socialbug_affiliate_id'];
    				$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
    				$url = 'http://***/AffiliateId=' . $gformid;
    				$gformxml = file_get_contents($url, false, $context);
    				$gformxml = simplexml_load_string($gformxml);
    			}
    			return $gformxml;
    		}
    	// END ADD SHORTCODE
    
    		}
    	}
    ?>

    Thanks for any suggestions you might provide.

    D.

  • Milan

    Hello Deryk

    Hope you are well today and sorry for delay here.

    Actually I did not provide you final snippet but a example for you to understand and make it according to your need. :slight_smile: Honestly this type of custom coding we are not supporting here at support forum as it falls out of scope of this forum.

    You could hire developer for this or use our Job Portal for further insights and proper snippet. Our Job Portal is here,
    Job Portal

    Hope this helps you. :slight_smile:
    Kind Regards,
    Milan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.