3

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 until it is updated again. 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 );
}
4

1 回答 1

1

您的代码存在问题:

  1. 不必要的块(它与手头的目标无关):

    $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] ) : ”;  
    
  2. 您正在节省testimonial_text

    update_post_meta( $post_id, 'testimonial_text', $testimonial_text );
    

    但得到_my_meta_value_key

    $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
    

    将 更改get_testimonial_text

  3. Atextarea没有value,内容位于打开/关闭标签内:

    echo '<textarea id="testimonial_text" name="testimonial_text" />'.esc_attr($value).'</textarea>';
    
  4. 钩子save_post有 2 个参数:

    add_action( 'save_post', 'testimonial_text_box_save', 10, 2 );
    function testimonial_text_box_save( $post_id, $post ) { /* code */ }
    
  5. 有一个不工作的 if/else 应该是这样的:

    if ( 'testimonial' !== $post->post_type )        
        return;
    
    if ( !current_user_can( 'edit_post' ) )
        return;
    

要在前端使用 post meta,只需使用get_post_meta().

于 2014-06-19T08:57:28.407 回答