如何根据用户在Customizer中的选择设置Post meta-box默认值?

时间:2018-08-04 作者:Bheemsen

我在主题定制器中有一个无线电控件。该控件的代码如下所示:

$wp_customize->add_setting( \'post_layout\', array(
  \'capability\' => \'edit_theme_options\',
  \'default\' => \'cover\',
  \'sanitize_callback\' => \'sanitize_text_field\',
) );

$wp_customize->add_control( \'post_layout\', array(
  \'type\' => \'radio\',
  \'section\' => \'post_options\',
  \'label\' => __( \'Post Layout\', \'mytheme\' ),
  \'choices\' => array(
    \'cover\' => __( \'Cover\', \'mytheme\' ),
    \'thumbnail\' => __( \'Thumbnail\', \'mytheme\' ),
    \'default\' => __( \'Default\', \'mytheme\' ),
  ),
) );
负责呈现post meta框的完整代码是:

<?php
/**
 * Add the meta box
 *
 * @return void
 */
function mytheme_add_custom_box() {
  add_meta_box(
    \'mytheme_sectionid\',
    __( \'Post Options\', \'mytheme\' ),
    \'mytheme_build_custom_box\',
    \'post\',
    \'side\',
    \'low\'
  );
}
add_action( \'add_meta_boxes\', \'mytheme_add_custom_box\' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 * @return void
 */
function mytheme_build_custom_box( $post ) {

  // Add an nonce field so we can check for it later.
  wp_nonce_field( \'mytheme_build_custom_box\', \'mytheme_build_custom_box_nonce\' );

  /*
   * Use get_post_meta() to retrieve an existing value
   * from the database and use the value for the form.
   */
  $value = get_post_meta( $post->ID, \'_my_meta_value_key\', true );

  $loxxo_post_header_layout = get_theme_mod( \'loxxo_post_header\', \'cover\' );
?>


  <p><?php __( \'Select a style\', \'mytheme\' ); ?></p>
  <input type="radio" id="mytheme_post_layout-cover" name="mytheme_post_layout" value="cover" <?php checked( $value, \'cover\' ) ?> /><label for="mytheme_post_layout-cover"><?php _e( \'Cover/Fullscreen\', \'mytheme\' ); ?></label>
  <br>
  <input type="radio" id="mytheme_post_layout-thumbnail" name="mytheme_post_layout" value="thumbnail" <?php checked( $value, \'thumbnail\' ) ?> /><label for="mytheme_post_layout-thumbnail"><?php _e( \'Regular\', \'mytheme\' ) ?></label>
  <br>
  <input type="radio" id="mytheme_post_layout-default" name="mytheme_post_layout" value="default" <?php checked( $value, \'default\' ) ?> /><label for="mytheme_post_layout-default"><?php _e( \'Default\', \'mytheme\' ) ?></label>
<?php

}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 * @return void
 */
function mytheme_save_postdata( $post_id ) {

  /*
   * We need to verify this came from the our screen and with proper authorization,
   * because save_post can be triggered at other times.
   */

  // Check if our nonce is set.
  if ( ! isset( $_POST[\'mytheme_build_custom_box_nonce\'] ) )
    return $post_id;

  $nonce = $_POST[\'mytheme_build_custom_box_nonce\'];

  // Verify that the nonce is valid.
  if ( ! wp_verify_nonce( $nonce, \'mytheme_build_custom_box\' ) )
      return $post_id;

  // If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return $post_id;

  // Check the user\'s permissions.
  if ( \'page\' == $_POST[\'post_type\'] ) {

    if ( ! current_user_can( \'edit_page\', $post_id ) )
        return $post_id;

  } else {

    if ( ! current_user_can( \'edit_post\', $post_id ) )
        return $post_id;
  }

  // Sanitize user input.
  $mydata = sanitize_text_field( $_POST[\'mytheme_post_layout\'] );

  // Update the meta field in the database.
  update_post_meta( $post_id, \'_my_meta_value_key\', $mydata );
}
add_action( \'save_post\', \'mytheme_save_postdata\' );
上面的代码有点长,很抱歉。选项渲染正确,自定义程序和元框中的一切看起来都很好。

问题是,我对如何将meta-box无线电选项的默认值与定制者的同步进行感到困惑。任何线索和想法都将不胜感激。

2 个回复
最合适的回答,由SO网友:Kaperto 整理而成

在处理defaut值之前,您可以对挂钩“save\\u post”做很多更改,第一个是不再使用它
仅在post类型上添加元框post, 然后你可以用钩子save_post_post 仅对此自定义帖子进行处理:
https://developer.wordpress.org/reference/hooks/save_post_post-post_type/

之后,您无需测试功能edit_post 因为后端页面“edit.php”在保存之前进行了测试。而且您不需要测试do\\u AUTOSAVE,因为在AUTOSAVE情况下,不会发送metabox值。然后你只需要测试nonce。

最后,要在metabox中设置默认值,可以在$update === FALSE 当您单击“创建新帖子”链接时,会调用该链接。

然后尝试以下代码:

add_action("save_post_post", function ($post_ID, $post, $update) {


    if (!$update) {

        // default values here

        $mydata = get_theme_mod( \'post_layout\', \'cover\' );
        update_post_meta( $post_ID, \'_my_meta_value_key\', $mydata );

        return;

    }


    if ( ! isset( $_POST[\'mytheme_build_custom_box_nonce\'] ) ) {
        return;
    }

    // check the nonce
    check_admin_referer(
          "mytheme_build_custom_box"
        , "mytheme_build_custom_box_nonce"
    );


    // save the values

    // Sanitize user input.
    $mydata = sanitize_text_field( $_POST[\'mytheme_post_layout\'] );

    // Update the meta field in the database.
    update_post_meta( $post_ID, \'_my_meta_value_key\', $mydata );


}, 10, 3);

SO网友:Jacob Peattie

您将在此处获得保存的值:

$value = get_post_meta( $post->ID, \'_my_meta_value_key\', true );
您只需检查该值是否包含任何内容。如果没有,请将值设置为customiser中的选项:

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

if ( ! $value ) {
    $value = get_theme_mod( \'loxxo_post_header\', \'cover\' );
}
可以在一行代码中使用ternary operator (?:):

$value = get_post_meta( $post->ID, \'_my_meta_value_key\', true ) ?: get_theme_mod( \'loxxo_post_header\', \'cover\' );

结束