-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
66 lines (41 loc) · 1.24 KB
/
functions.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
<?php
class wpc_post_editor {
public function __construct(){
add_action('add_meta_boxes', [$this, 'create_meta_box']);
add_action('save_post', [$this, 'save_editor']);
}
public function create_meta_box(){
add_meta_box('wpc_editor', 'Post Editor', [$this, 'meta_box_html'], ['post']);
}
public function save_editor( $post_id ) {
if(isset($_POST['wpc_post_editor']) && is_numeric($_POST['wpc_post_editor']) ){
$editor_id = sanitize_text_field($_POST['wpc_post_editor']);
update_post_meta($post_id, 'wpc_post_editor', $editor_id );
}
}
public function meta_box_html(){
$user_query = new WP_User_Query([
'role' => 'editor',
'number' => '-1',
'fields' => [
'display_name',
'ID',
],
]);
$editors = $user_query->get_results();
if( ! empty( $editors ) ) {
?>
<label for="post_editor">Editor:</label>
<select name="wpc_post_editor" id="post_editor">
<option> - Select One -</option>
<?php
foreach ($editors as $editor) {
echo '<option value="'.$editor->ID.'" '.selected(get_post_meta(get_the_ID(), 'wpc_post_editor', true), $editor->ID, false).'>'.$editor->display_name.'</option>';
}
?>
</select>
<?php
} else {
echo '<p>No Editors Found.</p>';
}
}