-
Notifications
You must be signed in to change notification settings - Fork 25
/
plugin-example.php
324 lines (297 loc) · 8.71 KB
/
plugin-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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/*
Plugin Name: Example Plugin (Sunrise)
Plugin URI: http://anovladimir.me/sunrise/
Version: 7.0.0
Description: Options Pages Framework
Author: Vladimir Anokhin
Author URI: http://anovladimir.me/
Text Domain: plugin-example
Domain Path: /languages
License: MIT
*/
require_once 'sunrise.php';
/**
* Initialize example plugin
*/
function plugin_example_init() {
// Make plugin available for translation, you can change /languages/ to your .mo-files folder name
load_plugin_textdomain( 'plugin-example', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// Initialize Sunrise
$admin = new Sunrise7( array(
// Sunrise file path
'file' => __FILE__,
// Plugin slug (should be equal to plugin directory name)
'slug' => 'plugin-example',
// Plugin prefix
'prefix' => 'plugin_example_',
// Plugin textdomain
'textdomain' => 'plugin-example',
// Custom CSS assets folder
'css' => '',
// Custom JS assets folder
'js' => '',
) );
// Prepare array with options
$options = array(
// Open tab: Regular fields
array(
'type' => 'opentab',
'name' => __( 'Regular fields', 'plugin-example' ),
),
// Text field
array(
'id' => 'text_field_id',
'type' => 'text',
'default' => 'Default value',
'name' => __( 'Text field', 'plugin-example' ),
'desc' => __( 'Text field description', 'plugin-example' ),
),
// Textarea
array(
'id' => 'textarea_id',
'type' => 'textarea',
'default' => 'Default value',
'rows' => 10,
'name' => __( 'Textarea', 'plugin-example' ),
'desc' => __( 'Textarea description', 'plugin-example' ),
),
// Checkbox
array(
'id' => 'checkbox_id',
'type' => 'checkbox',
'default' => 'on',
'name' => __( 'Checkbox', 'plugin-example' ),
'desc' => __( 'Checkbox description', 'plugin-example' ),
'label' => __( 'Enabled', 'plugin-example' ),
),
// Select (dropdown list)
array(
'id' => 'select_id',
'type' => 'select',
'default' => 'option-1',
'name' => __( 'Select', 'plugin-example' ),
'desc' => __( 'Select description', 'plugin-example' ),
'options' => array(
array(
'value' => 'option-1',
'label' => __( 'Option 1', 'plugin-example' ),
),
array(
'value' => 'option-2',
'label' => __( 'Option 2', 'plugin-example' ),
),
array(
'value' => 'option-3',
'label' => __( 'Option 3', 'plugin-example' ),
),
),
),
// Multi-select (dropdown list with multiple choices)
array(
'id' => 'multi_select_id',
'type' => 'select',
'default' => array( 'option-1', 'option-2' ),
'name' => __( 'Multi-select', 'plugin-example' ),
'desc' => __( 'Multi-select description', 'plugin-example' ),
'options' => array(
array(
'value' => 'option-1',
'label' => __( 'Option 1', 'plugin-example' ),
),
array(
'value' => 'option-2',
'label' => __( 'Option 2', 'plugin-example' ),
),
array(
'value' => 'option-3',
'label' => __( 'Option 3', 'plugin-example' ),
)
),
'multiple' => true,
'size' => 4,
),
// Radio buttons
array(
'id' => 'radio_id',
'type' => 'radio',
'default' => 'option-1',
'name' => __( 'Radio', 'plugin-example' ),
'desc' => __( 'Radio description', 'plugin-example' ),
'options' => array(
array(
'value' => 'option-1',
'label' => __( 'Option 1', 'plugin-example' ),
),
array(
'value' => 'option-2',
'label' => __( 'Option 2', 'plugin-example' ),
),
array(
'value' => 'option-3',
'label' => __( 'Option 3', 'plugin-example' ),
)
)
),
// Number picker
array(
'id' => 'number_field_id',
'type' => 'number',
'default' => 10,
'name' => __( 'Number', 'plugin-example' ),
'desc' => __( 'Number field description', 'plugin-example' ),
'min' => 0,
'max' => 20,
'step' => 1,
),
// Close tab: Regular fields
array(
'type' => 'closetab',
),
// Open tab: Extra fields
array(
'type' => 'opentab',
'name' => __( 'Extra fields', 'plugin-example' ),
),
// Media (text field with Media library button)
array(
'id' => 'media_field_id',
'type' => 'media',
'default' => '',
'name' => __( 'Media', 'plugin-example' ),
'desc' => __( 'Media field description', 'plugin-example' ),
),
// Color picker
array(
'id' => 'color_field_id',
'type' => 'color',
'default' => '#0099ff',
'name' => __( 'Color', 'plugin-example' ),
'desc' => __( 'Color field description', 'plugin-example' ),
),
// Size picker
array(
'id' => 'size_field_id',
'type' => 'size',
'default' => array( 0 => '20', 1 => 'px' ),
'name' => __( 'Size', 'plugin-example' ),
'desc' => __( 'Size field description', 'plugin-example' ),
'units' => array( 'px', 'em', '%' ),
'min' => 0,
'max' => 200,
'step' => 10,
),
// Checkbox group
array(
'id' => 'checkbox_group_id',
'type' => 'checkbox_group',
'default' => array(
'checkbox-1' => 'on',
'checkbox-2' => 'on',
),
'name' => __( 'Checkbox group', 'plugin-example' ),
'desc' => __( 'Checkbox group description', 'plugin-example' ),
'options' => array(
array(
'id' => 'checkbox-1',
'label' => __( 'Checkbox 1', 'plugin-example' ),
),
array(
'id' => 'checkbox-2',
'label' => __( 'Checkbox 2', 'plugin-example' ),
),
array(
'id' => 'checkbox-3',
'label' => __( 'Checkbox 3', 'plugin-example' ),
)
),
'multiple' => true,
'size' => 4,
),
// Custom HTML content
array(
'type' => 'html',
'content' => '<h3>HTML field type</h3><p>Paragraph tag</p>',
),
// Custom title
array(
'type' => 'title',
'name' => __( 'Title field', 'plugin-example' ),
),
// Image radio
array(
'id' => 'image_radio_id',
'type' => 'image_radio',
'default' => 'option-1',
'name' => __( 'Image radio', 'plugin-example' ),
'desc' => __( 'Image radio description', 'plugin-example' ),
'options' => array(
array(
'value' => 'option-1',
'label' => __( 'Option 1', 'plugin-example' ),
'image' => 'http://lorempixel.com/120/90/food/1/',
),
array(
'value' => 'option-2',
'label' => __( 'Option 2', 'plugin-example' ),
'image' => 'http://lorempixel.com/120/90/food/2/',
),
array(
'value' => 'option-3',
'label' => __( 'Option 3', 'plugin-example' ),
'image' => 'http://lorempixel.com/120/90/food/3/',
),
),
),
// Close tab: Extra fields
array(
'type' => 'closetab',
)
);
// Add top-level menu (like Dashboard -> Comments)
$admin->add_menu( array(
// Settings page <title>
'page_title' => __( 'Example Plugin Settings', 'plugin-example' ),
// Menu title, will be shown in left dashboard menu
'menu_title' => __( 'Example Plugin', 'plugin-example' ),
// Minimal user capability to access this page
'capability' => 'manage_options',
// Unique page slug
'slug' => 'plugin-example-settings',
// Add here your custom icon url, or use [dashicons](https://developer.wordpress.org/resource/dashicons/)
// 'icon_url' => admin_url( 'images/wp-logo.png' ),
'icon_url' => 'dashicons-wordpress',
// Menu position from 80 to <infinity>, you can use decimals
'position' => '91.1',
// Array with options available on this page
'options' => $options,
) );
// Add sub-menu (like Dashboard -> Settings -> Permalinks)
$admin->add_submenu( array(
// Settings page <title>
'page_title' => __( 'Example Plugin Settings', 'plugin-example' ),
// Menu title, will be shown in left dashboard menu
'menu_title' => __( 'Page 2', 'plugin-example' ),
// Unique page slug, you can use here the slug of parent page, which you've already created
'slug' => 'plugin-example-settings-2',
// Slug of the parent page (see above)
'parent_slug' => 'plugin-example-settings',
// Array with options available on this page
'options' => $options,
) );
// Add another sub-menu (like Dashboard -> Settings -> Permalinks)
$admin->add_submenu( array(
// Settings page <title>
'page_title' => __( 'Example Plugin Settings', 'plugin-example' ),
// Menu title, will be shown in left dashboard menu
'menu_title' => __( 'Page 3', 'plugin-example' ),
// Unique page slug, you can use here the slug of parent page, which you've already created
'slug' => 'plugin-example-settings-3',
// Slug of the parent page (see above)
'parent_slug' => 'plugin-example-settings',
// Array with options available on this page
'options' => $options,
) );
}
// Hook to plugins_loaded
add_action( 'plugins_loaded', 'plugin_example_init' );