Hi,
I am trying to create a form that gets the latitude and longitude from Google maps and saves it to a custom table in WordPress.
Currently i have created a the required fields and everything populates as expected.
I have then added a custom field type to gravity forms. This renders fine on the client side however doesn't appear to be working on the server side as it is not sending the values through to gravity forms... i would like the function to display as the name field displays in entries.
I am hoping you maybe able to let me know where i am going wrong
// Add a custom field button to the advanced to the field editor
add_filter( 'gform_add_field_buttons', 'add_loc_field' );
function add_loc_field( $field_groups ) {
foreach( $field_groups as &$group ){
//if( $group["name"] == "advanced_fields" ){ // to add to the Advanced Fields
//if( $group["name"] == "standard_fields" ){ // to add to the Standard Fields
if( $group["name"] == "post_fields" ){ // to add to the Standard Fields
$group["fields"][] = array(
"class"=>"button",
"value" => __("Add Location", "gravityforms"),
"onclick" => "StartAddField('add_loc');"
);
break;
}
}
return $field_groups;
}
// Adds title to GF custom field
add_filter( 'gform_field_type_title' , 'add_loc_title' );
function add_loc_title( $type ) {
if ( $type == 'add_loc' )
echo $field["type"];
return __( 'Add Location' , 'gravityforms' );
}
// Adds the input area to the external side
add_action( "gform_field_input" , "add_loc_field_input", 10, 5 );
function add_loc_field_input ( $input, $field, $value, $lead_id, $form_id ){
if ( $field["type"] == "add_loc" ) {
$value = array (
);
$max_chars = "";
if(!IS_ADMIN && !empty($field["maxLength"]) && is_numeric($field["maxLength"]))
$max_chars = self::get_counter_script($form_id, $field_id, $field["maxLength"]);
$input_name = $form_id .'_' . $field["id"];
$tabindex = GFCommon::get_tabindex();
//div start
$wrap_start = sprintf("<div id='gmaps_location_%s' class='ginput_complex ginput_container'>",$field["id"]);
//Location - Address
$location_address = sprintf("<input type='text' name='input_%d_address' id='%s_address'/><label for='add_loc_" . $field["id"] . "_address' id='add_loc_" . $field["id"] . "_address_label'>Enter Location</label>" , $field["id"], 'add_loc_'.$field['id'], $location_value, $disabled_text, $field_id);
//Submit Location
$sub_id = "add_loc_".$field['id']."_address";
$sub_location = "<input type=\"button\" onclick=\"findAddress(document.getElementById('{$sub_id}').value);\" value=\"Set Address on Map\" class=\"b_submit\">";
$location_submit = $sub_location;
//Map Canvas
$map_canvas = "<div id='map_canvas' style='width:500px; height:500px'></div>";
//Lat Position
$lat_position = sprintf("<input type='text' name='input_%d_lat' id='%s_lat' value='%F' $tabindex %s/><label for='add_loc_" . $field["id"] . "_lat' id='add_loc_" . $field["id"] . "_lat_label'>Latitude</label>", $field["id"], 'add_loc_'.$field['id'], esc_attr(stripslashes($geo_latitude)), $disabled_text, $field_id);
//Lon Position
$lon_position = sprintf("<input type='text' name='input_%d_lon' id='%s_lon' value='%F' $tabindex %s/><label for='add_loc_" . $field["id"] . "_lon' id='add_loc_" . $field["id"] . "_lon_label'>Longitude</label>", $field["id"], 'add_loc_'.$field['id'], esc_attr(stripslashes($geo_longitude)), $disabled_text, $field_id);
//end div
$wrap_end = "</div>";
$multifield = $wrap_start;
$multifield .= $location_address;
$multifield .= $location_submit;
$multifield .= $map_canvas;
$multifield .= $lat_position;
$multifield .= $lon_position;
$multifield .= print_r($value);
$multifield .= $wrap_end;
//$multifield = sprintf("<div class='ginput_container'><input type= 'text' name='input_%s_address' id='%s_address' class='textfield gform_add_loc %s' value='%s' /></div>", $field["id"], 'add_loc-'.$field['id'] , $field["type"] . ' ' . esc_attr($field['cssClass']) . ' ' . $field['size'] , esc_attr(stripslashes($value)));
return $multifield;
}
return $input;
}
// Now we execute some javascript technicalitites for the field to load correctly
add_action( "gform_editor_js", "wps_gform_editor_js" );
function wps_gform_editor_js(){
?>
<script type='text/javascript'>
jQuery(document).ready(function($) {
//Add all textarea settings to the "add_loc" field plus custom "add_loc_setting"
// fieldSettings["add_loc"] = fieldSettings["textarea"] + ", .add_loc_setting"; // this will show all fields that Paragraph Text field shows plus my custom setting
// from forms.js; can add custom "add_loc_setting" as well
fieldSettings["add_loc"] = ".label_setting, .description_setting, .admin_label_setting, .size_setting, .default_value_textarea_setting, .error_message_setting, .css_class_setting, .visibility_setting, .add_loc_setting"; //this will show all the fields of the Paragraph Text field minus a couple that I didn't want to appear.
//binding to the load field settings event to initialize the checkbox
$(document).bind("gform_load_field_settings", function(event, field, form){
jQuery("#field_add_loc").attr("checked", field["field_add_loc"] == true);
$("#field_add_loc_value").val(field["add_loc"]);
});
});
</script>
<?php
}
// Add a custom setting to the add_loc advanced field
add_action( "gform_field_advanced_settings" , "add_loc_settings" , 10, 2 );
function add_loc_settings( $position, $form_id ){
// Create settings on position 50 (right after Field Label)
if( $position == 50 ){
?>
<li class="add_loc_setting field_setting">
<input type="checkbox" id="field_add_loc" onclick="SetFieldProperty('field_add_loc', this.checked);" />
<label for="field_add_loc" class="inline">
<?php _e("Disable Submit Button", "gravityforms"); ?>
<?php gform_tooltip("form_field_add_loc"); ?>
</label>
</li>
<?php
}
}
//Filter to add a new tooltip
add_filter('gform_tooltips', 'wps_add_add_loc_tooltips');
function wps_add_add_loc_tooltips($tooltips){
$tooltips["form_field_add_loc"] = "<h6>Disable Submit Button</h6>Check the box if you would like to disable the submit button.";
$tooltips["form_field_default_value"] = "<h6>Default Value</h6>Enter the Add Location here.";
return $tooltips;
}
// Add a script to the display of the particular form only if add_loc field is being used
add_action( 'gform_enqueue_scripts' , 'wps_gform_enqueue_scripts' , 10 , 2 );
function wps_gform_enqueue_scripts( $form, $ajax ) {
// cycle through fields to see if add_loc is being used
foreach ( $form['fields'] as $field ) {
if ( $field['type'] == 'add_loc' ) {
wp_register_script( 'gmaps_api', 'http://maps.googleapis.com/maps/api/js?sensor=true');
wp_enqueue_script( 'gmaps_api' );
break;
}
}
}