开发者

How do I make my custom WordPress meta box visible to admins only?

开发者 https://www.devze.com 2023-03-07 23:02 出处:网络
I am using my functions.php to add a custom meta box on my posts page in the WordPress Admin Area. However, I need to make it so i开发者_如何学Gots only visible to admins, and not editors, contributor

I am using my functions.php to add a custom meta box on my posts page in the WordPress Admin Area. However, I need to make it so i开发者_如何学Gots only visible to admins, and not editors, contributors, etc.

What would I do to make it visible to admins only?


function your_function() {
    global $current_user;
    if($current_user->roles[0] == 'administrator') {
        add_meta_box(your parameters);
        // fill in your parameters
    }
}
add_action('admin_init','your_function');


if ( is_user_logged_in() ) {
    get_currentuserinfo();
    # check if current user is admin
    if ( $current_user->wp_user_level >= 10 ) {
        # put your admin-only function here
    }
}


This snippet works for custom taxonomies. It removes / hides a custom taxonomy meta box for all non-admins, assuming no other role has the update_core capability. Similar, but opposite of the answer by @johannes-pille

function remove_tax_metaboxes() {
    if (!current_user_can('update_core')) {
        remove_meta_box( 'taxdiv', 'post', 'side' );
    }
}
add_action( 'do_meta_boxes', 'remove_tax_metaboxes' );

Note that the third argument of remove_meta_box may differ, see https://codex.wordpress.org/Function_Reference/remove_meta_box

0

精彩评论

暂无评论...
验证码 换一张
取 消