Using WordPress ‘enable_login_autofocus’ PHP filter

The enable_login_autofocus WordPress PHP filter controls whether the login input field should automatically receive focus when the login screen is loaded.

Usage

add_filter('enable_login_autofocus', 'custom_login_autofocus');
function custom_login_autofocus($print) {
  // your custom code here
  return $print;
}

Parameters

  • $print (bool): Whether to print the function call. Default is true.

More information

See WordPress Developer Resources: enable_login_autofocus

Examples

Disable autofocus on the login screen

Prevent the username or email input field from automatically receiving focus when the login screen is loaded.

add_filter('enable_login_autofocus', '__return_false');

Enable autofocus only for non-mobile devices

Allow the username or email input field to receive focus automatically on non-mobile devices.

add_filter('enable_login_autofocus', 'enable_autofocus_for_non_mobile_devices');
function enable_autofocus_for_non_mobile_devices($print) {
  if (wp_is_mobile()) {
    return false;
  }
  return $print;
}

Enable autofocus only for specific user roles

Allow the username or email input field to receive focus automatically only for users with a specific role, such as administrators.

add_filter('enable_login_autofocus', 'enable_autofocus_for_administrators', 10, 2);
function enable_autofocus_for_administrators($print) {
  if (current_user_can('manage_options')) {
    return true;
  }
  return $print;
}

Enable autofocus only on specific pages

Allow the username or email input field to receive focus automatically only on specific pages, such as a custom login page.

add_filter('enable_login_autofocus', 'enable_autofocus_on_specific_pages');
function enable_autofocus_on_specific_pages($print) {
  if (is_page('custom-login')) {
    return true;
  }
  return $print;
}

Enable autofocus based on a custom condition

Allow the username or email input field to receive focus automatically based on any custom condition.

add_filter('enable_login_autofocus', 'enable_autofocus_based_on_custom_condition');
function enable_autofocus_based_on_custom_condition($print) {
  // Define your custom condition
  $custom_condition = true;
  if ($custom_condition) {
    return true;
  }
  return $print;
}