How can I add the User Menu for Authors (role)?

How do I make this (and the pages) appear for authors, now that they actually have the relevant capabilities? They don’t have the needed capabilities, just because a role can add/edit/remove users does not mean they can list_users. Similarly there are other capabilities such as role promotion etc, here’s the code for administrators, e.g. this … Read more

Why is user_can_access_admin_page() an undefined function?

As mentioned by Jacob Peattie: functions not loaded yet. You will need to use hooks/actions (init, wp etc) https://codex.wordpress.org/Plugin_API/Action_Reference e.g. function callMyMethod(){ $userLoggedIn = is_user_logged_in(); echo ‘INIT Action: User Logged in: ‘.var_export($userLoggedIn, true); if ( is_admin() ) { $canAccess = user_can_access_admin_page(); } } add_action(‘init’,’callMyMethod’); This answer is assuming you’re creating a plugin and calling the … Read more

What is the correct way of validating running code when a particular role accesses a screen?

Reducing the number of checks increases the performance of your code, so yes, check is_user_logged_in() and current_user_can() as few times as you can. For executing functions depending on the admin page, I’d probably attach callbacks to load-{$pagenow} hook (untested): function wpse417218_do_something_for_edit_page() { if ( ‘page’ !== get_current_screen()->id ) { return; } if ( ! current_user_can( … Read more