• Resolved dgcov

    (@dgcov)


    Hi.

    I’m adding some meta_boxes:
    add_meta_box('ss_product','Simple Sales fields','simple_sales_show_boxes', 'ss_product', 'normal', 'high');

    This is my callback simple_sales_show_boxes

    function simple_sales_show_boxes(){
      global $post;
      $ID=$post->ID;
      echo '<input type="hidden" name="simple_sales_meta_boxes_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
      $box_fields=
        array(
          array(
            'name' => 'price',
            'desc' => 'Price of Item',
            'id' => 'product_price',
            'type' => 'text',
            'value' => get_post_meta($ID,'product_price',true)
          ),
          array(
            'name' => 'ordered',
            'desc' => 'Items ordered',
            'id' => 'ordered',
            'type' => 'number',
            'value' => get_post_meta($ID,'ordered',true)
          ),
          array(
            'name' => 'stock',
            'desc' => 'Stock remaining',
            'id' => 'stock',
            'type' => 'number',
            'value' => get_post_meta($ID,'ordered',true)
          )
      );
      foreach($box_fields as $field){
        echo '<tr>
              <div style="width:20%"><label for="'. $field['id']. '">'. $field['desc'].'</div>';
        switch($field['type']){
          case 'text':
            echo '<input type="text" name="'. $field['id']. '" id="'. $field['id']. '" value="'.$field['value']. '"
              size="30" style="width:50%" /><br />';
            break;
          case 'hidden':
            echo "
            <input id='".$field['id']."' type='hidden' name='".$field['id']."' value='".$field['value']."'>";
          break;
          case 'photos':
            echo '<input id="upload_image" type="text" size="36" name="upload_image" value="" />
            <input id="upload_image_button" type="button" value="Upload Image" />';
          break;
          case 'number':
            echo '<input id="'.$field['id'].'" type="text" size="36" name="upload_image" value=""
            onkeypress="return (event.charCode >= 48 && event.charCode <= 57)||event.charCode ==46||event.charCode==45" /><br />';
          break;
        }
        echo '</label>';
      }
    }

    I then use the save_post add_action with the callback simple_sales_save_products

    add_action('save_post','simple_sales_save_products');
    
    function simple_sales_save_products(){
      if(!check_nonces_an_all('simple_sales_meta_boxes_nonce','ss_product')){
        return;
      }
      global $post;
      update_post_meta($post->ID, "product_price", $_POST["price"]);
      update_post_meta($post->ID, "stock", $_POST["stock"]);
      update_post_meta($post->ID, "ordered", $_POST["ordered"]);
    }

    However, when the $_POST array is checked it’s empty.

    $_POST = array (
    );

    And the database table post_meta has NULL values in the meta_value fields corresponding to the meta_keys ‘product_price’, ‘ordered’ and ‘stock’.

    What am I doing wrong?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    How are you checking $_POST? It should never be empty when ‘save_post’ fires, regardless of what you do with meta boxes. $_POST also contains all the normal post fields needed to save the post, it shouldn’t be empty under any circumstance.

    It’s possible some plugin is unsetting $_POST before your simple_sales_save_products callback runs, though no one should be doing that. Try deactivating all plugins to see if a plugin is the cause. Also revert to a twenty* theme to ensure your theme is not at fault. Naturally you would need to copy your meta box code over to something that is active.

    Your simple_sales_save_products code does have a few issues contributing to your problem, though they are unrelated to $_POST being empty. $post->ID is invalid in this context, the correct post ID is passed to your callback, but your callback needs to collect it in order to use it.

    function simple_sales_save_products( $post_id ){
      update_post_meta($post_id, "product_price", $_POST["product_price"]);
      update_post_meta($post_id, "stock", $_POST["stock"]);
      update_post_meta($post_id, "ordered", $_POST["ordered"]);
    }

    The $_POST key for price will be ‘product_price’ because when you output the form you use $field['id'] for the name attribute.

    I’m not able to verify your nonce check is correct. In this context you don’t really need to do this because the post edit form has its own nonce check, ‘save_post’ will not fire unless the nonce is correct.

    Perhaps you intend to add field validation and sanitation later. It’s extremely important you do this before going live. Never save anything from a client process into your DB without doing this first!

    Thread Starter dgcov

    (@dgcov)

    Thanks for the reply.

    I’m using Blackbox to view the globals.

    $_GET = array (
      'post' => '217',
      'action' => 'edit',
      'message' => '6',
    );
    
    $_POST = array (
    );

    I think you are correct that something is emptying the $_POST global and I’ll look into the other plugins.

    I’m currently running this locally on a test machine, I’ll sort out the sanitation later.

    Moderator bcworkz

    (@bcworkz)

    I recognize that $_GET data. It is from the request after the POST request, the one that displays “Post updated.” with a view post link. You’re not seeing the correct request data that is POSTed.

    It’s not that easy to view debug data within the ‘save_post’ callback, screen output from here doesn’t show up anywhere. What you can do is capture debug data with $data = print_r( $_POST, true );, then output the data to a log file. Or email yourself the data with wp_mail() if your installation has any kind of mail functionality.

    Thread Starter dgcov

    (@dgcov)

    Thanks.

    That is very helpful.

    Thread Starter dgcov

    (@dgcov)

    Thank you again.

    I had failed to allocate a ‘name’ to my text inputs and only had an ‘id’ element, so the $_POST[‘stock’], etc wasn’t being populated.

    After I had used $data = print_r( $_POST, true );, I saw that the fields weren’t present and was able to identify the cause.

    Thanks once again!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘$_POST variable empty when using add_action save_post’ is closed to new replies.