Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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() {
    add_meta_box( 
        'testimonial_text_box',
        __( 'Testimonial Text:', 'myplugin_textdomain' ),
        'testimonial_text_box_content',
        'testimonial',
        'normal',
        'high'
    );
}

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 ) 
    return;

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

    if ( 'testimonial' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) )
        return;
    } else {
        if ( !current_user_can( 'edit_post', $post_id ) )
        return;
    }
    $testimonial_text = $_POST['testimonial_text'];
    update_post_meta( $post_id, 'testimonial_text', $testimonial_text );
}
share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.