-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-setting-api.php
executable file
·215 lines (201 loc) · 5.47 KB
/
wp-setting-api.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
<?php
/**
* Plugin Name: WP-Setting-API Demo
* Description: WP-Setting-API 的使用示例插件
* Author: 树新蜂团队
* Author URI: https://github.com/TheTNB
* Version: 1.0.1
* License: GPLv3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
defined( 'ABSPATH' ) or exit;
include_once 'class-setting.php';
use TheTNB\Setting as Setting;
$setting_api = new Setting;
$sections = array(
array(
'id' => 'demo',
'title' => __( '基础', 'demo' )
),
array(
'id' => 'demo2',
'title' => __( '高级', 'demo' )
),
array(
'id' => 'demo3',
'show_submit' => false,
'title' => __( '测试', 'demo' )
)
);
$fields = array(
'demo' => array(
array(
'name' => 'text_val',
'label' => __( 'Text Input', 'demo' ),
'desc' => __( 'Text input description', 'demo' ),
'placeholder' => __( 'Text Input placeholder', 'demo' ),
'type' => 'text',
'default' => 'Title',
'sanitize_callback' => 'sanitize_text_field'
),
array(
'name' => 'number_input',
'label' => __( 'Number Input', 'demo' ),
'desc' => __( 'Number field with validation callback `floatval`', 'demo' ),
'placeholder' => __( '1.99', 'demo' ),
'min' => 0,
'max' => 100,
'step' => '0.01',
'type' => 'number',
'default' => 'Title',
'sanitize_callback' => 'floatval'
),
array(
'name' => 'textarea',
'label' => __( 'Textarea Input', 'demo' ),
'desc' => __( 'Textarea description', 'demo' ),
'placeholder' => __( 'Textarea placeholder', 'demo' ),
'type' => 'textarea'
),
array(
'name' => 'html',
'label' => __( 'HTML Area', 'demo' ),
'type' => 'html',
'html' => '<h1>Hello World!</h1>'
),
array(
'name' => 'checkbox',
'label' => __( 'Checkbox', 'demo' ),
'desc' => __( 'Checkbox Label', 'demo' ),
'type' => 'checkbox'
),
array(
'name' => 'radio',
'label' => __( 'Radio Button', 'demo' ),
'desc' => __( 'A radio button', 'demo' ),
'type' => 'radio',
'options' => array(
'yes' => 'Yes',
'no' => 'No'
)
),
array(
'name' => 'selectbox',
'label' => __( 'A Dropdown', 'demo' ),
'desc' => __( 'Dropdown description', 'demo' ),
'type' => 'select',
'default' => 'no',
'options' => array(
'yes' => 'Yes',
'no' => 'No'
)
),
array(
'name' => 'password',
'label' => __( 'Password', 'demo' ),
'desc' => __( 'Password description', 'demo' ),
'type' => 'password',
'default' => ''
)
),
'demo2' => array(
array(
'name' => 'file',
'label' => __( 'File', 'demo' ),
'desc' => __( 'File description', 'demo' ),
'type' => 'file',
'default' => '',
'options' => array(
'button_label' => 'Choose Image'
)
),
array(
'name' => 'color',
'label' => __( 'Color', 'demo' ),
'desc' => __( 'Color description', 'demo' ),
'type' => 'color',
'default' => ''
),
array(
'name' => 'password',
'label' => __( 'Password', 'demo' ),
'desc' => __( 'Password description', 'demo' ),
'type' => 'password',
'default' => ''
),
array(
'name' => 'wysiwyg',
'label' => __( 'Advanced Editor', 'demo' ),
'desc' => __( 'WP_Editor description', 'demo' ),
'type' => 'wysiwyg',
'default' => ''
),
array(
'name' => 'multicheck',
'label' => __( 'Multile checkbox', 'demo' ),
'desc' => __( 'Multi checkbox description', 'demo' ),
'type' => 'multicheck',
'default' => array( 'one' => 'one', 'four' => 'four' ),
'options' => array(
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
'four' => 'Four'
)
),
),
'demo3' => array(
array(
'name' => 'html',
'label' => __( '无保存按钮', 'demo' ),
'type' => 'html',
'html' => '<h1>通过给页面设置show_submit = false属性,即可隐藏保存按钮</h1>'
)
)
);
/**
* 挂载设置字段 Demo
*
* @return void
*/
function load_setting_fields() {
global $setting_api;
global $sections;
global $fields;
$setting_api->set_sections( $sections );
$setting_api->set_fields( $fields );
$setting_api->admin_init();
}
/**
* 挂载设置页面 Demo
*/
function load_setting_page() {
add_options_page( esc_html__( 'Demo', 'demo' ), esc_html__( 'Demo', 'demo' ), 'manage_options', 'demo', 'setting_page' );
add_filter( 'plugin_action_links', function ( $links, $file ) {
if ( 'wp-setting-api/wp-setting-api.php' !== $file ) {
return $links;
}
$settings_link = '<a href="' . add_query_arg( array( 'page' => 'demo' ), admin_url( 'options-general.php' ) ) . '">' . esc_html__( '设置', 'demo' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}, 10, 2 );
}
/**
* 设置页面 Demo
*/
function setting_page() {
echo '<h1>WP-Setting-API Demo</h1><span style="float: right;">By: WordPressCN@耗子</span>';
echo '<div class="wrap">';
global $setting_api;
$setting_api->show_navigation();
$setting_api->show_forms();
echo '</div>';
}
add_action( 'admin_init', 'load_setting_fields' );
add_action( 'admin_menu', 'load_setting_page' );
/**
* 获取设置字段的值 Demo
*/
$demo_value['text_val'] = $setting_api->get_option( 'text_val', 'demo', '' );
$demo_value['textarea'] = $setting_api->get_option( 'textarea', 'demo', '' );
$demo_value['selectbox'] = $setting_api->get_option( 'selectbox', 'demo', '' );