Custom meta box saving post issues
-
I am trying to create a custom metabox in the edit post screen – specifically a WooCommerce product post. The box is created ok but saving and recalling the meta data fails and I cannot see why ??
/* Plugin Name: WIZ Custom Meta Box Plugin Plugin URI: Description: Version: 1.0 Author: David Author URI: License: */ add_action( 'add_meta_boxes', 'wizpart_meta_box_init' ); // meta box functions for adding the meta box and saving the data function wizpart_meta_box_init() { // create our custom meta box add_meta_box( 'wizpart_meta', 'Parts Information', 'wizpart_meta_box', 'product', 'normal', 'high' ); } function wizpart_meta_box( $post ) { // retrieve the custom meta box values $wizpart_meta = get_post_meta( $post->ID, '_wizpart_data'); // Debug $wizpart_meta = array("sku"=>"GA1", "quant"=>"6"); echo var_dump($wizpart_meta); $wizpart_sku = (! empty( $wizpart_meta[0]))? $wizpart_meta[0] : 'NA'; $wizpart_quant = (! empty( $wizpart_meta[1]))? $wizpart_meta[1] : 'NA'; //nonce for security wp_nonce_field( 'meta_box_save', 'wizpart_plugin' ); // display stored values echo '<table>'; echo '<tr>'; echo '<td> SKU: </td>'; echo '<td> <input type="text" name="wizpart[sku]" value = "'.esc_attr( $wizpart_sku ).'" size="5" > </td>'; echo '</tr>'; echo '<tr>'; echo '<td> Quant: </td>'; echo '<td> <input type="text" name="wizpart[quant]" value = "'.esc_attr( $wizpart_quant ).'" size="5" > </td>'; echo '</tr>'; echo '</table>'; } // hook to save our meta box data when the post is saved add_action( 'save_post', 'wizpart_save_meta_box' ); function wizpart_save_meta_box( $post_id ) { // process form data if $_POST is set // if auto saving skip saving our meta box data if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; echo "<h2>PHP is Fun!</h2>"; //check nonce for security wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' ); // store data in an array $wizpart_data = $POST['wizpart']; // use array map function to sanitize options $wizpart_data = array_map( 'sanitize_text_field', $wizpart_data) ; // save the meta box data as post meta using the post ID as a unique prefix // using add_post_meta to increment the array with values foreach( $wizpart_data as $value ) { echo $value; add_post_meta( $post_id, '_wizpart_data', $value ); } }
The ‘PHP is Fun!’ line is not displayed. No data is entered under ‘_wizpart_data’ in the DB. Any ideas – I am relatively new to PHP
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Custom meta box saving post issues’ is closed to new replies.