9

I am using wordpress 4.2.2 and i am using buddypress latest version. I want all my users customize their profile at buddypress profile page. So i want to disable profile.php for the users. I hide the profile link from dashboard by the WP admin UI customize plugin.But when anyone type url mysite/wp-admin/profile.php it's appear on browser.So i want to escape from the problem and want to disable profile.php for the users. What should i do to do this?

4 Answers 4

16

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.

7
  • This is not helping brother. When i paste those two codes it is showing white screen.
    – akarim
    Jul 24, 2015 at 2:44
  • Are you using PHP < 5.3 ? If so then the anonymous functions aren´t supported, but it's easy to change that to normal functions - please check out the updated answer @azizul
    – birgire
    Jul 24, 2015 at 3:33
  • Brother! The two codes for PHP<5.3 is workinkg !! But there is a problem that my server(bluehost) cPanel showing PHP version 5.4.24 I don't know why this code is working. Is there any problem will be happen in future if i use this code?
    – akarim
    Jul 24, 2015 at 6:50
  • Glad to hear it worked for you. That's strange but both code versions should be PHP future safe. I just like the anonymous functions, introduced in PHP 5.3 ;-) @azizul
    – birgire
    Jul 24, 2015 at 9:51
  • Thank's a lot for above help. I want another help! I want to redirect admin.php?page=jetpack also for users. How to do that?
    – akarim
    Jul 28, 2015 at 16:42
2

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^

4
  • It looks like there's a missing exit();, after the redirect part.
    – birgire
    Jul 24, 2015 at 9:57
  • 1
    @birgire you are right. I din't notice that, thanks a lot. Fixed now. :) Jul 26, 2015 at 7:51
  • 1
    ps: we must be careful using if without curly braces. It works for a single expression but you got two. Currently the exit() is outside the if part and will always run. So we better use the if(){ } syntax when dealing with two or more expressions ;-)
    – birgire
    Jul 26, 2015 at 10:55
  • @birgire I missed that again. Thanks a lot again. :) Jul 26, 2015 at 11:04
1

I like this one. You can add admin pages to the array to redirect them. I have it redirecting to the Dashboard below, but you can redirect to BuddyPress URL as well... I'm just not sure what that URL is since I don't use BP very often.

   function no_proflie_admin_pages_redirect() {
      global $pagenow;
      if(!current_user_can('manage_options')) {
          $admin_redirects = array(
                    'profile.php'
                );
          if(in_array($pagenow, $admin_redirects)){
            wp_redirect( admin_url('/') ); exit;
          }
      }
    }
    add_action('admin_init', 'no_proflie_admin_pages_redirect');
  

You can also hide any additional profile.php links with simple CSS:

function hide_any_profile_links() { ?>
    <style type="text/css">
        a[href="http://example.com/wp-admin/profile.php"], a[href="profile.php"]{
            display: none!important;
        }
    </style>
<?php }
add_action('admin_head', 'hide_any_profile_links', 999);

The above could also be achieved with jquery or using PHP and the output buffer.

8
  • I'm afraid it will not work to have current_user_can() in the global scope, because of wp_get_current_user() not beeing defined yet.
    – birgire
    Jul 24, 2015 at 10:04
  • Hey @birgire, could you give example of when this would not work with profile page? Jul 26, 2015 at 6:04
  • The current_user_can() check should be inside the no_proflie_admin_pages_redirect callback, to avoid the undefined error.
    – birgire
    Jul 26, 2015 at 19:16
  • 1
    We can't use current_user_can() in the global scope, within a plugin. This leads to Fatal error: Call to undefined function wp_get_current_user(). But I guess you use it within the functions.php file?
    – birgire
    Oct 2, 2015 at 8:43
  • 1
    The CSS selector can be simplified to a[href$="profile.php"], which means matches the end of the href attribute value.
    – Raptor
    Oct 21, 2019 at 2:05
1

Extending on @birgire 's answer, from which I found the first snippet to be the cleanest one I've seen for this, I wanted to add the following since I think it might be useful for most people arriving to this thread.

If you don't want users to access their profile on the backend, you will most probably also don't want them to be able to access the dashboard.

The use case is when you have subscribers that you don't want to access the backend at all. By default they can access the dashboard and their profile, so let's avoid that:

function redirect_subscribers () {
    if( ! current_user_can( 'edit_posts' ) ) {
        exit( wp_safe_redirect( site_url() ) );
    }
}
add_action( 'load-index.php', 'redirect_subscribers' );
add_action( 'load-profile.php', 'redirect_subscribers' );

Notice that in this case I've used the edit_posts capability, since I'm only targeting subscribers and this is a capability all roles have except for subscribers.

Then I'm using site_url() to redirect them to the homepage, but you could also use get_permalink($page_id) using the id of the page where you have created a "My account" area as a parameter.

The load-{$page_hook} is documented in codex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.