Using WordPress ‘auth_redirect_scheme’ PHP filter

The auth_redirect_scheme WordPress PHP filter allows you to modify the authentication redirect scheme.

Usage

add_filter('auth_redirect_scheme', 'my_custom_auth_redirect_scheme');

function my_custom_auth_redirect_scheme($scheme) {
  // your custom code here
  return $scheme;
}

Parameters

  • $scheme (string): The authentication redirect scheme. Default is an empty string.

More information

See WordPress Developer Resources: auth_redirect_scheme

Examples

Change the authentication scheme to HTTPS

Force the authentication redirect to use the HTTPS scheme.

add_filter('auth_redirect_scheme', 'force_https_auth_redirect');

function force_https_auth_redirect($scheme) {
  return 'https';
}

Use a custom authentication scheme

Use a custom authentication scheme called “custom_scheme” for your redirects.

add_filter('auth_redirect_scheme', 'use_custom_scheme');

function use_custom_scheme($scheme) {
  return 'custom_scheme';
}

Modify the authentication scheme conditionally

Use a different authentication scheme based on the current user’s role.

add_filter('auth_redirect_scheme', 'modify_scheme_based_on_user_role');

function modify_scheme_based_on_user_role($scheme) {
  $current_user = wp_get_current_user();

  if (in_array('administrator', $current_user->roles)) {
    return 'admin_scheme';
  } else {
    return 'user_scheme';
  }
}

Change the authentication scheme for specific pages

Change the authentication scheme for specific pages based on their slug.

add_filter('auth_redirect_scheme', 'change_scheme_for_specific_pages');

function change_scheme_for_specific_pages($scheme) {
  global $post;
  $page_slug = $post->post_name;

  if ($page_slug == 'special-page') {
    return 'special_scheme';
  }

  return $scheme;
}

Modify the authentication scheme using a custom function

Use a custom function to determine the authentication scheme.

add_filter('auth_redirect_scheme', 'apply_custom_function_for_scheme');

function apply_custom_function_for_scheme($scheme) {
  return my_custom_scheme_function();
}

function my_custom_scheme_function() {
  // Determine the scheme based on your custom logic
  return 'custom_scheme';
}