一个帖子影响另一个帖子设置的Metabox

时间:2015-03-02 作者:Bram Vanroy

This great answer David Gard让我有一个代谢箱sticky (复选框)用于一次只能在循环中的一个帖子上设置(选中)的帖子。如果帖子A选中了那个元框,那么B,C。。。Z不能检查它。为了能够选中其他帖子中的框,用户必须首先取消选中帖子A中的框,然后才能选中其他框中的框。这本身就很有效。然而,我在使用其他元框(文本输入字段)时遇到了问题。每个帖子都有三个元数据库:sticky 和两个输入文本字段reviewsource (其他可能稍后添加)。

假设帖子Asticky 选中,值为reviewsource 没关系,它们甚至可以是空的。保存这篇文章效果很好,我可以随意更改所有三个元框的值,没有任何错误或错误行为。但是,当我编辑sourcereview 然后突然保存该帖子sticky 在帖子A中未设置。我不明白这些事情是如何联系在一起的。以下是三个元框的代码:

/*
 * STICKY POSTS
 *
*/

function add_sticky_metabox(){
    add_meta_box(
        \'sticky_post_metabox\', \'Sticky Post\', \'output_sticky_metabox\', \'post\'
    );
}
add_action(\'add_meta_boxes\', \'add_sticky_metabox\');

// Make a post sticky
function output_sticky_metabox($post){

    /** Grab the current \'my_sticky_post\' option value */
    $sp = intval(get_option(\'sticky_post\'));

    /** Check to see if the \'my_sticky_post\' option should be disabled or checked for the current Post */
    $checked = checked($sp, $post->ID, false);
    if($sp > 0) :
        $disabled = (!disabled($sp, $post->ID, false)) ? \'disabled="true"\' : \'\';
    else :
        $disabled = \'\';
    endif;

    /** Add a nonce field */
    wp_nonce_field(\'sticky_post_metabox\', \'sticky_post_metabox_nonce\');

    /** Add a hidden field to check against in case it is unchecked before save */
    $value = ($checked) ? \'1\' : \'0\';
    echo \'<input type="hidden" name="was_checked" value="\' . $value . \'" />\';

    /** Output the checkbox and label */
    echo \'<label for="sticky_post">\';
    echo \'<input type="checkbox" id="sticky_post" name="sticky_post" value="\' . $post->ID . \'" \' . $checked . $disabled . \'>\';
    echo \'Make highlight?</label>\';

    /** Let the user know which Post is currently sticky */
    switch($sp) :

        case 0:
            $message = \'There\\\'s no highlight.\';
            break;
        case $post->ID:
            $message = \'This post is the highlight!\';
            break;
        default:
            $message = \'<a href="\' . get_edit_post_link($sp) . \'" title="\' . the_title_attribute(\'before=Bewerk bericht \\\'&after=\\\'&echo=0\') . \'">\' . get_the_title($sp) . \'</a> is currently lit\';;

    endswitch;
    echo \'<p><em>\' . $message .\'</em></p>\';

}


function save_sticky_metabox($post_id){
    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    /** Ensure that a nonce is set */
    if(!isset($_POST[\'sticky_post_metabox_nonce\'])) :
        return;
    endif;

    /** Ensure that the nonce is valid */
    if(!wp_verify_nonce( $_POST[\'sticky_post_metabox_nonce\'], \'sticky_post_metabox\')) :
        return;
    endif;

    /** Ensure that an AUTOSAVE is not taking place */
    if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) :
        return;
    endif;

    /** Ensure that the user has permission to update this option */
    if(!current_user_can(\'edit_post\', $post_id)) :
        return;
    endif;

    /**
     * Everything is valid, now the option can be updated
     */

    /** Check to see if the \'my_sticky_post\' option was checked */
    if(isset($_POST[\'sticky_post\'])) : // It was...

        update_option(\'sticky_post\', $_POST[\'sticky_post\']);  // Update the option

    else : // It was not...

        /** Check to see if the option was checked prior to the options being updated */
        if(isset($_POST[\'was_checked\'])) : // It was...

            update_option(\'sticky_post\', 0); // Set the option to \'0\'

        endif;

    endif;

}
add_action(\'save_post\', \'save_sticky_metabox\');


/*
 * Source
 *
*/

function add_source_metabox(){
    add_meta_box(
        \'source_post_metabox\', \'Bron\', \'output_source_metabox\', \'post\'
    );
}
add_action(\'add_meta_boxes\', \'add_source_metabox\');

function output_source_metabox($post){
    wp_nonce_field(\'source_post_metabox\', \'source_post_metabox_nonce\');
    $post_source = $post->post_source;

    echo \'<label for="source_post">\';
    echo \'<input type="text" id="source_post" name="source_post" value="\'.$post_source.\'" style="width: 80%;max-width: 720px;">\';
    echo \' Add a source to your post.</label>\';
    echo \'<p>E.g. <em>http://tweakers.net/nieuws/101372/ing-belgie-wil-betalingsgedrag-van-klanten-meer-gebruiken-voor-dienstverlening.html</em></p>\';

}
function save_source_metabox($post_id){
    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    /** Ensure that a nonce is set */
    if(!isset($_POST[\'source_post_metabox_nonce\'])) :
        return;
    endif;

    /** Ensure that the nonce is valid */
    if(!wp_verify_nonce( $_POST[\'source_post_metabox_nonce\'], \'source_post_metabox\')) :
        return;
    endif;

    /** Ensure that an AUTOSAVE is not taking place */
    if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) :
        return;
    endif;

    /** Ensure that the user has permission to update this option */
    if(!current_user_can(\'edit_post\', $post_id)) :
        return;
    endif;

    // Update and save the field so it can be used in our template
    if ( isset( $_POST[\'source_post\'] ) ) {
        $data = sanitize_text_field( $_POST[\'source_post\'] );
        update_post_meta( $post_id, \'post_source\', $data );
    }

}
add_action(\'save_post\', \'save_source_metabox\');


/*
 * Reviews name field
 *
*/

function add_review_metabox(){
    add_meta_box(
        \'review_post_metabox\', \'Review\', \'output_review_metabox\', \'post\'
    );
}
add_action(\'add_meta_boxes\', \'add_review_metabox\');

function output_review_metabox($post){
    wp_nonce_field(\'review_post_metabox\', \'review_post_metabox_nonce\');
    $post_review = $post->post_review;

    echo \'<label for="review_post">\';
    echo \'<input type="text" id="review_post" name="review_post" value="\'.$post_review.\'" style="width: 80%;max-width: 720px;">\';
    echo \' Add the name of the reviewed product.</label>\';
    echo \'<p>E.g. <em>Lumia 930</em></p>\';

}
function save_review_metabox($post_id){
    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    /** Ensure that a nonce is set */
    if(!isset($_POST[\'review_post_metabox_nonce\'])) :
        return;
    endif;

    /** Ensure that the nonce is valid */
    if(!wp_verify_nonce( $_POST[\'review_post_metabox_nonce\'], \'review_post_metabox\')) :
        return;
    endif;

    /** Ensure that an AUTOSAVE is not taking place */
    if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) :
        return;
    endif;

    /** Ensure that the user has permission to update this option */
    if(!current_user_can(\'edit_post\', $post_id)) :
        return;
    endif;

    // Update and save the field so it can be used in our template
    if ( isset( $_POST[\'review_post\'] ) ) {
        $data = sanitize_text_field( $_POST[\'review_post\'] );
        update_post_meta( $post_id, \'post_review\', $data );
    }

}
add_action(\'save_post\', \'save_review_metabox\');
因此,在保存一篇帖子时,这会影响sticky 其他人的。但我不明白这是怎么发生的,也不明白为什么会发生。有什么想法吗?

1 个回复
最合适的回答,由SO网友:David Gard 整理而成

这里的问题是原始代码中有一个小错误。

接近底部save_sticky_metabox() 回调,更改行-

if(isset($_POST[\'was_checked\'])) :
到这个-

if($_POST[\'was_checked\'] != 0) :
Thewas_checked 值是从隐藏字段而不是复选框派生的,因此无论值是多少,它都将始终被设置。因此,我们需要检查该值是否不等于0, 而不是仅仅检查它是否已设置。

还有一些提示-

你应该在字符串中使用转义引号和撇号字符Correct - \'没有突出显示。”

  • Incorrect - \'没有亮点。”(这将导致PHP错误)
  • 在创建2个Posteta字段时,请考虑使用_ (\'_post_source\'\'_post_review\'). 这样做意味着Posteta将被“隐藏”,并且不会出现在Custom Fields 编辑帖子页面上的metabox
  • 如果您愿意,您可以通过一个回调来添加所有3个元数据库,而不是一个一个(但这是个人喜好,按自己的方式做没什么错)
    1. 以备不时之需-

      function add_post_metaboxs(){
      
          add_meta_box(\'sticky_post_metabox\', \'Sticky Post\', \'output_sticky_metabox\', \'post\');
          add_meta_box(\'source_post_metabox\', \'Bron\', \'output_source_metabox\', \'post\');
          add_meta_box(\'review_post_metabox\', \'Review\', \'output_review_metabox\', \'post\');
      
      }
      

    结束

    相关推荐

    通过自定义角色添加到CPT管理的默认分类‘post_tag’:metabox中没有任何内容

    我创建了一个自定义帖子类型,自定义角色可以管理它:它只有管理这个CPT的功能。注册CPT时,我设置了参数:“taxonomies”=>数组(“post\\u tag”),但当我以自定义角色的用户身份登录此CPT时,我可以看到元框“Tags”,但只有标题标题,框中没有任何内容。我没有找到任何将其提供给自定义角色的功能。。。。如何修复此问题?Thx!