Redirect users from admin pages the optimal solution

Question

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;
    }
} );

0
Iurie Malai 2 months 2021-04-11T13:23:54-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse