Search code examples
phpwordpressadvanced-custom-fieldsmultisitegravityforms

Multisite - passing ACF values from current site to Gravity Forms form from another site on multisite


I'm trying to pass data from Advance Custom Fields into my Gravity Forms form. The problem I'm running into is that the form is being generated from another site on the multisite via:

<?php switch_to_blog(1);?>   
     <?php echo do_shortcode( '[gravityform id="3" title="false" 
     description="false"]' ); ?>
<?php restore_current_blog(); ?>

In my functions.php file (for both blog 1 and the current site), I have:

add_filter( 'gform_field_value_lead_source_detail', 'populate_lead_source_detail' );
function populate_lead_source_detail( $value ) {

   $leadsourcedetail = get_field('lead_source_detail', $post->ID);
   return $leadsourcedetail;
}

add_filter( 'gform_field_value_lifecycle_status', 'populate_lifecycle_status' );
function populate_lifecycle_status( $value ) {
   $lifecycle = get_field('lifecycle_status', $post->ID);
   return $lifecycle;
}

add_filter( 'gform_field_value_lead_source', 'populate_lead_source' );
function populate_lead_source( $value ) {
   $leadsource = get_field('lead_source', $post->ID);
   return $leadsource;
}

add_filter( 'gform_field_value_channel', 'populate_channel' );
function populate_channel( $value ) {
   $channel = get_field('channel', $post->ID);
   return $channel;
}

add_filter( 'gform_field_value_expected_op_type', 'populate_expected_op_type' );
function populate_expected_op_type( $value ) {
   $expected = get_field('expected_op_type', $post->ID);
   return $expected;
}

The ACF fields work perfectly on blog 1, where the Gravity Form is being generated from, but I can't get them to pass the data to the form on the current current blog.

To make sure the ACF fields work (in general), I tested <?php the_field(); ?> for each of them, and the data is definitely there... so I'm assuming it has something to do with the fact that I'm pulling the form from a different site on the multisite.

Anyone have any ideas on how to pass data from my current site to the blog 1 form? Thanks in advance!


Solution

  • Went about this a completely different way. Found this in the Gravity Forms docs: https://docs.gravityforms.com/using-dynamic-population/ -- under shortcodes:

    [gravityform id=1 field_values=’parameter_name1=value1&parameter_name2=value2′]

    Wish I would've found this before I spent a whole day on this. Lol... Here were my steps after that:

    1. I exported/imported the Gravity Form onto another site in the multisite that shared the same theme (so I wasn't working on two different functions.php files; this is why the blog id and Gravity Forms id are different below).

    2. I removed all of the functions I originally created in the functions.php file that I stated earlier. (populate_lead_source_detail, populate_lifecycle_status, etc.)

    3. I created variables from the ACF fields for that page.

      <?php $lead_source_detail = get_field('lead_source_detail'); $lifecycle_status = get_field('lifecycle_status'); $lead_source = get_field('lead_source'); $channel = get_field('channel'); $expected_op_type = get_field('expected_op_type'); ?>

    4. I called the variables in the shortcode. (See below for my final code.)

                      <?php switch_to_blog(11);?>   
      
                          <?php echo do_shortcode( '[gravityform id="1" title="false" description="false" field_values="lead_source_detail='.$lead_source_detail.'&lifecycle_status='.$lifecycle_status.'&lead_source='.$lead_source.'&channel='.$channel.'&expected_op_type='.$expected_op_type.'"]' ); ?>
      
                      <?php restore_current_blog(); ?>