Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PogHallam/8ea1cc556844ff21ac43cd66363d96de to your computer and use it in GitHub Desktop.
Save PogHallam/8ea1cc556844ff21ac43cd66363d96de to your computer and use it in GitHub Desktop.
function registration_form_add_country_field() {
// Only show if secret arg is in URL.
if ( empty( $_GET['add_field'] ) || $_GET['add_field'] != 1 ) {
return;
}
$form = GFAPI::get_form( TEST_FORM_ID );
$new_field_id = 0;
foreach ( $form['fields'] as $field ) {
if ( $field->id > $new_field_id ) {
$new_field_id = $field->id;
}
}
$countries_array = [
'Afghanistan' => 'AF',
'Åland Islands' => 'AX',
'Albania' => 'AL',
'Algeria' => 'DZ',
'Andorra' => 'AD'
];
$choices = [];
foreach ( $countries_array as $name => $code ) {
$choices[] = [
'text' => $name,
'value' => $code
];
}
$new_field_id ++;
$field = GF_Fields::get( 'select' );
$field->id = $new_field_id;
$field->name = 'countries';
$field->label = 'Country Yeeee Haaaawwww!';
$field->placeholder = 'Select a country';
$field->choices = $choices;
$form['fields'][] = $field;
GFAPI::update_form( $form );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment