0

I am trying to get a small plugin Im putting together to work from the admin page - I have the functrion working, although it would be better to "activate" it from that page *the default would be "OFF"

here is a copy of the function - it just turns off the lost password text and function in Wordpress

class LostPasswordPage
{
    /* Holds the values to be used in the fields callbacks */
    private $options;

    /* Start up */
    public function __construct()
    {
        add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
        add_action( 'admin_init', array( $this, 'page_init' ) );
    }

    /** Add options page */
    public function add_plugin_page()
    {

    /* This page will be under "Settings" */
    add_menu_page(
            'Settings Admin', 
            'Lost Password', 
            'manage_options', 
            'lost-pwd-setting-admin', 
            array( $this, 'create_admin_page' )
        );
    }

    /* Options page callback */
    public function create_admin_page()
    {
        // Set class property
        $this->options = get_option( 'lost_pwd_option_onoff' );
        ?>
        <div class="wrap">
            <h1 style="margin-bottom:-10px;">Small Additional Security Function</h1>
            <span style="display:block;max-width:370px;text-align:right;"><sub>1.0.0a Robab Tools</sub></span>
            <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields( 'lost_pwd_option_group' );
                do_settings_sections( 'lost-pwd-setting-admin' );
                submit_button();
            ?>
            </form>
        </div>
        <?php
    }

    /* Register and add settings */
    public function page_init()
    {        
        register_setting(
            'lost_pwd_option_group', // Option group
            'lost_pwd_option_onoff', // Option name
            array( $this, 'sanitize' ) // Sanitize
        );

        add_settings_section(
            'setting_section_id', // ID
            'DisableLost Password Form and Label', // lostpwd
            array( $this, 'print_section_info' ), // Callback
            'lost-pwd-setting-admin' // Page
        );  

        add_settings_field(
            'lostpwd', 
            'Disable Lost Password? &rarr;', 
            array( $this, 'lostpwd_callback' ), 
            'lost-pwd-setting-admin', 
            'setting_section_id'
        );      
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
        $new_input = array();
        if( isset( $input['lostpwd'] ) )
            $new_input['lostpwd'] = sanitize_text_field( $input['lostpwd'] );

        return $new_input;
    }

    /** 
     * Print the Section text
     */
    public function print_section_info()
    {
        print 'Removes the Lost Password feature.
        <br>Removes the Lost Password text on login page.
        <br>Returns to Login page and 301.
        <h3 style="margin-bottom:-12px;">Instructions for use</h3>
        <br>Check to activate. Save setting';
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function lostpwd_callback()
    {
        printf(
            '<input type="checkbox" id="lostpwd" name="lost_pwd_option_onoff[lostpwd]" value="%s" />',
            isset( $this->options['lostpwd'] ) ? esc_attr( $this->options['lostpwd']) : ''

        );
    }
}

if( is_admin() )
    $my_settings_page = new LostPasswordPage();

///////////////////////////////////////////////////////////////////////
/* Removes the Lost Password feature */
function disable_reset_lost_password()
{
return false;
}
add_filter( 'allow_password_reset', 'disable_reset_lost_password');

/* Removes the Lost Password text */
function remove_lostpassword_text ( $text ) {
     if ($text == 'Lost your password?'){$text = '';}
        return $text;
     }
add_filter( 'gettext', 'remove_lostpassword_text' );

/* Send lost password back to login & 301 */
function disable_lost_password() {
    if (isset( $_GET['action'] )){
        if ( in_array( $_GET['action'], array('lostpassword', 'retrievepassword') ) ) {
            wp_redirect( wp_login_url(), 301 );
            exit;
        }
    }
}
add_action( "login_init", "disable_lost_password" );

I have been trying to get the function to activate whenever the checkbox is used - no success - It needs another function to check if the box is used ot not, and if so, run the function - right now it just runs on global activation (I have a tried a couple of elseifs, but got nowhere), Im not getting it right. And if memory serves, it has to be to put into the options of WP to be remembered by WP (which it does, by checking the options.php, the serialized data is there, but the checkbox does not remain "on", something wrong there also and I cant fins d it - or I'm having seroius senior moments :)

Would this also need another function to "clear out" the [ lost_pwd_option_onoff ] enties in the database/options.php if plugin is disabled/removed?

1
Contributor
0
Replies
25
Views
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.