Skip to content
Newer
Older
100644 325 lines (299 sloc) 9.68 KB
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
1 <?php
2 /**
3 * Registering meta boxes
4 *
5 * In this file, I'll show you how to add more field type (in this case, the 'taxonomy' type)
6 * All the definitions of meta boxes are listed below with comments, please read them CAREFULLY
7 *
8 * You also should read the changelog to know what has been changed
9 *
10 * For more information, please visit: http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html
11 *
12 */
13
14 /**
15 * Add field type: 'taxonomy'
16 *
17 * Note: The class name must be in format "RWMB_{$field_type}_Field"
18 */
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
19 if ( !class_exists( 'RWMB_Taxonomy_Field' ) )
20 {
21 class RWMB_Taxonomy_Field
22 {
23 /**
24 * Enqueue scripts and styles
25 *
26 * @return void
27 */
28 static function admin_print_styles()
29 {
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
30 wp_enqueue_style( 'rwmb-taxonomy', RWMB_CSS_URL . 'taxonomy.css', RWMB_VER );
31 wp_enqueue_script( 'rwmb-taxonomy', RWMB_JS_URL . 'taxonomy.js', array( 'jquery', 'wp-ajax-response' ), RWMB_VER, true );
32 }
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
33
34 /**
35 * Add default value for 'taxonomy' field
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
36 *
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
37 * @param $field
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
38 *
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
39 * @return array
40 */
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
41 static function normalize_field( $field )
42 {
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
43 // Default query arguments for get_terms() function
44 $default_args = array(
45 'hide_empty' => false
46 );
47 if ( !isset( $field['options']['args'] ) )
48 $field['options']['args'] = $default_args;
49 else
50 $field['options']['args'] = wp_parse_args( $field['options']['args'], $default_args );
51
52 // Show field as checkbox list by default
53 if ( !isset( $field['options']['type'] ) )
54 $field['options']['type'] = 'checkbox_list';
55
56 // If field is shown as checkbox list, add multiple value
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
57 if ( 'checkbox_list' == $field['options']['type'] || 'checkbox_tree' == $field['options']['type'])
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
58 $field['multiple'] = true;
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
59
60 if('checkbox_tree' == $field['options']['type'] && !isset( $field['options']['args']['parent'] ) )
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
61 $field['options']['args']['parent'] = 0;
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
62
63 return $field;
64 }
65
66 /**
67 * Get field HTML
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
68 *
902f7d4 @rilwis update demo.php file to make it works during upgrade
authored Nov 25, 2011
69 * @param $html
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
70 * @param $field
71 * @param $meta
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
72 *
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
73 * @return string
74 */
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
75 static function html( $html, $meta, $field )
76 {
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
77 global $post;
78
79 $options = $field['options'];
80
81 $meta = wp_get_post_terms( $post->ID, $options['taxonomy'], array( 'fields' => 'ids' ) );
6e456a7 @rilwis Fix demo & child class "save" call
authored Oct 20, 2011
82 $meta = is_array( $meta ) ? $meta : ( array ) $meta;
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
83 $terms = get_terms( $options['taxonomy'], $options['args'] );
84
85 $html = '';
86 // Checkbox_list
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
87 if ( 'checkbox_list' == $options['type'] )
88 {
89 foreach ( $terms as $term )
90 {
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
91 $html .= "<input type='checkbox' name='{$field['id']}[]' value='{$term->term_id}'" . checked( in_array( $term->term_id, $meta ), true, false ) . " /> {$term->name}<br/>";
92 }
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
93 }
94 //Checkbox Tree
95 elseif ( 'checkbox_tree' == $options['type'] )
96 {
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
97 $html .= self::walk_checkbox_tree($meta, $field, true);
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
98 }
99 // Select
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
100 else
101 {
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
102 $html .= "<select name='{$field['id']}" . ( $field['multiple'] ? "[]' multiple='multiple' style='height: auto;'" : "'" ) . ">";
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
103 foreach ( $terms as $term )
104 {
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
105 $html .= "<option value='{$term->term_id}'" . selected( in_array( $term->term_id, $meta ), true, false ) . ">{$term->name}</option>";
106 }
107 $html .= "</select>";
108 }
109
110 return $html;
111 }
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
112
113 /**
114 * Walker for displaying checkboxes in treeformat
115 *
116 * @param $meta
117 * @param $field
118 * @param bool $active
119 *
120 * @return string
121 */
122 static function walk_checkbox_tree( $meta, $field, $active = false )
123 {
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
124 $options = $field['options'];
125 $terms = get_terms( $options['taxonomy'], $options['args'] );
126 $count = count($terms);
127 $html = '';
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
128 $hidden = ( !$active ? 'hidden' : '' );
129 if ( $count > 0 )
130 {
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
131 $html = "<ul class = 'rw-taxonomy-tree {$hidden}'>";
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
132 foreach ( $terms as $term )
133 {
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
134 $html .= "<li> <input type='checkbox' name='{$field['id']}[]' value='{$term->term_id}'" . checked( in_array( $term->term_id, $meta ), true, false ) . disabled($active,false,false) . " /> {$term->name}";
135 $field['options']['args']['parent'] = $term->term_id;
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
136 $html .= self::walk_checkbox_tree($meta, $field, (in_array( $term->term_id, $meta))) . "</li>";
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
137 }
138 $html .= "</ul>";
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
139 }
e7e599a @funkedgeek Added a checkbox tree option to the taxonomy field class. This type c…
funkedgeek authored Dec 21, 2011
140 return $html;
141 }
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
142
143 /**
144 * Save post taxonomy
145 * @param $post_id
146 * @param $field
147 * @param $old
148 * @param $new
149 */
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
150 static function save( $new, $old, $post_id, $field )
151 {
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
152 wp_set_post_terms( $post_id, $new, $field['options']['taxonomy'] );
153 }
154 }
155 }
156
157 /********************* META BOXES DEFINITION ***********************/
158
159 /**
160 * Prefix of meta keys (optional)
161 * Wse underscore (_) at the beginning to make keys hidden
162 * You also can make prefix empty to disable it
163 */
164 $prefix = '_';
165
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
166 global $meta_boxes;
167
168 $meta_boxes = array();
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
169
170 // First meta box
171 $meta_boxes[] = array(
172 'id' => 'personal', // Meta box id, unique per meta box
173 'title' => 'Personal Information', // Meta box title
174 'pages' => array( 'post', 'slider' ), // Post types, accept custom post types as well, default is array('post'); optional
175 'context' => 'normal', // Where the meta box appear: normal (default), advanced, side; optional
176 'priority' => 'high', // Order of meta box: high (default), low; optional
177
178 'fields' => array( // List of meta fields
179 array(
180 'name' => 'Full name', // Field name
181 'desc' => 'Format: First Last', // Field description, optional
182 'id' => $prefix . 'fname', // Field id, i.e. the meta key
183 'type' => 'text', // Field type: text box
184 'std' => 'Anh Tran' // Default value, optional
185 ),
186 array(
187 'name' => 'DOB',
188 'id' => $prefix . 'dob',
189 'type' => 'date', // File type: date
190 'format' => 'd MM, yy' // Date format, default yy-mm-dd. Optional. See: http://goo.gl/po8vf
191 ),
192 array(
193 'name' => 'Gender',
194 'id' => $prefix . 'gender',
195 'type' => 'radio', // File type: radio box
196 'options' => array( // Array of 'key' => 'value' pairs for radio options. Note: the 'key' is stored in meta field, not the 'value'
197 'm' => 'Male',
198 'f' => 'Female'
199 ),
200 'std' => 'm',
201 'desc' => 'Need an explaination?'
202 ),
203 array(
204 'name' => 'Bio',
205 'desc' => 'What\'s your professions? What you\'ve done?',
206 'id' => $prefix . 'bio',
207 'type' => 'textarea', // File type: textarea
208 'std' => 'I\'m a WP developer and a freelancer from Vietnam.',
209 'style' => 'width: 200px; height: 100px'
210 ),
211 array(
212 'name' => 'Where do you live?',
213 'id' => $prefix . 'place',
214 'type' => 'select', // File type: select box
215 'options' => array( // Array of 'key' => 'value' pairs for select box
216 'usa' => 'USA',
217 'vn' => 'Vietnam'
218 ),
219 'multiple' => true, // Select multiple values, optional. Default is false.
220 'std' => array( 'vn' ), // Default value, can be string (single value) or array (for both single and multiple values)
221 'desc' => 'Select the current place, not in the past'
222 ),
223 array(
224 'name' => 'About WordPress', // File type: checkbox
225 'id' => $prefix . 'love_wp',
226 'type' => 'checkbox',
720ed70 @rilwis Fix issue 6, change EOL to LF
authored Nov 8, 2011
227 'desc' => 'I love WordPress',
228 'std' => 1 // Value can be 0 or 1
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
229 ),
230 array(
231 'name' => 'Categories',
232 'id' => $prefix . 'cats',
233 'type' => 'taxonomy', // File type: taxonomy
234 'options' => array(
235 'taxonomy' => 'category', // Taxonomy name
236 'type' => 'checkbox_list', // How to show taxonomy: 'checkbox_list' (default) or 'select'. Optional
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
237 'args' => array(), // Additional arguments for get_terms() function
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
238 ),
239 'desc' => 'Choose One Category'
240 )
241 )
242 );
243
244 // Second meta box
245 $meta_boxes[] = array(
246 'id' => 'additional',
247 'title' => 'Additional Information',
248 'pages' => array( 'post', 'film', 'slider' ),
249
250 'fields' => array(
251 array(
252 'name' => 'Your thoughts about Deluxe Blog Tips',
253 'id' => $prefix . 'thoughts',
254 'type' => 'wysiwyg', // Field type: WYSIWYG editor
255 'std' => '<b>It\'s great!</b>',
256 'desc' => 'Do you think so?',
257 ),
258 array(
259 'name' => 'Upload your source code',
260 'desc' => 'Any modified code, or extending code',
261 'id' => $prefix . 'code',
262 'type' => 'file' // Field type: file upload
263 ),
264 array(
265 'name' => 'Screenshots',
266 'desc' => 'Screenshots of problems, warnings, etc.',
267 'id' => $prefix . 'screenshot',
268 'type' => 'image' // Field type: image upload
269 )
270 )
271 );
272
273 // Third meta box
274 $meta_boxes[] = array(
275 'id' => 'survey',
276 'title' => 'Survey',
277 'pages' => array( 'post', 'slider', 'page' ),
278
279 'fields' => array(
280 array(
281 'name' => 'Your favorite color',
282 'id' => $prefix . 'color',
283 'type' => 'color' // Field type: color
284 ),
285 array(
286 'name' => 'Your hobby',
287 'id' => $prefix . 'hobby',
288 'type' => 'checkbox_list', // Field type: checkbox list
289 'options' => array( // Options of checkboxes, in format 'key' => 'value'
290 'reading' => 'Books',
291 'sport' => 'Gym, Boxing'
292 ),
293 'desc' => 'What do you do in free time?'
294 ),
295 array(
296 'name' => 'When do you get up?',
297 'id' => $prefix . 'getdown',
298 'type' => 'time', // Field type: time
299 'format' => 'hh:mm:ss' // Time format, default hh:mm. Optional. See: http://goo.gl/hXHWz
300 )
301 )
302 );
303
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
304 // Hook to 'admin_init' to make sure the meta box class is loaded before (in case using the meta box class in another plugin)
305 // This is also helpful for some conditionals like checking page template, categories, etc.
306 add_action( 'admin_init', 'your_prefix_register_meta_boxes' );
307
902f7d4 @rilwis update demo.php file to make it works during upgrade
authored Nov 25, 2011
308 /**
309 * Register meta boxes
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
310 *
311 * @return void
902f7d4 @rilwis update demo.php file to make it works during upgrade
authored Nov 25, 2011
312 */
2b07a1e @rilwis Add styles for 'side' meta boxes
authored Dec 27, 2011
313 function your_prefix_register_meta_boxes()
314 {
315 global $meta_boxes;
316
317 // Make sure there's no errors when the plugin is deactivated or during upgrade
318 if ( class_exists( 'RW_Meta_Box' ) )
319 {
320 foreach ( $meta_boxes as $meta_box )
321 {
322 new RW_Meta_Box( $meta_box );
323 }
902f7d4 @rilwis update demo.php file to make it works during upgrade
authored Nov 25, 2011
324 }
9236cd3 @rilwis - Done the 4.0 version
authored Oct 7, 2011
325 }
Something went wrong with that request. Please try again.