/
CustomMetabox.php
156 lines (135 loc) · 5.27 KB
/
CustomMetabox.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
<?php
/**
* Remember to include this metabox in functions.php
* include_once( get_stylesheet_directory() . '/inc/meta_boxes/CustomMetabox.php' );
*/
class ajotka_custom_metabox {
/**
* [REQUIRED]
* The type of writing screens on which to show the edit screen section
* includes: 'post','page','dashboard','link','attachment','custom_post_type','comment'
*
* @param array $screens
*/
public $screens = array('page');
/**
* [REQUIRED]
* @param string $id = null HTML 'id' attribute of the edit screen section
*/
public $id = 'ajotka_custom';
/**
* [REQUIRED]
* @param string $title Title of the edit screen section, visible to user
*/
public $title = 'AJOTKA Custom metabox';
/**
* [REQUIRED]
* Function that prints out the HTML for the edit screen section. The function name as a string,
* or, within a class, an array to call one of the class's methods.
* The callback can accept up to two arguments: the first argument is the $post object for
* the post or page that is currently being edited.
* The second argument is the full $metabox item (an array).
*
* @param callback $callback = null
*/
public $callback = null;
/**
* The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side').
* (Note that 'side' doesn't exist before 2.7)
*
* @param string $context = 'advanced'
*/
public $context = 'normal';
/**
* The priority within the context where the boxes should show ('high', 'core', 'default' or 'low')
*
* @param string $priority = 'default'
*/
public $priority = 'high';
/**
* Arguments to pass into your callback function. The callback will receive the $post object and whatever
* parameters are passed through this variable.
*
* @param array $callback_args = null
*/
public $callback_args = null;
/**
* @param array $fields Array of field names to be saved
*/
public $fields = array(
'ajotka_custom',
);
/**
* Construct
*/
public function __construct() {
// $this->screens = ( empty( $this->screens ) )? array('post') : $this->screens;
$this->callback = array($this, 'render_metabox');
add_action('add_meta_boxes', array($this, 'add_metabox'));
add_action('save_post', array($this, 'save'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
}
/**
* Add metabox
*/
public function add_metabox() {
foreach ( $this->screens as $screen ) {
add_meta_box(
$this->id,
$this->title,
$this->callback,
$screen,
$this->context,
$this->priority,
$this->callback_args
);
}
}
/**
* Output metabox content
*/
public function render_metabox( $post ) {
?>
<p>
<label for="ajotka_custom" style="display: block;"><strong>Select</strong></label>
<select name="ajotka_custom" id="ajotka_custom">
<option value=""></option>
<option value="val1" selected="selected">Option 1</option>
<option value="val2">Option 2</option>
</select>
</p>
<script>
(function($){
$('select#ajotka_custom').select2({
placeholder: 'Select...',
allowClear: true,
width: '100%',
tags: true,
tokenSeparators: [',', ' ']
});
})(jQuery);
</script>
<?php
}
/**
* Save metadata to database
*/
public function save( $post_id ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if ( array_key_exists('ajotka_custom', $_POST) ) {
update_post_meta($post_id, 'ajotka_custom', $_POST['ajotka_custom'] ) ;
}
}
public function enqueue_scripts( $hook ) {
if ( ! in_array( $hook , array('post.php','post-new.php')) )
return;
global $post_type;
if ( 'signup' != $post_type )
return;
wp_enqueue_style( 'select2-css', get_stylesheet_directory_uri() . '/lib/select2/select2.min.css', array(), '4.0.1');
wp_enqueue_script( 'select2-js', get_stylesheet_directory_uri() . '/lib/select2/select2.min.js', array('jquery'), '4.0.1');
}
}
new ajotka_custom_metabox();
?>