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
1 Answer
Reset to default 0The 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
版权声明:本文标题:users - current_user_can() causing critical error 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741663889a2391193.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论