Wordpress - How to disable profile.php for users?

Redirect from profile.php to the dashboard

Here's one way to do it:

add_action( 'load-profile.php', function() {
    if( ! current_user_can( 'manage_options' ) )
        exit( wp_safe_redirect( admin_url() ) );
} );

where we redirect to the dashboard instead, if the current user can't manage options.

Redirect from profile.php to the current user's member page

If you want to redirect to the member's profile page, you could try (untested):

add_action( 'load-profile.php', function() {
    if( ! current_user_can( 'manage_options' ) && function_exists( 'bp_core_get_user_domain' ) )
        exit( wp_safe_redirect( bp_core_get_user_domain( get_current_user_id() ) ) );
} );

The bp_core_get_user_domain() function is mentioned in this answer, few years ago, by @BooneGorges.

I just checked the BP source and this function is still available in BP 2.3 (see here).

For PHP < 5.3

add_action( 'load-profile.php', 'wpse_195353_profile_redirect_to_dashboard' );
function wpse_195353_profile_redirect_to_dashboard()
{
    if( ! current_user_can( 'manage_options' ) )
        exit( wp_safe_redirect( admin_url() ) );
}

and

add_action( 'load-profile.php', 'wpse_195353_profile_redirect_to_member_page' );
function wpse_195353_profile_redirect_to_member_page()
{
    if( ! current_user_can( 'manage_options' ) && function_exists( 'bp_core_get_user_domain' ) )
        exit( wp_safe_redirect( bp_core_get_user_domain( get_current_user_id() ) ) );
}

but you should consider updating your PHP if that's the case.


The following code* will redirect non-admin to a custom profile page in the front end, because instead of disabling you need to redirect them to your custom page. :)

<?php
add_action ('init' , 'wpse_redirect_profile_access');

function wpse_redirect_profile_access(){
        //admin won't be affected
        if (current_user_can('manage_options')) return '';

        //if we're at admin profile.php page
        if (strpos ($_SERVER ['REQUEST_URI'] , 'wp-admin/profile.php' )) {
            wp_redirect ( home_url( '/my-profile' )); // to page like: example.com/my-profile/
            exit();
        }

}

*Source^

Tags:

Profiles