-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.php
113 lines (97 loc) · 4.38 KB
/
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
<?php
/* ------------------------------------------------------------------------ *
* Create page in menu for settings page
* ------------------------------------------------------------------------ */
function default_category_plugin_menu() {
add_options_page(
__('Default Category Plugin','default-category'), // The title to be displayed in the browser window for this page.
__('Default Category', 'default-category'), // The text to be displayed for this menu item
'administrator', // Which type of users can see this menu item
'options-default-category', // The unique ID - that is, the slug - for this menu item
'default_category_page_callback' // The name of the function to call when rendering the page for this menu
);
} // default_category_plugin_menu
add_action('admin_menu', 'default_category_plugin_menu');
/**
* Initializes the options page by registering the Sections,
* Fields, and Settings.
*
* This function is registered with the 'admin_init' hook.
*/
add_action('admin_init', 'default_category_initialize_options');
function default_category_initialize_options() {
defaultCategory_i18n_init();
// First, we register a section. This is necessary since all future options must belong to one.
add_settings_section(
'default_category_section', // ID used to identify this section and with which to register options
__('Default Category Options', 'default-category'), // Title to be displayed on the administration page
'default_category_page_section_callback', // Callback used to render the description of the section
'options-default-category' // Page on which to add this section of options
);
// Next, we will introduce the fields
add_settings_field(
'default_category_id', // ID used to identify the field throughout the theme
'', // The label to the left of the option interface element
'default_category_id_callback', // The name of the function responsible for rendering the option interface
'options-default-category', // The page on which this option will be displayed
'default_category_section', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback. In this case, just a description.
__('Activate this setting to display the header.', 'default-category')
)
);
// Finally, we register the fields with WordPress
register_setting(
'options-default-category',
'default_category_id',
'default_category_id_validate'
);
}
/* ------------------------------------------------------------------------ *
* Section Callbacks
* ------------------------------------------------------------------------ */
/*
* HTML render section
*/
function default_category_page_callback() {
?>
<form method="POST" action="options.php">
<?php settings_fields( 'options-default-category' ); //pass slug name of page, also referred
//to in Settings API as option group name
do_settings_sections( 'options-default-category' ); //pass slug name of page
submit_button();
?>
</form>
<?php
}
/*
* Default section
* Render checkboxes
*/
function default_category_page_section_callback() {
echo '<p>'.__('Select the default category for new posts. Multiple categories can be selected.', 'default-category').'</p>';
$default_category_id = get_option('default_category_id');
echo '<ul>';
wp_category_checklist(0,0, $default_category_id['default_category_id']);
echo '</ul>';
}
/*
* Render category id field
*/
function default_category_id_callback() {
?><input type="hidden" value="<?php echo get_option('default_category_id'); ?>" />
<?php
}
/*
* Validate category id field
* This is where the magic happens
* The values from the category checkboxes are saved as an array in our custom option
*/
function default_category_id_validate($input) {
$input['default_category_id'] = $_POST['post_category'];
return $input;
}
function defaultCategory_i18n_init() {
$pluginDir = dirname(plugin_basename(__FILE__));
load_plugin_textdomain('default-category', false, $pluginDir . '/languages/');
}
?>