@@ -36,12 +36,12 @@ public function register() {
3636
3737 public function render () {
3838
39- wp_nonce_field ( basename ( __FILE__ ), 'metaBoxName_nonce ' ); ?>
39+ wp_nonce_field ( basename ( __FILE__ ), 'metabox_name_nonce ' ); ?>
4040
4141 <p>
42- <label for="metaBoxName "><?php _e ( "Custom Text " , 'myPlugintextDomain ' ); ?> </label>
42+ <label for="metabox_name "><?php _e ( "Custom Text " , 'textdomain ' ); ?> </label>
4343 <br />
44- <input class="widefat" type="text" name="metaBoxFieldName " id="metaBoxFieldName " value="<?php echo esc_attr ( get_post_meta ( $ object ->ID , 'metaBoxName ' , true ) ); ?> " />
44+ <input class="widefat" type="text" name="metabox_field_name " id="metabox_field_name " value="<?php echo esc_attr ( get_post_meta ( $ object ->ID , 'metabox_name ' , true ) ); ?> " />
4545 </p>
4646 <?php
4747 }
@@ -51,34 +51,21 @@ public function render() {
5151 //Save the post data
5252 function save ( $ post_id , $ post ) {
5353
54+ //Check if doing autosave
55+ if ( defined ( 'DOING_AUTOSAVE ' ) && DOING_AUTOSAVE ) return ;
56+
5457 //Verify the nonce before proceeding.
55- if ( !isset ( $ _POST ['metaBoxName_nonce ' ] ) || !wp_verify_nonce ( $ _POST ['metaBoxName_nonce ' ], basename ( __FILE__ ) ) )
56- return $ post_id ;
58+ if ( !isset ( $ _POST ['metabox_name_nonce ' ] ) || !wp_verify_nonce ( $ _POST ['metabox_name_nonce ' ], basename ( __FILE__ ) ) ) return ;
5759
5860 //Get the post type object.
5961 $ post_type = get_post_type_object ( $ post ->post_type );
6062
6163 //Check if the current user has permission to edit the post.
62- if ( !current_user_can ( $ post_type ->cap ->edit_post , $ post_id ) )
63- return $ post_id ;
64-
65- //Get the posted data and sanitize it for use as an HTML class.
66- $ new_meta_value = ( isset ( $ _POST ['metaBoxName ' ] ) ? sanitize_html_class ( $ _POST ['metaBoxName ' ] ) : '' );
67-
68- //Get the meta key.
69- $ meta_key = 'metaBoxName ' ;
70-
71- //Get the meta value of the custom field key.
72- $ meta_value = get_post_meta ( $ post_id , $ meta_key , true );
64+ if ( !current_user_can ( $ post_type ->cap ->edit_post , $ post_id ) ) return $ post_id ;
7365
74- //If a new meta value was added and there was no previous value, add it.
75- if ( $ new_meta_value && '' == $ meta_value ) {
76- add_post_meta ( $ post_id , $ meta_key , $ new_meta_value , true );
77- } elseif ( $ new_meta_value && $ new_meta_value != $ meta_value ) {
78- update_post_meta ( $ post_id , $ meta_key , $ new_meta_value );
79- } elseif ( '' == $ new_meta_value && $ meta_value ) {
80- delete_post_meta ( $ post_id , $ meta_key , $ meta_value );
66+ if ( isset ( $ _POST ['metabox_field_name ' ] ) ) {
67+ update_post_meta ( $ post_id , 'metabox_field_name ' , esc_attr ($ _POST ['metabox_field_name ' ]) );
8168 }
8269 }
8370 }
84- } ?>
71+ } ?>
0 commit comments