Extend Function To Not Apply To Certain Roles

Hi

Firstly a big thank you to @Sajid for helping me out in this thread

https://premium.wpmudev.org/forums/topic/how-to-re-map-a-url-to-new-url-with-variables

He provided the following function which is perfect.

function wpmu_map_profile_url(){
	$user_id = $_GET['user_id'];

	if($user_id){
		$new_url = get_site_url('', 'profile');
		$new_url = $new_url.'/'.$user_id;
		if(!headers_sent()){
			wp_redirect($new_url);
			exit;
		}
	}
}
add_action('admin_init', 'wpmu_map_profile_url');

Is there any way to make this so that it does not do it to two of our user roles who may still need to amend from the backend? The roles I would like to exclude from being affected by this function are 'administrator' and 'hub admin'

Many Thanks

Wayne

  • Tyler Postle
    • CGO

    Hey Wayne,

    Thanks for your question! I'm just going to follow up with @Sajid on this to see what the best way to go about adding that additional parameter would be. Either myself or Sajid will reply back here once we have more info :slight_smile:

    In the meantime, let us know if you have any further questions.

    Cheers,
    Tyler

  • Sajid
    • DEV MAN’s Sidekick

    Hi @Wayne,

    Hope you are doing good today :slight_smile:

    I have modified the code accordingly, I assuming the "hub admin" is custom user role. If yes then make sure its the correct name (normally roles don't have spaces) and replace with "hub admin" in the condition.

    function wpmu_map_profile_url(){
    	$user_id = $_GET['user_id'];
    
    	if($user_id && ( !current_user_can('administrator') || !current_user_can('hub admin') ) ){
    		$new_url = get_site_url('', 'profile');
    		$new_url = $new_url.'/'.$user_id;
    		if(!headers_sent()){
    			wp_redirect($new_url);
    			exit;
    		}
    	}
    }
    add_action('admin_init', 'wpmu_map_profile_url');

    Take care and have a nice day :slight_smile:

    Kind Regards,
    Sajid J

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.