-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheadstart_settings.php
More file actions
198 lines (144 loc) · 5.56 KB
/
Copy pathheadstart_settings.php
File metadata and controls
198 lines (144 loc) · 5.56 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
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
<?php
/*
* Headstart plugin settings
*/
add_action( 'admin_menu', 'headstart_settings_page_setup' );
function headstart_settings_page_setup() {
global $headstart;
$headstart -> select1 = array(
'key1' => __( 'value1', 'headstart' ),
'key2' => __( 'value2', 'headstart' )
);
/* add_options_page( $page_title, $menu_title, $capability, $menu_slug, $funtion ); */
$headstart -> settings_page = add_options_page( __( 'Headstart Settings', 'headstart' ), __( 'Headstart', 'headstart' ), 'manage_options', 'headstart-settings-page', 'headstart_display_settings_page' );
add_action( 'admin_init', 'headstart_register_settings' );
add_action( 'load-' . $headstart -> settings_page, 'headstart_settings_sections' );
/* Admin scripts & Styles */
add_action( 'admin_print_scripts-' . $headstart -> settings_page, 'headstart_admin_scripts' );
add_action( 'admin_print_styles-' . $headstart -> settings_page, 'headstart_admin_styles' );
}
function headstart_register_settings() {
/* register_setting( $option_group, $option_name, $sanitize_callback ); */
register_setting( 'headstart_settings', 'headstart_settings', 'headstart_validate_settings' );
}
function headstart_settings_sections() {
/* add_settings_section( $id, $title, $callback, $page ); */
add_settings_section( 'main_settings', 'Main Settings', 'headstart_section_main', 'headstart-settings-page' );
}
function headstart_section_main() {
global $headstart;
}
function headstart_create_setting($args = array(), $before = '<p>', $after = '</p>') {
extract( $args );
$field_value = headstart_get_option( $id );
$prefix = 'headstart_settings';
$setting_id = "$prefix[$id]";
$html = $before . "\n";
switch ($type) {
case 'text' :
$html .= "\t" . '<input type="text" id="' . $id . '" name="' . $setting_id . '" ';
if ( isset( $class ) )
$html .= 'class="' . $class . '" ';
$html .= 'value="' . esc_attr( $field_value ) . '" />' . "\n";
if ( isset( $label ) )
$html .= "\t" . '<label for="' . $id . '">' . $label . '</label>' . "\n";
if ( isset( $desc ) )
$html .= '<span class="description">' . esc_attr( $desc ) . '</span>' . "\n";
break;
case 'checkbox' :
$html .= "\t" . '<input type="checkbox" id="' . $id . '" name="' . $setting_id . '" ';
if ( isset( $class ) )
$html .= 'class="' . $class . '" ';
$html .= 'value="' . $value . '"' . checked( $value, $field_value, false ) . ' />' . "\n";
if ( isset( $label ) )
$html .= "\t" . '<label for="' . $id . '">' . $label . '</label>' . "\n";
if ( isset( $desc ) )
$html .= '<span class="description">' . esc_attr( $desc ) . '</span>' . "\n";
break;
case 'select' :
$html .= "\t" . '<select id="' . $id . '" name="' . $setting_id . '"';
if ( isset( $class ) )
$html .= ' class="' . $class . '"';
$html .= '>';
foreach ( $options as $value => $label ) {
$html .= "\t\t" . '<option value="' . esc_attr( $value ) . '"' . selected( $value, $field_value, false ) . '>' . esc_attr( $label ) . '</option>' . "\n";
}
$html .= "\t" . '</select>' . "\n";
if ( isset( $label ) )
$html .= "\t" . '<label for="' . $id . '">' . $label . '</label>' . "\n";
if ( isset( $desc ) )
$html .= '<span class="description">' . esc_attr( $desc ) . '</span>' . "\n";
break;
case 'textarea' :
if ( isset( $label ) )
$html .= "\t" . '<label for="' . $id . '">' . $label . '</label>' . "\n";
$html .= "\t" . '<textarea id="' . $id . '" name="' . $setting_id . '" ';
if ( isset( $class ) )
$html .= 'class="' . $class . '" ';
if ( isset( $rows ) )
$html .= 'rows="' . $rows . '"';
if ( isset( $cols ) )
$html .= 'cols="' . $cols . '"';
$html .= '>' . esc_attr( $field_value ) . '</textarea>' . "\n";
if ( isset( $desc ) )
$html .= '<span class="description">' . esc_attr( $desc ) . '</span>' . "\n";
break;
default :
break;
}
$html .= $after . "\n";
return $html;
}
function headstart_display_settings_page() {
echo '<div class="wrap">' . "\n";
screen_icon( );
echo '<h2>Headstart Settings</h2>' . "\n";
settings_errors( );
echo '<form method="post" action="options.php">' . "\n";
settings_fields( 'headstart_settings' );
do_settings_sections( $_GET['page'] );
submit_button( esc_attr__( 'Update Settings' ) );
echo '</form>' . "\n";
echo '</div>' . "\n";
}
function headstart_validate_settings($input) {
global $headstart;
$text_options = array(
'text1',
'text2'
);
foreach ( $text_options as $text_option ) {
$settings[$text_option] = trim( esc_attr( $input[$text_option] ) );
}
$integer_options = array(
'int1',
'int2'
);
foreach ( $integer_options as $integer_option ) {
$settings[$integer_option] = is_numeric( $input[$integer_option] ) ? intval( $input[$integer_option] ) : headstart_get_option( $integer_option );
}
$float_options = array(
'float1',
'float2'
);
foreach ( $float_options as $float_option ) {
$settings[$float_option] = is_numeric( $input[$float_option] ) ? round( $input[$float_option], 2 ) : headstart_get_option( $float_option );
}
$checkbox_options = array(
'checkbox1',
'checkbox2',
);
foreach ( $checkbox_options as $checkbox_option ) {
$settings[$checkbox_option] = isset( $input[$checkbox_option] ) ? true : false;
}
if ( array_key_exists( $input['select1'], $headstart -> select1 ) )
$settings['select1'] = $input['select1'];
return $settings;
}
function headstart_admin_scripts() {
wp_enqueue_script( 'headstart-admin-script', HEADSTART_URI . 'js/admin-script.js' );
}
function headstart_admin_styles() {
wp_enqueue_style( 'headstart-admin-style', HEADSTART_URI . 'css/admin-style.css' );
}
?>