-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta-boxes.php
More file actions
103 lines (93 loc) · 3.44 KB
/
meta-boxes.php
File metadata and controls
103 lines (93 loc) · 3.44 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
<?php
/**
* Meta Boxes
*
* A class to easily add custom meta boxes and fields programmatically
*
* @version 0.1
* @author Fetch Designs
* @copyright 2013 Fetch Designs <http://www.fetchdesigns.com/>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class FD_Meta_Boxes
{
/**
* Meta Box Definition
*
* @var array $meta_box
*/
private $meta_box;
/**
* Initialize Class
*
* @param array $meta_box
*/
public function __construct($meta_box)
{
// Clean Input
$defaults = array(
'id' => null, // HTML 'id' attribute of the edit screen section
'title' => ' ',
'post_type' => 'page',
'context' => 'normal', // ('normal', 'advanced', or 'side')
'priority' => 'default', // ('high', 'core', 'default' or 'low')
'fields' => array( // the value of this field is not set as a default, just included for reference
'label' => null,
'key' => null,
'type' => 'text',
),
);
$this->meta_box = array_intersect_key($meta_box + $defaults, $defaults);
add_action('admin_init', array($this, 'admin_init'));
add_action('save_post', array($this, 'save_details'));
}
/**
* Admin Init to actually add the meta box
*/
public function admin_init()
{
add_meta_box($this->meta_box['id'], $this->meta_box['title'], array($this, 'display_fields'), $this->meta_box['post_type']);
}
/**
* Display the fields in the edit pages
*/
public function display_fields()
{
global $post;
$custom = get_post_custom($post->ID);
foreach ($this->meta_box['fields'] as $field) {
$value = (isset($custom[$field['key']])) ? $custom[$field['key']][0] : null;
echo '<label>' . $field['label'] . '</label><br />';
switch ($field['type']) {
case 'textarea':
echo '<textarea name="' . $field['key'] . '" style="width: 98%; ' . (!empty($field['style']) ? ' '.$field['style'] : null) . '"' . (!empty($field['rows']) ? ' rows="'.$field['rows'].'"' : null) . '>' . $value . '</textarea>';
break;
case 'radio':
foreach ($field['options'] as $key=>$option) {
$checked = ($value == $option['value']) ? ' checked="checked"' : null;
echo '<input type="radio" name="' . $field['key'] . '" value="'.$option['value'].'" id="'.$field['key'].'-'.$key.'"'.$checked.'> <label for="'.$field['key'].'-'.$key.'">'.$option['label'].'</label><br />';
}
break;
case 'text':
default:
echo '<input name="' . $field['key'] . '" value="' . $value . '"' . (!empty($field['style']) ? ' style="'.$field['style'].'"' : null) . '" />';
break;
}
echo '<br /><br />';
}
reset($this->meta_box);
}
/**
* Save details when data is updated
*/
public function save_details()
{
global $post;
if (!isset($post->ID)) return;
foreach ($this->meta_box['fields'] as $field) {
$value = (isset($_POST[$field['key']])) ? $_POST[$field['key']] : null;
update_post_meta($post->ID, $field['key'], $value);
}
reset($this->meta_box);
}
}