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