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.

dynamically populate Advanced Custom Field instead of a normal custom field

  1. I'm successfully using Advanced Custom Fields and Gravity Forms in unison. I'm trying to take an extra step with the dynamic population and pre-populate a field from an Advanced Custom Field in a specific post. For example, if i wanted to find an ACF called "field_slug" from post id #1, I would put this in my template:

    <?php echo the_field('field_slug', 1); ?>

    I hoped I could take this and put it into the following tutorial I found on this forum:

    /* Dynamic Population from Custom Field */
    add_filter("gform_field_stock_num", "populate_stock");
    function populate_stock($value){
        global $post;
        return get_post_meta($post->ID, 'stock_num', true);
    }

    First, I simplified it to make sure it was filling out the right input field (it was, and "woo" appeared just fine in the form's desired input field).

    add_filter('gform_field_value_trend_values', 'trend_auto_populate');
    function trend_auto_populate($value){
    	return 'woo';
    }

    However, when I try to add a truly dynamic field like ACF the_field(), The values do not appear in the input, they appear outside of the box near the header. Here is the final code I've put together. Again, the $value comes back just fine, but it doesn't "return" inside the input as i had hoped.

    add_filter('gform_field_value_trend_values', 'trend_auto_populate');
    function trend_auto_populate($value){
    	$recent_snapshot = 1373;
    	$value = the_field('trend_values', $recent_snapshot);
    	return $value;
    }

    So my question is, how can I pre-populate an ADVANCED custom field rather than just a string?

    Posted 12 years ago on Monday March 5, 2012 | Permalink
  2. I figured out the issue. Instead of trying to create a "global" set of ACF's, I purchased the options add-on which lets you set global options instead of page or post specific options (a great way to build site preferences with forms that alter the CSS for admins who want customization). I check "dynamically populate" in gravity forms.. then put this into my functions.php file:

    /* Dynamically populate Gravity
    ------------------------------------ */
    add_filter('gform_field_value_name', 'my_acct_name');
    function my_acct_name($value){
        return get_field('name', 'options');
    }

    in the get_field function, "name" is the field name and options is the options add-on (global fields rather than post fields). If i wanted to pull a basic advanced custom field it would be this:

    get_field('name')

    So this pulls the advanced custom field fine now!

    This has presented a new problem. How do you get data to push through a gravity form to a global option? I thought the obvious answer was "Custom Field Name" in the GF Projects tab. Luckily, the field i setup for "name" was listed in the existing drop-down. However, this is not working. I'm assuming that it's trying to send it to a regular ACF and not a global option.

    Any help would be greatly appreciated as I believe understanding what we can now do between GF and ACF is the supreme way to turn Wordpress into a fully-custom CMS for those of us who aren't experts in PHP.

    Posted 12 years ago on Thursday March 15, 2012 | Permalink
  3. Hey Shawn! How good are you at the acf and gravity forms thing? If you are good I would like to hire you. Look at my site. http://hml.metrohealth.net/

    Email me at alex (at) greertechnologies (dot) com

    Posted 12 years ago on Thursday March 29, 2012 | Permalink

This topic has been resolved and has been closed to new replies.