0

On two different sites I use two somewhat different solutions. Are they different in terms of security and performance? Which is better? Are there even better ones?

Solution 1.

/** Redirect users from admin pages if not administrators **/
add_action( 'admin_init', function() {
    if( ! current_user_can( 'manage_options' ) && ( ! wp_doing_ajax() ) ) {
        wp_safe_redirect( site_url() );
        exit;
    }
} );

Solution 2.

/** Redirect users from admin pages if not administrators **/
add_action( 'admin_init', function() {
    if( ! in_array( 'administrator', wp_get_current_user()->roles ) ) {
        wp_redirect( get_bloginfo( 'wpurl' ) );
        exit;
    }
} );
1
  • 1
    Keep in mind you need to write your question so that you can mark a single answer as the correct answer written factually for a single question, and not as a discussion reply. The 2 solutions you listed do not do the same thing.
    – Tom J Nowell
    Apr 11 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.