When you build the form using ACF (Advanced Custom Field plugin) you’d probably want to use ACF field properties you created in the ACF admin page like this:
Honestly I tried to Google solution but haven’t found no suitable ACF helper function. If you have more elegant solution please write me some comment.
So I decided to make my own helper my solution is to create a simple function in the functions.php of your theme like this:
/**
* Retrieve all ACF field options
**/
function get_field_options($field_name)
{
global $wpdb;
$field_key_query = "SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key = '_" . $field_name . "' ORDER BY meta_value ASC LIMIT 1";
$res = $wpdb->get_results($field_key_query);
$field_key = $res[0]->meta_value;
$field_object_settings_query = "SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key = '" . $field_key . "' ORDER BY meta_value ASC LIMIT 1";
$res = $wpdb->get_results($field_object_settings_query);
return unserialize($res[0]->meta_value);
}
/**
* Get only default value of the ACF field
**/
function get_field_default_value($field_name) {
$options = get_field_options($field_name);
return $options['default_value'];
}
/**
* Get only placeholder value of the ACF field
**/
function get_field_placeholder($field_name) {
$options = get_field_options($field_name);
return $options['placeholder'];
}
Simple as this.
In your theme you can add form fields like this:
Of course we can simplify SQL and make 1 query instead of 2 and use $wpdb->wp_postmeta instead of hardcoded table name but I leave it up to you 🙂
Enjoy and put some comments if you have something to add.