$_POST is empty after 'save_post' action
-
I’m new to plug-in development, but not wordpress, itself. I think I’m doing something wrong but cannot figure out what.
I created a new metabox on the ‘edit post’ page. I put a checkbox in it.
Based on the checkbox being ticked or not, it will create/update a custom field with a value.
My problem is after I hit ‘update post’, the page reloads, but $_POST is empty./* Add a new meta box to the admin menu. */ add_action( 'admin_menu', 'fm_create_meta_box' ); /* Saves the meta box data. */ add_action( 'save_post', 'fm_save_meta_data' ); /* Function for adding meta boxes to the admin. */ function fm_create_meta_box() { add_meta_box( 'post-meta-boxes', __('Featured Post'), 'display_post_meta_boxes', 'post', 'side', 'high' ); } function display_post_meta_boxes() { global $post; var_dump($_POST); if(isset($_POST['chkFeatureMe'])){ $check = $_POST['chkFeatureMe']; } else { $check = 'false'; } if(get_post_meta($post->ID, 'isFeatured', true)) { ?> <input type="checkbox" id="chkFeatureMe" name="chkFeatureMe" value="true" checked="checked"> Feature this post?<br/> <? } else { ?> <input type="checkbox" id="chkFeatureMe" name="chkFeatureMe" value="true"> Feature this post?<br/> <? } } function fm_save_meta_data() { global $post; $isToBeFeatured = $_POST['chkFeatureMe']; $retval = add_post_meta($post->ID, 'isFeatured', $isToBeFeatured, true); if (!$retval) { $retval = update_post_meta($post->ID, 'isFeatured', $isToBeFeatured, true); } }
You can see I do a var_dump on the $_POST array and it outputs “array(0) { }”
This code should be able to be cut and pasted into a new plug-in as is.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘$_POST is empty after 'save_post' action’ is closed to new replies.