dsarttv037029 2017-08-13 18:52
浏览 55
已采纳

WordPress自定义元框不会保存

I have the following code which adds a custom meta box to wordpress pages but for some reason it wont save the selected option. Any ideas whats wrong ?

Thanks.

 // Add Header Select Option MetaBox
function alfie_header_select_meta() {
add_meta_box( 'alfie_header_select_meta', __( 'Header Style', 'alfie' ), 'alfie_header_select_meta_callback', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'alfie_header_select_meta' );

function alfie_header_select_meta_callback( $post ) {
$values = get_post_custom( $post->ID );
$selected = isset( $values['alfie_selected_header'] ) ? esc_attr( $values['alfie_selected_header'][0] ) : ”;
wp_nonce_field( 'alfie_header_select_meta_nonce', 'alfie_header_select_meta_nonce' );
?>
  <p>
      <select name="alfie_selected_header" id="alfie_selected_header">
          <option value="alfie-header-default" <?php selected( $selected, 'default' ); ?>>Default</option>
          <option value="alfie-header-style-minimal" <?php selected( $selected, 'minimal' ); ?>>Minimal</option>
          <option value="alfie-header-test" <?php selected( $selected, 'test' ); ?>>Just a test option</option>
      </select>
  </p>
<?php
}

function alfie_meta_save( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'alfie_header_select_meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'alfie_header_select_meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}

if( isset( $_POST[ 'alfie_selected_header' ] ) ) {
    update_post_meta( $post_id, 'alfie_selected_header', sanitize_text_field( $_POST[ 'alfie_selected_header' ] ) );
}
}
add_action( 'save_post', 'alfie_meta_save' );
  • 写回答

1条回答 默认 最新

  • doutang1884 2017-08-13 20:18
    关注

    Here is the working debugged code. The post meta was being saved. But it wasnt displaying because of a difference in the string values and how you got the meta. Just use get_post_meta for a single field.

     // Add Header Select Option MetaBox
    function alfie_header_select_meta() {
    add_meta_box( 'alfie_header_select_meta', __( 'Header Style', 'alfie' ), 'alfie_header_select_meta_callback', 'page', 'side' );
    }
    add_action( 'add_meta_boxes', 'alfie_header_select_meta' );
    
    function alfie_header_select_meta_callback( $post ) {
    $selected = get_post_meta( $post->ID,'alfie_selected_header',true);
    wp_nonce_field( 'alfie_header_select_meta_nonce', 'alfie_header_select_meta_nonce' );
    ?>
      <p>
          <select name="alfie_selected_header" id="alfie_selected_header">
              <option value="alfie-header-default" <?php selected( $selected, 'alfie-header-default' ); ?>>Default</option>
              <option value="alfie-header-style-minimal" <?php selected( $selected, 'alfie-header-style-minimal' ); ?>>Minimal</option>
              <option value="alfie-header-test" <?php selected( $selected, 'alfie-header-test' ); ?>>Just a test option</option>
          </select>
      </p>
    <?php
    }
    
    function alfie_meta_save( $post_id ) {
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'alfie_header_select_meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'alfie_header_select_meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
    
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }
    
    if( isset( $_POST[ 'alfie_selected_header' ] ) ) {
        update_post_meta( $post_id, 'alfie_selected_header', sanitize_text_field( $_POST[ 'alfie_selected_header' ] ) );
    }
    }
    add_action( 'save_post', 'alfie_meta_save' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看