Halloween Sale 15% off Ends in See the plans
PHP

Restrict wp-admin to Staff With WooCommerce My Account Support

Sends subscribers, customers and other users without editing rights from wp-admin to the WooCommerce My Account page or the homepage, with options to keep the profile screen open and hide the admin bar. AJAX and front-end forms keep working.

Don’t have WPIDE yet? Install it on your site

Code

// Users with this capability keep full access to wp-admin. 'edit_posts' covers contributors and up.
$wpide_admin_access_capability = 'edit_posts';

// Let everyone else still open Users > Profile to change their name, email or password.
$wpide_allow_profile_page = false;

// Where to send them. Leave empty for the WooCommerce My Account page (when WooCommerce is active) or the homepage.
// Must be an address on this site.
$wpide_admin_redirect_url = '';

// Also hide the admin bar from these users on the front end.
$wpide_hide_admin_bar_for_them = true;

$wpide_admin_destination = function ( $for_profile = false ) use ( $wpide_admin_redirect_url ) {
    if ( '' !== $wpide_admin_redirect_url ) {
        return $wpide_admin_redirect_url;
    }

    if ( function_exists( 'wc_get_page_id' ) && function_exists( 'wc_get_page_permalink' ) && wc_get_page_id( 'myaccount' ) > 0 ) {
        return ( $for_profile && function_exists( 'wc_get_account_endpoint_url' ) ) ? wc_get_account_endpoint_url( 'edit-account' ) : wc_get_page_permalink( 'myaccount' );
    }

    return home_url( '/' );
};

add_action( 'admin_init', function () use ( $wpide_admin_access_capability, $wpide_allow_profile_page, $wpide_admin_destination ) {
    global $pagenow;

    if ( wp_doing_ajax() || wp_doing_cron() || ! is_user_logged_in() || current_user_can( $wpide_admin_access_capability ) ) {
        return;
    }

    // Front-end forms post to these files.
    if ( in_array( $pagenow, array( 'admin-post.php', 'async-upload.php' ), true ) ) {
        return;
    }

    $is_profile = 'profile.php' === $pagenow;

    if ( $is_profile && $wpide_allow_profile_page ) {
        return;
    }

    wp_safe_redirect( $wpide_admin_destination( $is_profile ) );
    exit;
}, 1 );

// WooCommerce blocks wp-admin for customers on its own. Let the profile page through when it is allowed.
add_filter( 'woocommerce_prevent_admin_access', function ( $prevent ) use ( $wpide_allow_profile_page ) {
    return ( $wpide_allow_profile_page && isset( $GLOBALS['pagenow'] ) && 'profile.php' === $GLOBALS['pagenow'] ) ? false : $prevent;
} );

// "Edit Profile" links point to the front-end account page when the profile screen is closed.
add_filter( 'edit_profile_url', function ( $url, $user_id ) use ( $wpide_admin_access_capability, $wpide_allow_profile_page, $wpide_admin_destination ) {
    if ( $wpide_allow_profile_page || user_can( $user_id, $wpide_admin_access_capability ) ) {
        return $url;
    }

    return $wpide_admin_destination( true );
}, 10, 2 );

add_filter( 'show_admin_bar', function ( $show ) use ( $wpide_hide_admin_bar_for_them, $wpide_admin_access_capability ) {
    if ( $wpide_hide_admin_bar_for_them && is_user_logged_in() && ! current_user_can( $wpide_admin_access_capability ) ) {
        return false;
    }

    return $show;
} );

Notes

A fuller version of "Keep Subscribers Out of wp-admin"; use one or the other. Works without WooCommerce. When the profile screen is closed, "Edit Profile" links go to My Account > Account details (WooCommerce) or the redirect address.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.