custom metabox data not being available in $_POST data
-
Currently trying to develop a plugin that will place a text input on the post editor page, the metabox does show up and is visible, however, both the nonce field and the custom metabox value are not in the $_POST array when I publish the post. I’m pretty stumped as I’m new to wordpress and don’t have a ton of experience with PHP. Any help/feedback would be greatly appreciated.
class PayperPostPrice { public function __construct() { add_action('add_meta_boxes', array($this, 'add_payper_meta_box')); add_action('save_post', array($this, 'save_meta_box_data'), 5, 1); } public function meta_box_callback($post) { // add nonce wp_nonce_field(basename(__FILE__), 'payper_meta_box_nonce'); // Retrieve existing value if it exists $payper_post_price = get_post_meta($post->ID, 'payper_post_price', true); // Output HTML for input field ?> <label for="payper_post_price">Price:</label> <input type="text" id="payper_post_price" name="payper_post_price" value="<?php echo $payper_post_price ?>"> <?php } public function add_payper_meta_box() { add_meta_box( 'payper_post_price', // Unique ID for the meta box 'PayPer', // Title of the meta box array($this, 'meta_box_callback'), // Callback function to display content 'post', // Post type where the meta box should be displayed 'side', // Context: 'normal', 'side', or 'advanced' 'default', // Priority: 'default', 'high', 'low', or 'core' ); } public function save_meta_box_data($post_id) { error_log('nonce: ' . $_POST['payper_meta_box_nonce']); // // Check if this is an autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } // Check user permissions if (!current_user_can('edit_post', $post_id)) { return; } // Debug: Log the entire $_POST array error_log('$_POST: ' . print_r($_POST, true)); // Check if 'payper_post_price' key exists in $_POST array if (isset($_POST['payper_post_price'])) { $payper_post_price = sanitize_text_field($_POST['payper_post_price']); update_post_meta($post_id, 'payper_post_price', $payper_post_price); } else { error_log('payper_post_price not found in $_POST array'); } } } // Initialize PayPerMetaBox class new PayperPostPrice();
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.