-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.php
231 lines (227 loc) · 8.6 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* Custom Settings example by using WP Settings API Wrapper.
*
* @package wp-custom-settings
*/
use WP_Custom_Settings\WP_Custom_Settings;
use WP_Custom_Settings\WP_Custom_Settings_Section;
use WP_Custom_Settings\WP_Custom_Settings_Field;
/**
* An example of adding a custom settings page.
*
* @return void
*/
function wp_register_custom_settings() {
$custom_settings = new WP_Custom_Settings(
// Arguments to add menu page. Following arguments are same as add_menu_page() function arguments.
// Callback argument does not needed.
[
'page_title' => __( 'Custom Settings', 'wp-custom-settings' ),
'menu_title' => __( 'Custom Settings', 'wp-custom-settings' ),
'capability' => 'manage_options',
'menu_slug' => 'wp-custom-settings-page',
'icon_url' => '',
'position' => null,
],
// Arguments to register setting. Following arguments are same as register_setting() function arguments.
[
'option_group' => 'wp_custom_settings_group',
'option_name' => 'wp_custom_settings_options',
'args' => array(
'type' => 'array',
'description' => 'Description of Custom Settings.',
'show_in_rest' => true,
'default' => array(),
'sanitize_callback' => null,
),
],
// Arguments to add sections and fields.
[
new WP_Custom_Settings_Section(
'wp_custom_settings_form_elements_section', // ID.
__( 'HTML Form Elements', 'wp-custom-settings' ), // Title.
__( 'All the HTML form elements.', 'wp-custom-settings' ), // Description.
[
new WP_Custom_Settings_Field(
'text', // Field type.
'wp_custom_settings_text_field', // ID. Also, it will used for "name" attribute.
__( 'Text Input', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of text field.',
'label_for' => 'wp_custom_settings_text_field',
'class' => 'regular-text',
]
),
new WP_Custom_Settings_Field(
'select', // Field type.
'wp_custom_settings_select_field', // ID. Also, it will used for "name" attribute.
__( 'Select Input', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'options' => [
'' => 'Select Option',
'option-1' => 'Option 1',
'option-2' => 'Option 2',
'option-3' => 'Option 3',
],
'description' => 'Description of select field.',
'label_for' => 'wp_custom_settings_select_field',
'class' => 'regular-text',
]
),
new WP_Custom_Settings_Field(
'textarea',
'wp_custom_settings_textarea_field',
__( 'Textarea Input', 'wp-custom-settings' ),
[
'description' => 'Description of textarea field.',
'label_for' => 'wp_custom_settings_textarea_field',
'class' => 'regular-text',
]
),
]
),
new WP_Custom_Settings_Section(
'wp_custom_settings_input_types_section',
__( 'Input Types.', 'wp-custom-settings' ),
__( 'All the input types.', 'wp-custom-settings' ),
[
new WP_Custom_Settings_Field(
'checkbox', // Field type.
'wp_custom_settings_checkbox_field', // ID. Also, it will used for "name" attribute.
__( 'Checkbox Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'value' => '1',
'label' => 'Checkbox label',
'description' => 'Description of checkbox input.',
]
),
new WP_Custom_Settings_Field(
'radio', // Field type.
'wp_custom_settings_radio_field', // ID. Also, it will used for "name" attribute.
__( 'Radio Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'options' => [
'yes' => 'Yes',
'no' => 'No',
],
'description' => 'Description of radio field.',
'label_for' => 'wp_custom_settings_radio_field',
]
),
new WP_Custom_Settings_Field(
'password', // Field type.
'wp_custom_settings_password_field', // ID. Also, it will used for "name" attribute.
__( 'Password Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of password input type.',
'label_for' => 'wp_custom_settings_password_field',
'class' => 'regular-text',
]
),
new WP_Custom_Settings_Field(
'email', // Field type.
'wp_custom_settings_email_field', // ID. Also, it will used for "name" attribute.
__( 'Email Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of email input type.',
'label_for' => 'wp_custom_settings_email_field',
'class' => 'regular-text',
]
),
new WP_Custom_Settings_Field(
'url', // Field type.
'wp_custom_settings_url_field', // ID. Also, it will used for "name" attribute.
__( 'URL Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of url input type.',
'label_for' => 'wp_custom_settings_url_field',
'class' => 'regular-text',
]
),
new WP_Custom_Settings_Field(
'tel', // Field type.
'wp_custom_settings_tel_field', // ID. Also, it will used for "name" attribute.
__( 'Tel Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of Tel input type.',
'label_for' => 'wp_custom_settings_tel_field',
'class' => 'regular-text',
]
),
new WP_Custom_Settings_Field(
'number', // Field type.
'wp_custom_settings_number_field', // ID. Also, it will used for "name" attribute.
__( 'Number Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of number input type.',
'label_for' => 'wp_custom_settings_number_field',
'class' => 'small-text',
]
),
new WP_Custom_Settings_Field(
'color', // Field type.
'wp_custom_settings_color_field', // ID. Also, it will used for "name" attribute.
__( 'Color Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of color input type.',
'label_for' => 'wp_custom_settings_color_field',
'class' => 'small-text',
]
),
new WP_Custom_Settings_Field(
'date', // Field type.
'wp_custom_settings_date_field', // ID. Also, it will used for "name" attribute.
__( 'Date Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of date input type.',
'label_for' => 'wp_custom_settings_date_field',
'class' => 'regualr-text',
]
),
new WP_Custom_Settings_Field(
'datetime-local', // Field type.
'wp_custom_settings_datetime_local_field', // ID. Also, it will used for "name" attribute.
__( 'Datetime-local Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of Datetime-local input type.',
'label_for' => 'wp_custom_settings_datetime_local_field',
'class' => 'regular-text',
]
),
new WP_Custom_Settings_Field(
'month', // Field type.
'wp_custom_settings_month_field', // ID. Also, it will used for "name" attribute.
__( 'Month Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of month input type.',
'label_for' => 'wp_custom_settings_month_field',
'class' => 'regualr-text',
]
),
new WP_Custom_Settings_Field(
'week', // Field type.
'wp_custom_settings_week_field', // ID. Also, it will used for "name" attribute.
__( 'Week Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of week input type.',
'label_for' => 'wp_custom_settings_week_field',
'class' => 'regualr-text',
]
),
new WP_Custom_Settings_Field(
'time', // Field type.
'wp_custom_settings_time_field', // ID. Also, it will used for "name" attribute.
__( 'Time Input Type', 'wp-custom-settings' ), // Title.
[ // Pass additional arguments.
'description' => 'Description of time input type.',
'label_for' => 'wp_custom_settings_time_field',
'class' => 'regualr-text',
]
),
]
),
]
);
}
add_action( 'init', 'wp_register_custom_settings' );