“isset” not working in from submission to WP metabox
-
I cannot see what is wrong with the statements below which try to identify an error. Nothing is saved and nothing printed – why?
For printing do I need to create a text field in the form?
if ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"]))) //if ( (empty($_POST["wizpart[sku]"])) || (empty($_POST["wizpart[quant]"]))) { //if (! isset($_POST["wizpart[sku]"], $_POST["wizpart[quant]"])) { print '<p class= "error">Fields not completed!</p>'; $okay = FALSE; }
Complete code
<?php 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', // Unique ID 'Parts Information', // Title 'wizpart_meta_box', // Callback function 'product', // post type 'normal', // Context 'high' // Priority ); } function wizpart_meta_box( $post ) { // Leading HTML // retrieve the custom meta box values $wizpart_meta_full = get_post_meta( $post->ID, '_wizpart_data'); echo var_dump($wizpart_meta_full).'<br />'; if (! empty($wizpart_meta_full)) { foreach( $wizpart_meta_full as $entry){ // print out each part print 'Product comprises '.$entry["quant"].' units of '.$entry["sku"].' <br />'; } } //nonce for security wp_nonce_field( 'meta_box_save', 'wizpart_plugin' ); // Form HTML echo '<div class = wizpart>'; echo '<table>'; echo '<tr>'; echo '<td> Enter correct SKU of part: </td>'; echo '<td> <input type="text" name="wizpart[sku]" value = "" size="5" > </td>'; echo '</tr>'; echo '<tr>'; echo '<td> Enter quantity of parts required as integer: </td>'; echo '<td> <input type="text" name="wizpart[quant]" value = "" size="5" > </td>'; echo '</tr>'; echo '</table>'; echo '</div>'; } // hook to save meta box data when the post is saved add_action( 'save_post', 'wizpart_save_meta_box' ); function wizpart_save_meta_box( $post_id ) { // Flag variable $okay = TRUE; // catch incomplete form if ( (! isset($_POST["wizpart[sku]"])) || (! isset($_POST["wizpart[quant]"]))) //if ( (empty($_POST["wizpart[sku]"])) || (empty($_POST["wizpart[quant]"]))) { //if (! isset($_POST["wizpart[sku]"], $_POST["wizpart[quant]"])) { print '<p class= "error">Fields not completed!</p>'; $okay = FALSE; } // if auto saving skip saving our meta box data if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; //check nonce for security wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' ); // store data in an array // Get the post ID from the from SKU $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 if ($okay) { add_post_meta( $post_id, '_wizpart_data', $wizpart_data ); print '<p class= "error">Success!</p>'; } else { print '<p class= "error">Encountered error</p>'; } } ?>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘“isset” not working in from submission to WP metabox’ is closed to new replies.