Skip links
Published on: Block wp-admin in wordpress

How to Block wp-admin in wordpress

Rate this post

Block WordPress dashboard but allowing login page

 

Lets we discuss How to Block Wp-Admin In WordPress.The code below will allow the users to access only the default WordPress login page, once signed in they will not be able to see the dashboard.

Add below lines in functions.php
function block_dashboard() {
    $file = basename($_SERVER['PHP_SELF']);
    if (is_user_logged_in() && is_admin() && !current_user_can('edit_posts') && $file != 'admin-ajax.php'){
        wp_redirect( home_url() );
        exit();
    }
}

add_action('init', 'block_dashboard');

You might want to add a logout link somewhere on your website so the user can end his session.

Add below lines in header.php
if(is_user_logged_in()) {
    <a href="<?php echo wp_logout_url( home_url() ); ?>">Logout</a>
}

Completely blocking the WordPress login page and dashboard

If you have a frontend login system then blocking the default WordPress login page, together with the dashboard, might come in handy.

Add below lines in functions.php
function block_wpadmin() {
    $file = basename($_SERVER['PHP_SELF']);
    if ($file == 'wp-login.php' || is_admin() && !current_user_can('edit_posts') && $file != 'admin-ajax.php'){
        wp_redirect( home_url() );
        exit();
    }
}

add_action('init', 'block_wpadmin');