forked from chrisguitarguy/WPSE-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-page-meta-box.php
157 lines (137 loc) · 4.06 KB
/
custom-page-meta-box.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
<?php
/*
Plugin Name: Custom Page Meta Box
Plugin URI: http://wordpress.stackexchange.com/q/57092/6035
Description: Put meta boxes on a plugin page for fun and profit
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Christopher Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
WPSE57092::init();
class WPSE57092
{
/**
* The hook to add an action to add our meta boxes.
*
*/
const ADD_HOOK = 'wpse57092_add_boxes';
/**
* The page key for our meta boxes. You could use this as the "group" for
* the settings API as well or something similar.
*
*/
const PAGE = 'do_wpse57092_boxes';
/**
* The setting key.
*
*/
const SETTING = 'wpse57092_opts';
public static function init()
{
add_action(
'admin_menu',
array(__CLASS__, 'page')
);
add_action(
self::ADD_HOOK,
array(__CLASS__, 'meta_box')
);
add_action(
'admin_init',
array(__CLASS__, 'settings')
);
}
public static function settings()
{
register_setting(
self::PAGE,
self::SETTING,
array(__CLASS__, 'validate')
);
add_settings_section(
'default',
__('A Settings Section', 'wpse57092'),
'__return_false',
self::PAGE
);
add_settings_field(
'wpse57092-text',
__('Some Field', 'wpse57092'),
array(__CLASS__, 'field_cb'),
self::PAGE,
'default',
array('label_for' => self::SETTING)
);
}
public static function meta_box()
{
add_meta_box(
'custom-meta-wpse57092',
__('Just Another Meta Box', 'wpse57092'),
array(__CLASS__, 'box_cb'),
self::PAGE,
'main',
'high'
);
}
public static function box_cb($setting)
{
// do_settings_fields doesn't do form tables for you.
echo '<table class="form-table">';
do_settings_fields(self::PAGE, 'default');
echo '</table>';
}
public static function field_cb($args)
{
printf(
'<input type="text" id="%1$s" name="%1$s" class="widefat" value="%2$s" />',
esc_attr($args['label_for']),
esc_attr(get_option($args['label_for']))
);
echo '<p class="description">';
_e('Just some help text here', 'wpse57092');
echo '</p>';
}
public static function page()
{
$p = add_options_page(
__('WPSE 57092 Options', 'wpse57092'),
__('WPSE 57092', 'wpse57092'),
'manage_options',
'wpse57092-options',
array(__CLASS__, 'page_cb')
);
}
public static function page_cb()
{
do_action(self::ADD_HOOK);
?>
<div class="wrap metabox-holder">
<?php screen_icon(); ?>
<h2><?php _e('WPSE 57092 Options', 'wpse57092'); ?></h2>
<p> </p>
<form action="<?php echo admin_url('options.php'); ?>" method="post">
<?php
settings_fields(self::PAGE);
do_meta_boxes(self::PAGE, 'main', self::SETTING);
?>
</form>
</div>
<?php
}
public static function validate($dirty)
{
return esc_url_raw($dirty);
}
}