I am trying to add a custom field to all my posts and of course saving/reading the value. As a starting point I took the example from the Wordpress Codex and stripped it down to bare bones. I know it know has serious security flaws, I am just trying to understand it.

The problem I am facing at the moment is that nothing is saved. After looking at what is POSTed when the post is saved, I noticed that the input's key & value is not present in the $_POST array at all. The property exists in the database, but without value of course.

I also noticed that the variable $post passed to the add_input is empty, but I don't why (I guess this is the problem?).

This is the code from my functions.php:

function add_custom_field() {
    add_meta_box( 'my_field', 'My Field', 'add_input', 'post' );
add_action( 'add_meta_boxes', 'add_custom_field' );

function add_input( $post ) {
    $value = get_post_meta( $post->ID, 'my_key', true );
    echo '<input type="text" value="' . $value . '" id="my_id">';

function save( $post_id ) {    
    $mydata = sanitize_text_field( $_POST['my_id'] );
    update_post_meta( $post->ID, 'my_key', $mydata );
add_action( 'save_post', 'save' );

Do you see any errors in the code above? Do you have any comments to my info I gave you? Why is nothing POSTed to the server?

  • 1
    To pass value when post you have to set the 'name' attribute on inout: '<input type="text" value="' . $value . '" name="my_id" id="my_id">';
    – gmazzap
    Sep 11, 2013 at 19:33
  • your input needs a name attribute. id's are for styling/javascript name value pairs get sent to $_GET and $_POST
    – Twifty
    Sep 11, 2013 at 19:34
  • looks like @G.M. beat me to it :p
    – Twifty
    Sep 11, 2013 at 19:35
  • Search this site for examples of save_post, you're missing lots of things. Your hook is running in places where it shouldn't.
    – brasofilo
    Sep 12, 2013 at 11:48
  • @brasofilo Do you mean security wise? Or because I have that code in my functions.php?
    – Sven
    Sep 12, 2013 at 18:41

1 Answer 1


You have two issues:

1.- Your are not passing the correct post ID. To solve this problem change:

update_post_meta( $post->ID, 'my_key', $mydata );


update_post_meta( $post_id, 'my_key', $mydata );

2.- Your are not createing the input correctly, the name attribute is missed:


echo '<input type="text" value="' . $value . '" id="my_id">';


echo '<input type="text" value="' . $value . '" id="my_id" name="my_id">';
  • Thank you, now it works! But one question: I haven't defined $post_id anywhere. So I guess this variable is always available when a post is edited? Also for pages, is it also $post_id? I haven't found anything in the Codex, maybe there is a more general term for it?
    – Sven
    Sep 12, 2013 at 6:05
  • The $post_id is the parameter of the function, as you can see function save( $post_id ). This parameter is passed by Wordpress to the callback function of the save_post hook and it is not a defined variable everywhere while editing a post.
    – cybmeta
    Sep 12, 2013 at 7:30
  • Ah ok thank you. But then it's a defined behavior by Wordpress to pass that information to the function in the callback, independent from the variable name?
    – Sven
    Sep 12, 2013 at 18:24
  • You define the name of the callback function and the name of the variable passed as parameter of this callback function. But the value of this parameter is passed by Wordpress, in the case of the save_post hook the value is the ID of the post being saved.
    – cybmeta
    Sep 12, 2013 at 20:03

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.