Custom Nonce
public function testimonial_module_meta_boxes() {
[add_meta_box](http://codex.wordpress.org/Function_Reference/add_meta_box)( 'testimonial_module_company_meta_box', __( 'Company Info', 'textdomain' ), array( $this, 'testimonial_module_company_meta_box' ), 'cpt_testimonial', 'side', 'default' );
}
public function testimonial_module_company_meta_box( $post ) {
if ($post->post_type != 'cpt_testimonial' ) {
return;
}
$post_id = $post->ID;
$value = [get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)( $post_id, '_company_meta_field', true );
[wp_nonce_field](http://codex.wordpress.org/Function_Reference/wp_nonce_field)( basename(__FILE__), 'testimonial_module_class_nonce' );
$output = '';
$output .= '<p>' . __( 'Company Name', 'textdomain' ) . '</p>';
$output .= '<label><input type="text" class="widefat" value="' . $value .'" name="_company_meta_field" placeholder="Company Name" autocomplete="off" /></label>';
echo $output;
}
public function testimonial_module_save_meta_box( $post_id ) {
// Save logic goes here. Don't forget to include nonce checks!
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (
!isset($_POST[ 'testimonial_module_class_nonce'] ) ||
( $_POST['testimonial_module_class_nonce'], basename(__FILE__) )
) {
return;
}
if (('edit_post', $post_id)) {
return;
}
$new_meta_value = ( isset( $_POST['testimonial_module_class_nonce'] ) ? [sanitize_html_class](http://codex.wordpress.org/Function_Reference/sanitize_html_class)( $_POST['testimonial_module_class_nonce'] ) : '' );
$meta_key = '_company_meta_field';
$meta_value = [get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)( $post_id, $meta_key, true );
if ( $new_meta_value && '' == $meta_value )
[add_post_meta](http://codex.wordpress.org/Function_Reference/add_post_meta)( $post_id, $meta_key, $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
[update_post_meta](http://codex.wordpress.org/Function_Reference/update_post_meta)( $post_id, $meta_key, $new_meta_value );
else
[delete_post_meta](http://codex.wordpress.org/Function_Reference/delete_post_meta)( $post_id, $meta_key, $new_meta_value );
}
// Another include file
private function define_admin_hooks() {
$plugin_admin = new Testimonial_Module_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->[add_action](http://codex.wordpress.org/Function_Reference/add_action)( '[save_post](http://codex.wordpress.org/Plugin_API/Action_Reference/save_post)', $plugin_admin, 'testimonial_module_company_meta_box' );
$this->loader->[add_action](http://codex.wordpress.org/Function_Reference/add_action)( '[save_post](http://codex.wordpress.org/Plugin_API/Action_Reference/save_post)', $plugin_admin, 'testimonial_module_save_meta_box', 10, 2 );
}