admin管理员组

文章数量:1301514

I am writing a function to add a link to the Media Library in the admin menu on front end:

function add_media_link_to_admin_menu( $wp_admin_bar ) {

    // add Media Library to admin menu
        $wp_admin_bar->add_menu( array(
            'parent' => 'appearance',
            'id'     => 'media-library',
            'title'  => 'Media Library',
            'href'   => '/wp-admin/upload.php',
        ) );
        
}

// restrict to only users who can upload media
if ( !current_user_can( 'upload_files' ) ) {

    add_action( 'admin_bar_menu', 'add_media_link_to_admin_menu', 999 );

}

Without the "if ( !current_user_can( 'upload_files' ) ) {" the function works fine. But with the if statement, I get a critical error.

Am I missing something? I just want to check if user can upload files. If not, they don't need the Media Library link.

Thanks, Chris

I am writing a function to add a link to the Media Library in the admin menu on front end:

function add_media_link_to_admin_menu( $wp_admin_bar ) {

    // add Media Library to admin menu
        $wp_admin_bar->add_menu( array(
            'parent' => 'appearance',
            'id'     => 'media-library',
            'title'  => 'Media Library',
            'href'   => '/wp-admin/upload.php',
        ) );
        
}

// restrict to only users who can upload media
if ( !current_user_can( 'upload_files' ) ) {

    add_action( 'admin_bar_menu', 'add_media_link_to_admin_menu', 999 );

}

Without the "if ( !current_user_can( 'upload_files' ) ) {" the function works fine. But with the if statement, I get a critical error.

Am I missing something? I just want to check if user can upload files. If not, they don't need the Media Library link.

Thanks, Chris

Share Improve this question asked Mar 22, 2021 at 0:24 c_estep_tcbguyc_estep_tcbguy 32 bronze badges 1
  • 2 In future, if you're asking about a critical error can you include the details of the error in the question too please? You'll find them in the email the site sends or in your server error log or PHP error log, or in wp-content/debug.log if that's enabled. – Rup Commented Mar 22, 2021 at 0:38
Add a comment  | 

1 Answer 1

Reset to default 0

The problem is that wp_get_current_user (which current_user_can relies upon) is pluggable, which means it isn't loaded until after plugins are loaded (to give plugins a chance to override it). That means it's not available to call from the top level of a plugin file.

Instead, I'd make the role check inside the hook e.g.

function add_media_link_to_admin_menu( $wp_admin_bar ) {
    if ( current_user_can( 'upload_files' ) ) {
        // add Media Library to admin menu
        $wp_admin_bar->add_menu(array(
            'parent' => 'appearance',
            'id' => 'media-library',
            'title' => 'Media Library',
            'href' => '/wp-admin/upload.php',
        ));
    }
}
add_action( 'admin_bar_menu', 'add_media_link_to_admin_menu', 999 );

(However themes are loaded after pluggable functions, so your original code would work fine in a theme.)

本文标签: userscurrentusercan() causing critical error