I don't understand why I shoud use lostpassword_url hook?

Look into my comment in the code below:

// WHY can I not just do this?

class Customuser {

    public function __construct() {

        // Using this when using lost password functionality
        add_filter("lostpassword_url", array( $this, 'lostyourpasswordpage') );

        $html .= '<a href="' . wp_lostpassword_url( get_bloginfo('url') ) . '">Forgot?</a>';
        echo $html;

        // WHY can I not just do this?
        $html .= '<a href="/forgotmypassword">Forgot?</a>';
        echo $html;            

    }

    public function lostyourpasswordpage() {
        return '/forgotmypassword';
    }

}

Can somebody please explain?

I guess I could get some getyourpassword-functionality when using the hook but I don’t understand how to use it? (Else I could do functions to achieve the same thing but it seems unnecessary when the WordPress core seems to support this functionality.)

Solutions Collecting From Web of "I don't understand why I shoud use lostpassword_url hook?"

Think of wp_lostpassword_url() as a placeholder for the link to the page containing the form to allow users to reset their password.

You can use this placeholder anywhere within your WordPress site and if you decide to provide your own form to allow users to reset their password you can easily update the link via the filter:

class Customuser {

    public function __construct() {
        add_filter( 'lostpassword_url', array( $this, 'lostyourpasswordpage' ), 10, 2 );
    }

    public function lostyourpasswordpage( $lostpassword_url, $redirect ) {
        return home_url( '/forgotmypassword/' );
    }

}

Calling your class via something like new Customuser(); will then change the output of wp_lostpassword_url() everywhere from the default /wp-login.php?action=lostpassword to /forgotmypassword. So there will be no need to search/replace/update the link everywhere.

Nevertheless it will not generate a page with a form like the one provided by WordPress. It’s up to you to do this.

Hope this will make things more clear now 🙂