1

I am trying to get the input from a textbox and display it upon update.

I don't know if the update_post_meta or get_post_meta is causing the problem.

Code

//Metabox Hook
add_action('add_meta_boxes','stars_meta_box');
//Metabox Init
function stars_meta_box(){
    global $post;
    add_meta_box('first_name_meta_box',
    'First Name','first_name_meta_box_html','spark_stars','normal','default');
}
//Metabox Html
function first_name_meta_box_html(){
    wp_nonce_field('first_name','first_name_meta_box_nonce');
    $value = get_post_meta( $post->ID, 'first_name_meta_box_key', true ); ?>
    <label>First Name: </label> <input type="text" name="fname" value="<?php echo $value; ?>"/>
<?php }

//Save Hook
add_action( 'save_post', 'save_meta_box' );
//Save Metabox Value
function save_meta_box(){
    global $post;
    update_post_meta( $post->ID, "first_name_meta_box_key", $_POST["fname"]); 
} 

The problem is that when I type a value in the textbox and update the page, the textbox remains blank.

2
  • and the problem is...try to make your problem explicit
    – Tomás Cot
    Commented Sep 17, 2014 at 14:37
  • The problem is that upon updating the page, the textbox is still blank
    – Femi
    Commented Sep 17, 2014 at 14:39

1 Answer 1

2

Try with this code,

    //Metabox Hook
add_action('add_meta_boxes','stars_meta_box');
//Metabox Init
function stars_meta_box(){

    add_meta_box('first_name_meta_box',
    'First Name','first_name_meta_box_html','spark_stars','normal','default'););
}
//Metabox Html
function first_name_meta_box_html($post){
    wp_nonce_field('first_name','first_name_meta_box_nonce');
    $value = get_post_meta( $post->ID, 'first_name_meta_box_key', true );

    echo '<label>First Name: </label> <input type="text" name="fname" value="' . esc_attr($value) . '"/>';
 }

//Save Hook
add_action( 'save_post', 'my_save_meta_box' );
//Save Metabox Value
function my_save_meta_box($post_id){
    //you might want this or not
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    if ( isset($_POST['first_name_meta_box_nonce']) && ! wp_verify_nonce( $_POST['first_name_meta_box_nonce'], 'first_name' ) ) {
        return;
    }



    if(!isset($_POST["fname"])){
        return;

    }
   update_post_meta( $post_id, "first_name_meta_box_key", sanitize_text_field($_POST["fname"])); 
}

In the save function I added a couple of checks, the first one is to check if is an "AUTOSAVE", which triggers when a draft is saved, if you want to save the value of the field even on "AUTOSAVE", then delete that block.

Then, I check if the "nonce" is valid.

Then I check if the field wasn't empty.

And finally, I save the value if everything was ok, you can add something else or change the logic, but those are a few basic things to check for.

Parameters

You can see that I used parameter in the functions instead of the global "Post" object.

Data sanitization

I also added a few function to sanitize the data, with the sanitization on the save function should be enough, but, is so simple that I also escaped the value in the meta box generator function.

I think that's it, if you have any question, just ask.

3
  • Thank you for your answer and the security. But it turns out that the reason why it was not displaying the entered value was because it was not getting the post id correct. The global $post only works in loops so the $post->ID was empty. I got the post id using get_the_ID()
    – Femi
    Commented Sep 17, 2014 at 16:25
  • well, in reality, get_the_ID() only works on loops, and uses the global post object, so it's almost the same.
    – Tomás Cot
    Commented Sep 17, 2014 at 16:49
  • True. You're right. You put $post inside the parameter and referenced it using $post->ID. It wasn't working because I didn't put $post in the parameter. Also, global $post works if you declare it inside the function too. Thanks for your help Tomás.
    – Femi
    Commented Sep 17, 2014 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.