-
Notifications
You must be signed in to change notification settings - Fork 0
/
003 Settings.php
271 lines (237 loc) · 7.49 KB
/
003 Settings.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
// SETTINGS API
/*--------------------------------------------------------------------------------------------------------------------*/
// options are stored in the: {$wpdb->prefix}_options table
// $table_prefix is set in wp-config.php
// The Settings API provides:
// 1. consistency
// 2. future proofing
// 3. less work
// 4. handles the form submissions
// 5. includes security measures (nonce and such)
// 6. sanitizes data
// Settings API functions:
register_setting();
unregister_setting();
add_settings_section();
add_settings_field();
settings_fields();
do_settings_sections();
do_settings_fields();
add_settings_error();
get_settings_errors();
settings_errors();
// First, add new setting:
register_setting($option_group, $option_name, $sanitize_callable='');
// then add a section:
add_settings_section($id, $title, $callback, $page);
add_settings_field($id, $title, $callback, $page, $section='', args=[]);
// Example:
function wporg_settings_init() {
// register a new setting for "reading" page
register_setting('reading', 'wporg_setting_name');
// register a new section in the "reading" page
add_settings_section(
'wporg_settings_section',
'WPOrg Settings Section', 'wporg_settings_section_callback',
'reading'
);
// register a new field in the "wporg_settings_section" section, inside the "reading" page
add_settings_field(
'wporg_settings_field',
'WPOrg Setting', 'wporg_settings_field_callback',
'reading',
'wporg_settings_section'
);
}
/**
* register wporg_settings_init to the admin_init action hook
*/
add_action('admin_init', 'wporg_settings_init');
/**
* callback functions
*/
// section content cb
function wporg_settings_section_callback() {
echo '<p>WPOrg Section Introduction.</p>';
}
// field content cb
function wporg_settings_field_callback() {
// get the value of the setting we've registered with register_setting()
$setting = get_option('wporg_setting_name');
// output the field
?>
<input type="text" name="wporg_setting_name" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>">
<?php
}
// Get Settings:
get_option(
$option='',
$default = false
);
// Get registered settings value
$setting = get_option('wporg_setting_name');
// OPTIONS API
/*--------------------------------------------------------------------------------------------------------------------*/
// options are stored in the: {$wpdb->prefix}_options table
// $table_prefix is set in wp-config.php
// single values are stored as single values
// add a new option
add_option('wporg_custom_option', 'hello world!');
// get an option
$option = get_option('wporg_custom_option');
// Arrays are stored as Arrays (go fucking figure!)
// array of options
$data_r = array('title' => 'hello world!', 1, false );
// add a new option
add_option('wporg_custom_option', $data_r);
// get an option
$options_r = get_option('wporg_custom_option');
// output the title
echo esc_html($options_r['title']);
// Functions Ref
// general option funcs
add_option();
get_option();
update_option();
delete_option();
// site specific funcs
add_site_option();
get_site_option();
update_site_option();
delete_site_option();
?>
// Big fat old example:
<?php
/**
* @internal never define functions inside callbacks.
* these functions could be run multiple times; this would result in a fatal error.
*/
/**
* custom option and settings
*/
function wporg_settings_init() {
// Register a new setting for "wporg" page.
register_setting( 'wporg', 'wporg_options' );
// Register a new section in the "wporg" page.
add_settings_section(
'wporg_section_developers',
__( 'The Matrix has you.', 'wporg' ), 'wporg_section_developers_callback',
'wporg'
);
// Register a new field in the "wporg_section_developers" section, inside the "wporg" page.
add_settings_field(
'wporg_field_pill', // As of WP 4.6 this value is used only internally.
// Use $args' label_for to populate the id inside the callback.
__( 'Pill', 'wporg' ),
'wporg_field_pill_cb',
'wporg',
'wporg_section_developers',
array(
'label_for' => 'wporg_field_pill',
'class' => 'wporg_row',
'wporg_custom_data' => 'custom',
)
);
}
/**
* Register our wporg_settings_init to the admin_init action hook.
*/
add_action( 'admin_init', 'wporg_settings_init' );
/**
* Custom option and settings:
* - callback functions
*/
/**
* Developers section callback function.
*
* @param array $args The settings array, defining title, id, callback.
*/
function wporg_section_developers_callback( $args ) {
?>
<p id="<?php echo esc_attr( $args['id'] ); ?>"><?php esc_html_e( 'Follow the white rabbit.', 'wporg' ); ?></p>
<?php
}
/**
* Pill field callbakc function.
*
* WordPress has magic interaction with the following keys: label_for, class.
* - the "label_for" key value is used for the "for" attribute of the <label>.
* - the "class" key value is used for the "class" attribute of the <tr> containing the field.
* Note: you can add custom key value pairs to be used inside your callbacks.
*
* @param array $args
*/
function wporg_field_pill_cb( $args ) {
// Get the value of the setting we've registered with register_setting()
$options = get_option( 'wporg_options' );
?>
<select
id="<?php echo esc_attr( $args['label_for'] ); ?>"
data-custom="<?php echo esc_attr( $args['wporg_custom_data'] ); ?>"
name="wporg_options[<?php echo esc_attr( $args['label_for'] ); ?>]">
<option value="red" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'red', false ) ) : ( '' ); ?>>
<?php esc_html_e( 'red pill', 'wporg' ); ?>
</option>
<option value="blue" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'blue', false ) ) : ( '' ); ?>>
<?php esc_html_e( 'blue pill', 'wporg' ); ?>
</option>
</select>
<p class="description">
<?php esc_html_e( 'You take the blue pill and the story ends. You wake in your bed and you believe whatever you want to believe.', 'wporg' ); ?>
</p>
<p class="description">
<?php esc_html_e( 'You take the red pill and you stay in Wonderland and I show you how deep the rabbit-hole goes.', 'wporg' ); ?>
</p>
<?php
}
/**
* Add the top level menu page.
*/
function wporg_options_page() {
add_menu_page(
'WPOrg',
'WPOrg Options',
'manage_options',
'wporg',
'wporg_options_page_html'
);
}
/**
* Register our wporg_options_page to the admin_menu action hook.
*/
add_action( 'admin_menu', 'wporg_options_page' );
/**
* Top level menu callback function
*/
function wporg_options_page_html() {
// check user capabilities
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// add error/update messages
// check if the user have submitted the settings
// WordPress will add the "settings-updated" $_GET parameter to the url
if ( isset( $_GET['settings-updated'] ) ) {
// add settings saved message with the class of "updated"
add_settings_error( 'wporg_messages', 'wporg_message', __( 'Settings Saved', 'wporg' ), 'updated' );
}
// show error/update messages
settings_errors( 'wporg_messages' );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
// output security fields for the registered setting "wporg"
settings_fields( 'wporg' );
// output setting sections and their fields
// (sections are registered for "wporg", each field is registered to a specific section)
do_settings_sections( 'wporg' );
// output save settings button
submit_button( 'Save Settings' );
?>
</form>
</div>
<?php
}