Skip to content

Instantly share code, notes, and snippets.

@grayayer
Created November 6, 2024 00:43
Show Gist options
  • Save grayayer/866f8812c562754c2b401b0c906061fa to your computer and use it in GitHub Desktop.
Save grayayer/866f8812c562754c2b401b0c906061fa to your computer and use it in GitHub Desktop.
allow wordpress password protected pages to show users what they're typing in the password field when it's focused
<?php
/**
* Password Peek
*
* Add focus toggle script to password-protected pages, so when the password field is focused, it will show the password.
*
* @package PDXWLF
*/
/**
* Enqueue password toggle script.
*/
function enqueue_password_toggle_script() {
// Only proceed if we're on a password-protected page.
if ( ! post_password_required() ) {
return;
}
// Define the inline script.
$script = "document.addEventListener('DOMContentLoaded',function(){const e=document.querySelector('input[name=\"post_password\"]'),t=document.querySelector('input[name=\"Submit\"]');e.addEventListener('focus',function(){this.setAttribute('type','text')}),e.addEventListener('blur',function(){document.activeElement.getAttribute('name')!=='Submit'&&this.setAttribute('type','password')}),t.addEventListener('focus',function(){e.setAttribute('type','password')})});";
// Add inline script with a unique handle.
wp_register_script( 'password-toggle', false, array(), '1.0.0', true );
wp_enqueue_script( 'password-toggle' );
wp_add_inline_script( 'password-toggle', $script );
}
add_action( 'wp_enqueue_scripts', 'enqueue_password_toggle_script' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment