0

I have a custom metabox made using the code from Wordpress Reference:

http://codex.wordpress.org/Function_Reference/add_meta_box

<?php
/* Define the custom box */

// WP 3.0+
// add_action('add_meta_boxes', 'myplugin_add_custom_box');

// backwards compatible
add_action('admin_init', 'myplugin_add_custom_box', 1);

/* Do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
    add_meta_box( 
        'myplugin_sectionid',
        __( 'My Post Section Title', 'myplugin_textdomain' ),
        'myplugin_inner_custom_box',
        'post' 
    );
    add_meta_box(
        'myplugin_sectionid',
        __( 'My Post Section Title', 'myplugin_textdomain' ), 
        'myplugin_inner_custom_box',
        'page'
    );
}

/* Prints the box content */
function myplugin_inner_custom_box() {

  // Use nonce for verification
  wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );

  // The actual fields for data entry
  echo '<label for="myplugin_new_field">';
       _e("Description for this field", 'myplugin_textdomain' );
  echo '</label> ';
  echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="whatever" size="25" />';
}

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) ) )
      return $post_id;

  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
      return $post_id;


  // Check permissions
  if ( 'page' == $_POST['post_type'] ) 
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return $post_id;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }

  // OK, we're authenticated: we need to find and save the data

  $mydata = $_POST['myplugin_new_field'];

  // Do something with $mydata 
  // probably using add_post_meta(), update_post_meta(), or 
  // a custom table (see Further Reading section below)

   return $mydata;
}
?>

And I'm not sure how to display it's value on each page?

<?php 

    $meta = get_post_meta($post->ID, 'myplugin_new_field'); 

    var_dump($meta);

?>

Gives:

array(0) {}

In addition, metabox field doesn't update it's value after clicking updating post/or page. That's because input value="whathever" while it should be something like the code above.

Any ideas?

Thank you!

[edit]

Final code that is not updating inputs value:

/* Define the custom box */
add_action('add_meta_boxes', 'myplugin_add_custom_box');

// backwards compatible
add_action('admin_init', 'myplugin_add_custom_box', 1);

/* Do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
    add_meta_box( 
        'metabox_sidebar_select',
        __( 'My Post Section Title', 'myplugin_textdomain' ),
        'myplugin_inner_custom_box',
        'post' 
    );
    add_meta_box(
        'metabox_sidebar_select',
        __( 'My Post Section Title', 'myplugin_textdomain' ), 
        'metabox_sidebar_select',
        'page'
    );
}

/* Prints the box content */
function myplugin_inner_custom_box() {

  // Use nonce for verification
  wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );

  // The actual fields for data entry
  echo '<label for="myplugin_new_field">';
       _e("Description for this field", 'myplugin_textdomain' );
  echo '</label> ';
  echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="'.get_post_meta($post->ID, 'myplugin_new_field',true).'" size="25" />';  
}

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) ) )
      return $post_id;

  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
      return $post_id;


  // Check permissions
  if ( 'page' == $_POST['post_type'] ) 
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return $post_id;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }

  // OK, we're authenticated: we need to find and save the data

  $mydata = $_POST['myplugin_new_field'];

  // Do something with $mydata 
  // probably using add_post_meta(), update_post_meta(), or 
  // a custom table (see Further Reading section below)

  global $post;
  update_post_meta($post->ID, myplugin_new_field, $mydata);

   return $mydata;
}
3

1 Answer 1

2

there is you answer, in your code where it says:

// Do something with $mydata probably using add_post_meta(), update_post_meta(), or a custom table (see Further Reading section below)

you need to actually insert/update the data to the database so add something like:

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

and the data will be saved so you could get it using your code:

$meta = get_post_meta($post->ID, 'myplugin_new_field'); 

and the only other change you need is in the function that displays the metabox change:

echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="whatever" size="25" />';

to

echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="'.get_post_meta($post->ID, 'myplugin_new_field',true).'" size="25" />';

Update:

To answer your question in the comments:

to save a select list you save it just the save way as a text input and to display the previously selected option in the meta box you just loop over the option and see what was selected and you add "selected" attribute. for example :

//get last selected value if exists
$selected = get_post_meta($post->ID, 'myplugin_new_select_field',true);
echo '<select name="myplugin_new_select_field">';
$x = 0;
while ($x < 4){
    echo '<option value="'.$x.'"';
    //check if the last value is the same is current value.
    if ($x == $selected)
        echo ' selected="selected"';
    echo '>'.$x.'</option>';
}
echo '</select>';
9
  • @Bainternet, Christ, I'm blind & stupid. I've just copy pasted code and get rid off comments immediately :/ Thank you so much. ANYWAYS the last line doesn't work (input values are not being updated, inputs are always empty). Any idea why? Commented Mar 29, 2011 at 22:51
  • 1
    @Wordpressor: paste bin your code and i'll tell you.
    – Bainternet
    Commented Mar 30, 2011 at 0:26
  • 1
    @Wordpressor: add 'global $post;' to myplugin_inner_custom_box() function, and add ' marks to update_post_meta($post->ID, 'myplugin_new_field', $mydata);
    – Bainternet
    Commented Mar 30, 2011 at 12:39
  • 1
    @Wordpressor: i updated my answer.
    – Bainternet
    Commented Mar 30, 2011 at 13:28
  • 1
    @Wordpressor: glad you got it.
    – Bainternet
    Commented Mar 30, 2011 at 14:04

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.