Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Advanced custom fields and gravity forms sync WordPress

  • SOLVED

Hey all,

I'm using two plugins advanced custom fields and gravity forms.

The advanced custom fields plugin is used for collecting data from wp-admin and gravity forms is used so that visitors to my site can submit data from the front-end.

All works really well and advanced custom fields post the data it collects back to the standard WordPress custom fields... however... when a user submits a gravity form from the front end the data is only added to the standard WordPress custom fields, but I need some way for this to be pushed back to the advanced custom fields too.

Hope that all makes sense. I've tried to come up with a solution to this myself but don't have any programming knowledge so am a bit lost.

Any help here is greatly appreciated.

Answers (5)

2011-08-30

Jurre Hanema answers:

If you want to stick to ACF, you can try adding this code to your functions.php:


add_action('gform_pre_submission', 'wpq_register_added_meta_hook');
add_action('gform_post_submission', 'wpq_remove_added_meta_hook');

function wpq_register_added_meta_hook()
{
add_action('added_post_meta', 'wpq_added_post_meta', 10, 4);
}


function wpq_remove_added_meta_hook()
{
remove_action('added_post_meta', 'wpq_added_post_meta', 10, 4);
}


function wpq_added_post_meta($meta_id, $object_id, $meta_key, $meta_value)
{
global $wpdb;

if(!class_exists('Acf'))
return;

$query = $wpdb->prepare("SELECT id, save_as_cf FROM {$wpdb->prefix}acf_fields WHERE name = %s", $meta_key);
$acf_field = $wpdb->get_row($query);

if($acf_field)
{
$query = $wpdb->prepare("INSERT INTO {$wpdb->prefix}acf_values (field_id, value, post_id) VALUES (%d, %s, %d)", $acf_field->id, $meta_value, $object_id);
$wpdb->query($query);
}
}


It's untested, but I have feeling it might "just work". The code I use to insert the data into ACF comes from a plugin I wrote a little while ago which had to import custom data from a CSV-file into ACF.


Dale Williams comments:

Thanks for the reply, just gave this a go, but unfortunately it didn't work


Jurre Hanema comments:

I just tested the code and it should definitely work. Unfortunately, there is no easy way for me to see what goes wrong in your case.

Have you made sure that the "Field name" you have set in the ACF-plugin exactly matches the "Custom field name" you set in Gravity Forms?


Dale Williams comments:

Yeah, all matching. Very strange. Which versions of ACF and Gravity Forms are you using? (I'm on 2.1.1 for ACF and 1.5.2.8 for Gravity Forms)


Jurre Hanema comments:

Turns out I was using an older version of ACF. I modified the code a bit and tested it again of ACF 2.1.2. Try this updated code:


add_action('gform_pre_submission', 'wpq_register_added_meta_hook');
add_action('gform_post_submission', 'wpq_remove_added_meta_hook');

function wpq_register_added_meta_hook()
{
add_action('added_post_meta', 'wpq_added_post_meta', 10, 4);
}


function wpq_remove_added_meta_hook()
{
remove_action('added_post_meta', 'wpq_added_post_meta', 10, 4);
}


function wpq_added_post_meta($meta_id, $object_id, $meta_key, $meta_value)
{
global $wpdb;

if(!class_exists('Acf'))
return;

$query = $wpdb->prepare("SELECT id, save_as_cf FROM {$wpdb->prefix}acf_fields WHERE name = %s", $meta_key);
$acf_field = $wpdb->get_row($query);

if($acf_field)
{
$query = $wpdb->prepare("INSERT INTO {$wpdb->prefix}acf_values (field_id, value, post_id) VALUES (%d, %d, %d)", $acf_field->id, $meta_id, $object_id);
$wpdb->query($query);
}
}


If this doesn't work, I'm stumped!


Dale Williams comments:

Nope, still not working. Am I supposed to change anything in the code?


Dale Williams comments:

oddly it's working on one install, but as far as I can tell the config is exactly the same. Just wondering if the database upgrade in the new version of ACF would have changed anything?

I believe the one install it's working on was upgraded within WordPress and the ones which aren't working were fresh installs of ACF without any need for upgrading.


Dale Williams comments:

Managed to find this in the error log, not sure if it helps in any way:

[01-Sep-2011 20:57:08] WordPress database error Unknown column 'save_as_cf' in 'field list' for query SELECT id, save_as_cf FROM wp_acf_fields WHERE name = 'guidewebsite' made by require, wp, WP->main, do_action_ref_array, call_user_func_array, RGForms->maybe_process_form, GFFormDisplay->process_form, GFFormDisplay->handle_submission, RGFormsModel->create_post, add_post_meta, add_metadata, do_action, call_user_func_array, wpq_added_post_meta


Dale Williams comments:

After exchanging a few more messages with Jurre we now have this working. For anyone else who wants to do the same thing (with a clean install of ACF and WordPress) try the following code:


add_action('gform_pre_submission', 'wpq_register_added_meta_hook');
add_action('gform_post_submission', 'wpq_remove_added_meta_hook');
function wpq_register_added_meta_hook()
{
add_action('added_post_meta', 'wpq_added_post_meta', 10, 4);
}
function wpq_remove_added_meta_hook()
{
remove_action('added_post_meta', 'wpq_added_post_meta', 10, 4);
}
function wpq_added_post_meta($meta_id, $object_id, $meta_key, $meta_value)
{
global $wpdb;
if(!class_exists('Acf'))
return;
$query = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}acf_fields WHERE name = %s", $meta_key);
$acf_field = $wpdb->get_row($query);

if($acf_field)
{
$query = $wpdb->prepare("INSERT INTO {$wpdb->prefix}acf_values (field_id, value, post_id) VALUES (%d, %d, %d)", $acf_field->id, $meta_id, $object_id);
$wpdb->query($query);
}
}


The issue was that the ACF database is structured slightly differently on a clean install than it is on an updated (from a pervious) version.

2011-08-29

Utkarsh Kukreti answers:

How have you created the field using "Advanced custom fields" plugin?


Dale Williams comments:

Sorry was supposed to post a link, the plugin is by Elliot Condon, and can be found here: http://plugins.elliotcondon.com/advanced-custom-fields/

2011-08-29

Dylan Kuhn answers:

I would hope that if you make "Field Name" in the Advanced Custom Fields plugin match "Custom Field Name" in gravity forms, both would use the same data.

2011-08-29

Hai Bui answers:

It's hard to solve because Advanced Custom Fields saves the data in custom db tables.
The best solution is replacing "Advanced Custom Fields" with custom code. You can follow this tutorial http://wefunction.com/2009/10/revisited-creating-custom-write-panels-in-wordpress/


Hai Bui comments:

Another solution is replacing "Advanced Custom Fields" with a similar plugin - "Magic Fields", you just have to use same custom field names.

2011-08-29

Sascha answers:

HI Dale,

I got frustrated with that problem too and decided to replace ACF with the 'More Fields' plugin. Try it out. It works great.
Hope you get there,
Sascha