Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this meta box that I have added to my site:

// 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

I want to limit access to it based on Capability. I tried the following code:

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

Did not work, tried this as well:

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

According to the Wordpress documents, the first code should work, what in the world am I doing wrong here?

share|improve this question
    
Is there any reason you can't add the permission check to the assistant_editor_box() function around or before add_meta_box()? –  totels Nov 13 '12 at 16:37
    
I would not know how to be honest, do you? –  Amanda Duke Nov 13 '12 at 16:41
    
Is the plugin creating the meta box your own? Or is it a 3rd-party plugin that may be updated and overwrite your edits? –  totels Nov 13 '12 at 17:04
add comment

1 Answer

up vote 2 down vote accepted

As @totels says, just apply the condition when you add the box, rather than trying to remove it later (FYI, it's not working because you call the removal on hooks that fire earlier!)

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',
            ...
        )
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.