• 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)
  • I’m having this problem as well. What’s particularly weird about it is that plugin values and my post are actually updating, except the one that I am working on. Using godaddy shared virtual hosting.

    Hi,

    POST should not exist on post edit page.
    WP uses the “redirect to view after action” scenario which is only right way to handle POST requests. Other words:
    1. handle POST data, save post
    2. redirect to /wp-admin/post.php?post=YourpostID&action=edit
    Your “display_post_meta_boxes” works after second stage so POST doesnt exists.
    You can get your value using get_post_meta if they was properly saved on save_post action.

    Thanks, I was actually coming in here to post just that. Feeling pretty dumb for not catching that myself the first time around, took emailing myself the values from the save action to realize what was going on :-/

    If anyone needs to see what values are making it to the save function you simply pass the entire post array to the add/update post meta functions as a temporary value. It will be serialized and stored. This will store a lot of redundant information however.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘$_POST is empty after 'save_post' action’ is closed to new replies.