Last active
May 29, 2020 04:28
-
-
Save webaware/c5b624416ad4a5641daab54ba9c91a0a to your computer and use it in GitHub Desktop.
Save the country subfield of a Gravity Forms address as the country code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 : [email protected]) | |
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