-
Notifications
You must be signed in to change notification settings - Fork 2
/
metabox.php
executable file
·166 lines (144 loc) · 5.1 KB
/
metabox.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
158
159
160
161
162
163
164
165
166
<?php
/**
* Calls the class on the post edit screen.
*/
function shots_metaboxs() {
new ShotsMetaboxs();
}
if ( is_admin() ) {
add_action( 'load-post.php', 'shots_metaboxs' );
add_action( 'load-post-new.php', 'shots_metaboxs' );
}
/**
* The Class.
*/
class ShotsMetaboxs {
/**
* Hook into the appropriate actions when the class is constructed.
*/
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
}
/**
* Adds the meta box container.
*/
public function add_meta_box( $post_type ) {
// Limit meta box to pages.
if ( $post_type == 'page' ) {
add_meta_box(
'shots_page_metabox',
__( 'Page Meta', 'shots' ),
array( $this, 'shots_render_page_meta' ),
$post_type,
'side',
'high'
);
}
// Limit meta box to post.
if ( $post_type == 'post' ) {
add_meta_box(
'shots_page_metabox',
__( 'Post Meta', 'shots' ),
array( $this, 'shots_render_post_meta' ),
$post_type,
'side',
'default'
);
}
}
/**
* Save the meta when the post is saved.
*
* @param int $post_id The ID of the post being saved.
*/
public function save( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['shots_page_nonce'] ) ) {
return $post_id;
}
$nonce = $_POST['shots_page_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'shots_metaboxs_action' ) ) {
return $post_id;
}
/*
* If this is an autosave, our form has not been submitted,
* so we don't want to do anything.
*/
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
/* OK, it's safe for us to save the data now. */
// Sanitize the user input.
$page_meta = sanitize_text_field( $_POST['shots_page_subtitle'] );
// Update the meta field.
update_post_meta( $post_id, 'shots_page_subtitle', $page_meta );
}
/**
* Render Meta Box content.
*
* @param WP_Post $post The post object.
*/
public function shots_render_page_meta( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'shots_metaboxs_action', 'shots_page_nonce' );
// Use get_post_meta to retrieve an existing value from the database.
$value = get_post_meta( $post->ID, 'shots_page_subtitle', true );
// Display the form, using the current value.
?>
<div class="shots-page-meta">
<table class="form-table shots-table">
<tr>
<th><label for="shots_page_subtitle"><?php _e( 'Subtitle', 'shots' ); ?></label></th>
<td>
<input type="text" id="shots_page_subtitle" name="shots_page_subtitle" value="<?php echo esc_attr( $value ); ?>" size="20" placeholder="Subtitle here" />
</td>
</tr>
<tr>
<th><label for="shots_page_title_bar"><?php _e( 'Show Title Bar', 'shots' ); ?></label></th>
<td>
<select name="shots_page_title_bar">
<?php
$option_values = array(
'yes' => 'Yes',
'no' => 'No',
);
foreach($option_values as $key => $value)
{
if($value == get_post_meta($post->ID, "shots_page_title_bar", true))
{
?>
<option value="<?php echo $key; ?>" selected><?php echo $value; ?></option>
<?php
}
else
{
?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php
}
}
?>
</select>
</td>
</tr>
</table>
</div>
<?php
}
}