Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I Look long enough to find the solution of my problem. Really, Is there any way to disable the "Remember me" to wp-admin? I want to disable it for safety reasons.

Thanks in advance.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You can do it with JavaScript (jQuery) or outputbuffering. A combination of both (outputbuffering and JS) are in the script below.

PHP:

namespace WPSE77581;

add_action( 'plugins_loaded', 'WPSE77581\remove_forgetmenot', 10, 0 );

function remove_forgetmenot() {

    add_action( 'login_enqueue_scripts', 'WPSE77581\enqueue_script', 10, 0 );

    add_action( 'login_head', 'WPSE77581\start_outputbuffer', 1, 0 );
    add_action( 'login_footer', 'WPSE77581\end_outputbuffer', 1, 0 );

}

function enqueue_script() {

    wp_enqueue_script(
        'WPSE77581-remove-forgetmenot',
        plugins_url( 'wpse77581.js', __FILE__ ),
        array( 'jquery' ),
        false,
        true
    );

}

function start_outputbuffer() {

    ob_start();

}

function end_outputbuffer() {

    $html = ob_get_clean();

    $search = '#<p class="forgetmenot">.+</p>#Uuis';

    $html = preg_replace( $search, '', $html );

    echo $html;

}

JS [wpse77581.js]:

jQuery( document ).ready(
    function ( $ ) {
        $( '.forgetmenot' ).remove();
    }
);

Both solutions has it's assets and drawbacks. Using outputbuffering can be a bit tricky if other plugins use outputbuffering too. JavaScript can be disabled in the visitors browser. Outputbuffering manipulate the DOM before it is send to the browser, JS after it was send to the browser.

Update

Why does the plugin code does not work in my functions.php?

To answer this question, we have to take a closer look at the hooks and have to understand when and what happened while WordPress is loading. At first the working code for the functions.php

<?php

/**
 * Remove forgetmenot-checkbox on login screen
 * Uses the hook 'after_theme_setup' instead of 'plugins_loaded' because
 * first the theme have to be setup for get_template_directory_uri()
 *
 * @uses add_action()
 * @hook after_setup_theme
 */
add_action(
    'after_setup_theme',
    'WPSE77581_remove_forgetmenot',
    10,
    0
);

/**
 * Hook up all actions and filters
 *
 * @uses add_action()
 * @hook login_enqueue_scripts
 * @hook login_head
 * @hook login_footer
 */
function WPSE77581_remove_forgetmenot() {

    // add JavaScript
    add_action( 'login_enqueue_scripts', 'WPSE77581_enqueue_script', 10, 0 );

    // plain PHP with outputbuffering
    add_action( 'login_head', 'WPSE77581_start_outputbuffer', 1, 0 );
    add_action( 'login_footer', 'WPSE77581_end_outputbuffer', 1, 0 );

}

/**
 * Enqueueing needed JavaScripts
 * JS is stored in the folder 'js' inside the theme folder
 *
 * @uses wp_enqueue_script
 * @uses get_template_directory_uri()
 */
function WPSE77581_enqueue_script() {

    wp_enqueue_script(
        'WPSE77581-remove-forgetmenot',
        get_template_directory_uri() . '/js/wpse77581.js',
        array( 'jquery' ),
        false,
        true
    );

}

/**
 * Start outputbuffering
 * Buffers the DOM for later manipulation
 */
function WPSE77581_start_outputbuffer() {

    ob_start();

}

/**
 * End outputbuffering
 * Get the content from outputbuffer and remove the paragraph with checkbox
 */
function WPSE77581_end_outputbuffer() {

    $html = ob_get_clean();

    $search = '#<p class="forgetmenot">.+</p>#Uuis';

    $html = preg_replace( $search, '', $html );

    echo $html;

}

Where are the differences to the plugin code? At first it is no longer a plugin and so we can not use plugins_url() to get the uri to the JavaScript. After a search in the Codex, we find the function [get_template_directory_uri()][1] to get the correct path to the JavaScript file.

After simply changing from plugins_url() to get_template_directory_uri() the code does not work. Now we have to start thinking. The code is no longer a plugin code, it is now a 'theme' or 'template' code. Can we use the hook plugins_loaded for a theme code?

It seems that this is not a good idea. To understand why this is not a good idea, we have a look inside the core files (in this case take wp-settings.php). As we read the core files, we find the line with do_action('plugins_loaded');. Until this point, no theme was setup and so get_template_directory_uri() does not know anything about a template directory. Continue reading the core file, we will find the part where the theme is setup. And we also find a line with do_action('after_setup_theme');. This seems to be a good point to hook up our code. After replacing plugins_loaded with after_setup_theme and test the code, we see the code will work.

If you want to develop plugins and / or themes for WordPress, you need to understand when and what happened while WordPress is loaded! If you get stuck, take a brief minute and start reading the core files.

Happy new year.

share|improve this answer
    
unset( $_POST['rememberme'] ); might help too. –  toscho Dec 29 '12 at 1:25
    
i create a function:remove_remember_me.php with the 1st part of code in .php and i call this for:require_once('includes/admin/remove_remember_me.php');, also i create a js with the 2nd part of code and i call this to function.php with <?php wp_enqueue_script('disable_me.js', get_template_directory_uri() . 'includes/js/disable_me.js',array('jquery'),'1.0',true);?>, but this is not work. What i am doing wrong? –  MikeD Dec 29 '12 at 14:52
    
First at all: Do you use PHP5.2 or PHP5.3? Namespaces are available since PHP5.3! Create a plugin, do not put everything in your functions.php (see discussion ). This function is not theme specific. So put it in a plugin. I create a gist which can be downloaded gist.github.com/4408787 This plugin works for me fine at PHP5.3 and PHP5.2 (both versions are included) –  Ralf912 Dec 29 '12 at 19:30
    
Thanks for help. I used PHP Version 5.4.7. I read the discussion and i will be back. –  MikeD Dec 29 '12 at 22:02
    
@Ralf912, i read in github the readme.txt, and and I realized that is plugin. But i don't use with plugin method, but only with custom method (php file and js). Αny help on my question? Thanks in advance. –  MikeD Dec 30 '12 at 13:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.