users – current_user_can() causing critical error

Question

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

0
c_estep_tcbguy 4 months 2021-03-21T19:24:12-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse