Create a Custom Field

Create a Custom Field

IntroductionExamples

Introduction
When using the Settings API you can define a custom field type by creating a function called settings_{your_custom_field_type}. The text after the first underscore will be what you decided to name your type. The function will run for every field of that type.

Important: The Settings API is not used to create custom form fields. To create custom form fields you would need to extend the GF_Field class, part of the Field Framework.

Examples
The following example renders the field for the field type my_custom_field_type. This function creates two text box fields.:
public function plugin_settings_fields() {
return
array(
array(
'title' => 'This is the title for Section 1',
'description' => 'This is a description of the purpose of Section 1',
'fields' => array(
array(
'label' => 'My Custom Field',
'type' => 'my_custom_field_type',
'name' => 'my_custom_field'
),
)
),
);
}

public function settings_my_custom_field_type(){
?>

My custom field contains a few settings:

settings_text(
array(
'label' => 'Item 1',
'name' => 'my_custom[1]',
'default_value' => 'Item 1'
)
);
$this->settings_text(
array(
'label' => 'Item 2',
'name' => 'my_custom[2]',
'default_value' => 'Item 2'
)
);
}

The following example has a custom field type named 「text_with_checkbox」. This custom type creates a text box field and a checkbox field.
public function plugin_settings_fields() {
return
array(
array(
'title' => 'Custom Types',
'fields' => array(
array(
'type' => 'text_with_checkbox',
'name' => 'text_checkbox',
'label' => 'Text box with a checkbox',
),
),
),
)
}

public function settings_text_with_checkbox(){
?>

This is a custom field type which will create a text box and a check box

settings_text(
array(
'label' => 'Text Box With Checkbox',
'name' => 'txt_cbx[1]',
'default_value' => 'Item 1'
)
);

$this->settings_checkbox(
array(
'label' => 'Checkbox',
'choices' => array(
array(
'label' => 'Checkbox',
'name' => 'txt_cbx[2]',
),
),
)
);
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注