Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Save the country subfield of a Gravity Forms address as the country code.
<?php
/*
Plugin Name: GF Country as Code
Plugin URI: https://gist.github.com/webaware/c5b624416ad4a5641daab54ba9c91a0a
Description: save the country subfield of an address as the country code
Version: 1
Author: WebAware
Author URI: https://shop.webaware.com.au/
*/
/*
copyright (c) 2020 WebAware Pty Ltd (email : support@webaware.com.au)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace webaware\examples\gravityforms;
if (!defined('ABSPATH')) {
exit;
}
add_filter('gform_entry_post_save', __NAMESPACE__ . '\\save_country_as_code', 20, 2);
/**
* @param array $entry
* @param array $form
* @return array
*/
function save_country_as_code($entry, $form) {
foreach ($form['fields'] as $field) {
if ($field->type === 'address') {
$country_name = rgar($entry, "{$field->id}.6");
$country_code = $country_name ? $field->get_country_code($country_name) : false;
if ($country_code) {
$entry["{$field->id}.6"] = $country_code;
\GFAPI::update_entry($entry);
}
}
}
return $entry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment