【问题标题】:WordPress custom meta box wont saveWordPress自定义元框不会保存
【发布时间】:2017-08-14 05:01:04
【问题描述】:

我有以下代码,它向 wordpress 页面添加了一个自定义元框,但由于某种原因,它不会保存选定的选项。有什么想法有什么问题吗?

谢谢。

 // 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' );

【问题讨论】:

  • 它可能在 alfie_meta_save() 内的自动保存/随机数检查中。你有没有看到代码是否在 if 块之后运行?你应该测试一下看看。
  • 对不起,我是笨蛋,但我该怎么做?
  • 一个想法是在 if 块之后保存一个默认元数据并检查它是否被添加。
  • 使用:update_post_meta($post_id, 'alfie_selected_header', 'yay');就在 if 块之后看看。我只是告诉你一个快速简便的调试方法
  • 谢谢,我添加了它,但没有任何改变,保存后它只是恢复默认

标签: javascript php jquery wordpress


【解决方案1】:

这里是工作调试代码。 帖子元被保存。但由于字符串值的不同以及获取元数据的方式不同,它没有显示。只需对单个字段使用 get_post_meta。

 // 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' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 2015-06-23
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多