尝试限制对自定义元框的访问但未成功

时间:2012-11-13 作者:Amanda Duke

我已将此元框添加到我的网站:

// Assistant Editor Box
add_action( \'add_meta_boxes\', \'assistant_editor_box\' );
function assistant_editor_box() {
    add_meta_box(
        \'assistant_editor_box\', // id, used as the html id att
        __( \'Assistant Editor\' ), // meta box title
        \'assistant_editor_cb\', // callback function, spits out the content
        \'post\', // post type or page. This adds to posts only
        \'side\', // context, where on the screen
        \'high\' // priority, where should this go in the context
    );
}

function assistant_editor_cb( $post ) {
    global $wpdb;
    $value = get_post_meta($post->ID, \'proofread\', true);
    echo \'<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Proofread: </label>\';

    $assedit = array(
        1 => \' Yes \',
    );

    echo \'<select name="proofread">\';
    echo \'<option value=""\' . ((($value == \'\') || !isset($assedit[$value])) ? \' selected="selected"\' : \'\') . \'> No </option>\';

    // output
    foreach ($assedit as $id => $text) {
        echo \'<option value="\' . $id . \'"\' . (($value == $id) ? \' selected="selected"\' : \'\') . \'">\' . $text. \'</option>\';
    }
    echo \'</select>\';

    echo \'</span></div>\';
}

add_action( \'save_post\', \'save_metadata\');

function save_metadataeditass($postid)
{   
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( \'edit_page\', $postid ) ) return false;
    if( empty($postid) ) return false;


    if ( is_null($_REQUEST["proofread"]) ) {
        delete_post_meta($postid, \'proofread\');
    } else {
        update_post_meta($postid, \'proofread\', $_REQUEST[\'proofread\']);
    }

}
// END Assistant Editor Box
我想根据能力限制对它的访问。我尝试了以下代码:

function remove_assistant_editor_box_meta() { 
   if (!current_user_can(\'edit_others_posts\')){
       remove_meta_box( \'assistant_editor_box\', \'post\', \'side\' );
   }
}

add_action( \'admin_menu\' , \'remove_assistant_editor_box_meta\' );
不起作用,也尝试了以下操作:

function remove_assistant_editor_box_meta(){
    if(function_exists(\'assistant_editor_box\') && !current_user_can(\'edit_others_posts\')){
        add_filter(\'assistant_editor_box\', \'__return_false\');
    }   
}
add_action(\'init\', \'remove_assistant_editor_box_meta\');
根据Wordpress文档,第一个代码应该可以工作,what in the world am I doing wrong here?

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

正如@totels所说,只需应用条件when you add the box, 而不是稍后再尝试删除它(仅供参考,它不起作用,因为您在较早触发的挂钩上调用了删除!)

add_action( \'add_meta_boxes\', \'assistant_editor_box\' );
function assistant_editor_box() {
    if ( current_user_can( \'edit_others_posts\' ) )
        add_meta_box(
            \'assistant_editor_box\',
            ...
        )
}

结束

相关推荐

删除Yoast SEO帖子Metabox

Yoasts SEO插件在后期编辑屏幕中添加了一个元框。我正在尝试为非编辑或更高级别的用户删除此项。我试着把remove_meta_box 在admin\\u init上调用,尝试删除$wpseo\\u metabox上的操作,但没有效果。如何在不需要用户干预的情况下删除此元数据库(用户永远不会知道该元数据库存在,因此单击屏幕选项不是一个选项)