Please help me through this example with a filter to understand how they work

I am looking at this generate password function:

function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
    $chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    if ( $special_chars )
        $chars .= '!@#$%^&*()';
    if ( $extra_special_chars )
        $chars .= '-_ []{}<>~`+=,.;:/?|';

    $password = '';
    for ( $i = 0; $i < $length; $i++ ) {
        $password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
    }

    // random_password filter was previously in random_password function which was deprecated
    return apply_filters('random_password', $password);
}

What I am trying to understand is the apply_filters part. Am I correct what it does is return random_password($password)? Where random_password is the filter function? But I cannot find random_password defined in any of the source files?

Am I right in thinking if the source code can’t find random_password() it returns $password, and if it can find random_password() defined somewhere it returns random_password($password)? If so, where in the source code do I define random_password()?

Thanks

2 Answers
2

To fully understand how WP works, you need to understand that WP is an event-driven platform(Though it’s a loose term, but I think it does help to understand better). You need to think like which event is occurring right now.

In this case, a password is generated.

Next question is, How can you change this generated your desired way. WP has provided a filter hook named random_password with apply_filters and also have provided the generated password for you to change( There’s also action hook. See Difference Between Filter and Action Hooks?).

You define the function to use to change the password with add_filter.

I ask you first to read the documentation of

  • apply_filters
  • add_filter

Let’s go back to your example. I also invite you to read the documentation of

  • wp_generate_password

codex says

It generates a random password drawn from the defined set of
characters.

Now say, you want to create a plugin which will change the way the password is generated.

You can’t(won’t any way want to) change the code of the WP core files. But you need to accomplish this. WordPress has given you the opportunity to change it via filter hook.

apply_filter function creates a filter in this case named random_password. So, now you have the option to change the generated password as the password is also sent as the argument.

So, random_password is not a function, it’s a filter by which you can filter WP generated password your desired way.

Now going back to your wish of changing the auto generated password, you use the filter like this:

add_filter('random_password', 'my_generated_password');

In this case, every event of password generation/call of wp_generate_password will also call my_generated_password function that you define and it will pass $password as argument.

For demonstration purpose, let’s say you want to add an extra character L to the generated password. In that case, you will define the function like this:

function my_generated_password($password){
    return ($password . 'L');
}

Leave a Comment