Last active
May 23, 2018 13:38
-
-
Save emilysnothere/e67957c6975dcbb1a1d1 to your computer and use it in GitHub Desktop.
Gravity Form Custom Hidden Fields
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 | |
class My_Custom_Field extends \GF_Field | |
{ | |
public $type = 'my_custom_field'; | |
public function get_form_editor_button() | |
{ | |
return array( | |
'group' => 'advanced_fields', | |
'text' => __('My Custom Field', 'gravityforms') | |
); | |
} | |
public function get_form_editor_field_title() | |
{ | |
return __('My Custom Fields', 'gravityforms'); | |
} | |
public function get_form_editor_field_settings() | |
{ | |
return array( | |
'label_setting' | |
); | |
} | |
public function get_field_content($value, $force_frontend_label, $form) | |
{ | |
$form_id = $form['id']; | |
$admin_buttons = $this->get_admin_buttons(); | |
$field_label = $this->get_field_label($force_frontend_label, $value); | |
$field_id = is_admin() || $form_id == 0 ? "input_{$this->id}" : 'input_' . $form_id . "_{$this->id}"; | |
$field_content = !is_admin() ? '{FIELD}' : $field_content = sprintf("%s<label class='gfield_label' for='%s'>%s</label>{FIELD}", $admin_buttons, $field_id, esc_html($field_label)); | |
return $field_content; | |
} | |
public function get_value_entry_detail($value, $currency = '', $use_text = false, $format = 'html', $media = 'screen') | |
{ | |
if (is_array($value)) { | |
$items = ''; | |
foreach ($value as $key => $item) { | |
if (!empty($item)) { | |
switch ($format) { | |
case 'text' : | |
$items .= \GFCommon::selection_display($item, $this, $currency, $use_text) . ', '; | |
break; | |
default: | |
$items .= '<li>' . \GFCommon::selection_display( $item, $this, $currency, $use_text ) . '</li>'; | |
break; | |
} | |
} | |
} | |
if (empty($items)) { | |
return ''; | |
} elseif ($format == 'text') { | |
return substr($items, 0, strlen( $items ) - 2); | |
} else { | |
return "<ul class='bulleted'>$items</ul>"; | |
} | |
} else { | |
return $value; | |
} | |
} | |
} |
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 | |
add_filter('gform_field_input', 'render_fields', 10, 5); | |
function render_fields($input, $field, $value, $entry_id, $form_id) | |
{ | |
if ($field->type == 'my_custom_field') { | |
$input = '<div class="ginput_complex ginput_container">'; | |
$i = 1; | |
foreach (custom_fields() as $f) { | |
$input .= sprintf('<input type="hidden" name="input_%1$s.%2$s" class="%3$s gform_hidden" id="input_%1$s_%4$s_%2$s" value="%5$s" />', | |
$field['id'], | |
$i, | |
esc_attr($f), | |
$form_id, | |
$value[$field['id'].'.'.$i] | |
); | |
$i ++; | |
if ($i % 10 == 0) { | |
$i++; | |
} | |
} | |
$input .= '</div>'; | |
} | |
return $input; | |
} |
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 | |
add_action('gform_editor_js_set_default_values', set_default_values); | |
function set_default_values() | |
{ | |
?> | |
case 'my_custom_field' : | |
field.label = '<?php _e('My Custom Hidden Fields', 'gravityforms'); ?>'; | |
field.inputs = [ | |
<?php | |
$i = 1; | |
foreach (custom_fields() as $f) { ?> | |
new Input(field.id + 0.<?php echo $i; ?>, '<?php echo esc_js(__($f, 'gravityforms')); ?>'), | |
<?php | |
$i++; | |
if ($i % 10 == 0) { | |
$i++; | |
} | |
} ?> | |
]; | |
break; | |
<?php | |
} |
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 | |
add_filter('gform_add_field_buttons', add_field); | |
function add_field() | |
{ | |
foreach ($field_group as &$group) { | |
if ($group['name'] == 'advanced_fields') { | |
$group['fields'][] = array ( | |
'class' => 'button', | |
'value' => __('My Custom Field', 'gravityforms'), | |
'onclick' => "StartAddField('my_custom_field');", | |
'data-type' => 'my_custom_field' | |
); | |
break; | |
} | |
} | |
return $field_group; | |
} | |
add_filter('gform_field_type_title', add_field_title, 10, 2); | |
funciton add_field_title() | |
{ | |
if ($field_type == 'my_custom_field') { | |
$title = __('My Custom Hidden Fields', 'gravityforms'); | |
} | |
return $title; | |
} |
HI, i did copy your every file but it's not working
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's a funciton?