Clarus Dignus
Clarus Dignus

Reputation: 3897

Disable WordPress' wp_attempt_focus()

What I have:

A standard log-in page (specifically, the default WordPress wp-login.php page).

Here are two live examples:

  1. https://twentyfourteendemo.wordpress.com/wp-login.php
  2. http://demo.opensourcecms.com/wordpress/wp-login.php

What I need:

I need to disable the auto-focusing of the first textbox in the form.

What I've tried:

  1. I've attempted a hidden or off-screen positioned textbox to steal focus.
  2. I've attempted setting the tabindex of all inputs to -1 (very unideal).
  3. I've attempted using .blur() on the auto-focused textbox.

None of the approaches work.

Upvotes: 1

Views: 1536

Answers (3)

Brad Morris
Brad Morris

Reputation: 365

For those coming from Google, there is now a hook for this in the core (correct from V5.2, could be earlier!).

wp-login.php:1149 enable_login_autofocus : Boolean

add_filter('enable_login_autofocus', '__return_false');

Will disable the autofocus javascript snippet

Upvotes: 2

Robbert
Robbert

Reputation: 5193

I've posted a few solutions on the WordPress SE here. Basically there are two ways around it (in WordPress 4.0) that don't involve changing the code itself. Kill the function altogether using a dirty hack:

add_action("login_form", "kill_wp_attempt_focus");
function kill_wp_attempt_focus() {
    global $error;
    $error = TRUE;
}

Or use Geeklab's solution to modify the HTML code through buffering, allowing you to specifically kill the autofocus. Good luck!

Upvotes: 1

Clarus Dignus
Clarus Dignus

Reputation: 3897

I've found a solution based on charlietfl's setTimeout function.

setTimeout(function() {
      $( '<input type="text" id="focusfix" style="position:absolute; top:-1000px;"/>' ).insertBefore( "#loginform #user_login" );
      $('#loginform #focusfix').focus();
      $('#loginform #focusfix').select();
}, 205);

Upvotes: 0

Related Questions