-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.php
More file actions
executable file
·139 lines (133 loc) · 5.15 KB
/
settings.php
File metadata and controls
executable file
·139 lines (133 loc) · 5.15 KB
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
<?php
if(!class_exists('WP_Plugin_Template_Settings'))
{
class WP_Plugin_Template_Settings
{
/**
* Construct the plugin object
*/
public function __construct()
{
// register actions
add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_menu', array(&$this, 'add_menu'));
} // END public function __construct
/**
* hook into WP's admin_init action hook
*/
public function admin_init()
{
// register your plugin's settings
register_setting('wp_plugin_template-group', 'setting_a');
register_setting('wp_plugin_template-group', 'setting_b');
register_setting('wp_plugin_template-group', 'setting_c');
// add your settings section
add_settings_section(
'wp_plugin_template-section',
'WP Plugin Template Settings123',
array(&$this, 'settings_section_wp_plugin_template'),
'wp_plugin_template'
);
// add your setting's fields
add_settings_field(
'wp_plugin_template-setting_a',
'Setting A',
array(&$this, 'settings_field_input_text'),
'wp_plugin_template',
'wp_plugin_template-section',
array(
'field' => 'setting_a'
)
);
add_settings_field(
'wp_plugin_template-setting_b',
'Setting B',
array(&$this, 'settings_field_input_textarea'),
'wp_plugin_template',
'wp_plugin_template-section',
array(
'field' => 'setting_b'
)
);
add_settings_field(
'wp_plugin_template-setting_c',
'Setting C',
array(&$this, 'settings_field_input_drop'),
'wp_plugin_template',
'wp_plugin_template-section',
array(
'field' => 'setting_c'
)
);
// Possibly do additional admin_init tasks
} // END public static function activate
public function settings_section_wp_plugin_template()
{
// Think of this as help text for the section.
echo 'These settings do things for the WP Plugin Template.';
}
/**
* This function provides text inputs for settings fields
*/
public function settings_field_input_text($args)
{
// Get the field name from the $args array
$field = $args['field'];
// Get the value of this setting
$value = get_option($field);
// echo a proper input type="text"
echo sprintf('<input type="text" name="%s" id="%s" value="%s" />', $field, $field, $value);
} // END public function settings_field_input_text($args)
public function settings_field_input_textarea($args)
{
// Get the field name from the $args array
$field = $args['field'];
// Get the value of this setting
$value = get_option($field);
// echo a proper input type="text"
echo sprintf('<textarea cols="50" rows="5" type="text" name="%s" id="%s" value="%s">%s</textarea>', $field, $field, $value, $value);
} // END public function settings_field_input_text($args)
public function settings_field_input_drop($args)
{
// Get the field name from the $args array
echo "It's a dropdown-menu";
$field = $args['field'];
$items = array("1","2","3");
// Get the value of this setting
$value = get_option($field);
// echo a proper input type="text"
echo sprintf('<select name="%s" id="%s" value="%d">', $field, $field, $value);
foreach ($items as $item) {
if($item == $value) {$selected = 'selected="selected"';}
echo "<option value='$item' $selected>$item</option>";
}
echo "</select>";
} // END public function settings_field_input_text($args)
/**
* add a menu
*/
public function add_menu()
{
// Add a page to manage this plugin's settings
add_options_page(
'WP Plugin Template Settings',
'WP Plugin Template',
'manage_options',
'wp_plugin_template',
array(&$this, 'plugin_settings_page')
);
} // END public function add_menu()
/**
* Menu Callback
*/
public function plugin_settings_page()
{
if(!current_user_can('manage_options'))
{
wp_die(__('You do not have sufficient permissions to access this page.'));
}
// Render the settings template
include(sprintf("%s/templates/settings.php", dirname(__FILE__)));
} // END public function plugin_settings_page()
} // END class WP_Plugin_Template_Settings
} // END if(!class_exists('WP_Plugin_Template_Settings'))