1/**
2 * Calls the class on the post edit screen.
3 */
4function call_someClass() {
5 new someClass();
6}
7
8if ( is_admin() ) {
9 add_action( 'load-post.php', 'call_someClass' );
10 add_action( 'load-post-new.php', 'call_someClass' );
11}
12
13/**
14 * The Class.
15 */
16class someClass {
17
18 /**
19 * Hook into the appropriate actions when the class is constructed.
20 */
21 public function __construct() {
22 add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
23 add_action( 'save_post', array( $this, 'save' ) );
24 }
25
26 /**
27 * Adds the meta box container.
28 */
29 public function add_meta_box( $post_type ) {
30 // Limit meta box to certain post types.
31 $post_types = array( 'post', 'page' );
32
33 if ( in_array( $post_type, $post_types ) ) {
34 add_meta_box(
35 'some_meta_box_name',
36 __( 'Some Meta Box Headline', 'textdomain' ),
37 array( $this, 'render_meta_box_content' ),
38 $post_type,
39 'advanced',
40 'high'
41 );
42 }
43 }
44
45 /**
46 * Save the meta when the post is saved.
47 *
48 * @param int $post_id The ID of the post being saved.
49 */
50 public function save( $post_id ) {
51
52 /*
53 * We need to verify this came from the our screen and with proper authorization,
54 * because save_post can be triggered at other times.
55 */
56
57 // Check if our nonce is set.
58 if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) ) {
59 return $post_id;
60 }
61
62 $nonce = $_POST['myplugin_inner_custom_box_nonce'];
63
64 // Verify that the nonce is valid.
65 if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
66 return $post_id;
67 }
68
69 /*
70 * If this is an autosave, our form has not been submitted,
71 * so we don't want to do anything.
72 */
73 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
74 return $post_id;
75 }
76
77 // Check the user's permissions.
78 if ( 'page' == $_POST['post_type'] ) {
79 if ( ! current_user_can( 'edit_page', $post_id ) ) {
80 return $post_id;
81 }
82 } else {
83 if ( ! current_user_can( 'edit_post', $post_id ) ) {
84 return $post_id;
85 }
86 }
87
88 /* OK, it's safe for us to save the data now. */
89
90 // Sanitize the user input.
91 $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
92
93 // Update the meta field.
94 update_post_meta( $post_id, '_my_meta_value_key', $mydata );
95 }
96
97
98 /**
99 * Render Meta Box content.
100 *
101 * @param WP_Post $post The post object.
102 */
103 public function render_meta_box_content( $post ) {
104
105 // Add an nonce field so we can check for it later.
106 wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
107
108 // Use get_post_meta to retrieve an existing value from the database.
109 $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
110
111 // Display the form, using the current value.
112 ?>
113 <label for="myplugin_new_field">
114 <?php _e( 'Description for this field', 'textdomain' ); ?>
115 </label>
116 <input type="text" id="myplugin_new_field" name="myplugin_new_field" value="<?php echo esc_attr( $value ); ?>" size="25" />
117 <?php
118 }
119}
120
1function custom_meta_box_markup($object)
2{
3 wp_nonce_field(basename(__FILE__), "meta-box-nonce");
4
5 ?>
6 <div>
7 <label for="meta-box-text">Text</label>
8 <input name="meta-box-text" type="text" value="<?php echo get_post_meta($object->ID, "meta-box-text", true); ?>">
9
10 <br>
11
12 <label for="meta-box-dropdown">Dropdown</label>
13 <select name="meta-box-dropdown">
14 <?php
15 $option_values = array(1, 2, 3);
16
17 foreach($option_values as $key => $value)
18 {
19 if($value == get_post_meta($object->ID, "meta-box-dropdown", true))
20 {
21 ?>
22 <option selected><?php echo $value; ?></option>
23 <?php
24 }
25 else
26 {
27 ?>
28 <option><?php echo $value; ?></option>
29 <?php
30 }
31 }
32 ?>
33 </select>
34
35 <br>
36
37 <label for="meta-box-checkbox">Check Box</label>
38 <?php
39 $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true);
40
41 if($checkbox_value == "")
42 {
43 ?>
44 <input name="meta-box-checkbox" type="checkbox" value="true">
45 <?php
46 }
47 else if($checkbox_value == "true")
48 {
49 ?>
50 <input name="meta-box-checkbox" type="checkbox" value="true" checked>
51 <?php
52 }
53 ?>
54 </div>
55 <?php
56}