WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

All I really want is for the meta box data to be saved A) as an accessible global variable inside the loop and B) to save the data to the textbox so that when the user presses update what they have written appears in the textbox once updated. Currently, i know it doesn't fit the criteria for B), and I'm not sure whether or not it is accessible as a global variable in the loop. Any help?

add_action( 'add_meta_boxes', 'testimonial_text_box' );

function testimonial_text_box() {
        __( 'Testimonial Text:', 'myplugin_textdomain' ),

function testimonial_text_box_content( $post ) {
    $values = get_post_custom( $post->ID );  
    $text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : ”;  
    $selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;  
    $check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'][0] ) : ”;  

    wp_nonce_field( plugin_basename( __FILE__ ), 'testimonial_text_box_content_nonce' );
  $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
  echo '<label for="testimonial_text">';
       _e("Text body of the testimonial:", 'myplugin_textdomain' );
  echo '</label> ';
    echo '<br/>';
  echo '<textarea align="top" id="testimonial_text" name="testimonial_text" value="'.esc_attr($value).'" style="width:100%;height:200px;margin:5px -20px 3px 0;" /></textarea>';

add_action( 'save_post', 'testimonial_text_box_save' );
function testimonial_text_box_save( $post_id ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 

    if ( !wp_verify_nonce( $_POST['testimonial_text_box_content_nonce'], plugin_basename( __FILE__ ) ) )

    if ( 'testimonial' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) )
    } else {
        if ( !current_user_can( 'edit_post', $post_id ) )
    $testimonial_text = $_POST['testimonial_text'];
    update_post_meta( $post_id, 'testimonial_text', $testimonial_text );
share|improve this question
up vote 1 down vote accepted

What you are trying to do is not going to work the way you are trying to do it because on post save WordPress will process the data and then redirect back to the originating page. That means that any global you set before the redirect isn't going to be there afterwards.

However, your custom meta information should be retrieved with this-- $values = get_post_custom( $post->ID );-- so your should have no trouble having that information to use in your testimonial_text_box_content and the same line of code would get the values (all custom post meta) inside the Loop on the front end.

When you echo your textarea you want to include your previous content between the opening and closing tags, like with paragraph tag. It is different in this way from an input form element.

echo '<textarea align="top" id="testimonial_text" name="testimonial_text" value="'.esc_attr($value).'" style="width:100%;height:200px;margin:5px -20px 3px 0;" />'.$values['testimonial_text'].'</textarea>';

I am working from memory here and can't test at the moment, so there may be bugs in that but hopefully it will move you forward.

Other than that, I didn't test the code but I don't spot anything obviously wrong, by the way.

share|improve this answer
So there's no way for the text typed into the textarea to immediately appear in the textarea once I've pressed update? That's very strange... How does the default editor do it then? – john Doe Jul 23 '13 at 20:56
Yes, you use the data in $values. I did just spot a problem though. – s_ha_dum Jul 23 '13 at 20:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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