Disable Remember Me in Login Form

I need to disable the 'Remember Me' prompt in login screens.

Based on answers here https://wordpress.stackexchange.com/a/306746/29416 (including my own answer from 2018 with code that I had working then) about disabling the Remember Me prompt on the WP login form (wp-login.php), I should be able to do this:

    add_filter('login_form_defaults', 'my_disable_remember_me');
    function my_disable_remember_me($args) {
        $args['remember']       = ;
        return $args;
    }

This is based on looking at wp-login.php (around line 604) where the 'remember' arguments shows the 'Remember Me' prompt. If the value is empty, or false, the HTML starting in line 605 will not be displayed. This is the code in wp-login.php that outputs the Remember Me' text and checkbox (line 604).

  ( $args['remember'] ?
        sprintf(
            'p class=login-rememberlabelinput name=rememberme type=checkbox id=%1$s value=forever%2$s / %3$s/label/p',
            esc_attr( $args['id_remember'] ),
            ( $args['value_remember'] ? ' checked=checked' : '' ),
            esc_html( $args['label_remember'] )
        ) : ''
    ) .

So using that filter 'login_form_defaults' should remove the prompt. But it doesn't. I have tried the other filters (login_form_top, login_form_middle, login_form_footer) to use the same my_disable_remember_me function, to no avail.

So I am confused as to why the login_form_defaults doesn't remove the Remember Me checkbox and text.

Topic wp-login-form filters Wordpress

Category Web


The login_form_defaults hook does work, but wp-login.php does not use wp_login_form() which runs the login_form_defaults hook, and instead the "Remember Me" checkbox is echoed like this, hence it's basically unremovable via PHP or there's no filter to disable (or an argument to bypass) the echo, or empty/modify the markup.

So that means, if wp_login_form() was used, then your code would have worked.

But on the wp-login.php page, you can disable the "Remember Me" via either CSS or JS, or both:

  • Use CSS to visually hide the checkbox, e.g. using the login_head hook:

    add_action( 'login_head', function () {
        echo '<style>.forgetmenot { display: none !important }</style>';
    } );
    
  • Use JS to completely remove the element, e.g. using the login_footer hook:

    add_action( 'login_footer', function () {
        ?>
            <script>
                try {
                    document.querySelector( '.forgetmenot' ).remove();
                } catch ( err ) {}
            </script>
        <?php
    } );
    

Or just create a custom login page and send users to that page instead when they attempt to login.

That way, you can manually call wp_login_form() and just disable the "Remember Me" as you wish.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.