[wp-hackers] overriding pluggable.php functions

Otto otto at ottodestruct.com
Sat Jan 10 20:18:30 GMT 2009


On Sat, Jan 10, 2009 at 12:13 AM, Will Norris <will at willnorris.com> wrote:
> What do you others think?

Another way to do this sort of thing is to have a special return value
on a filter.

function wp_authenticate($username, $password) {
  $pre = apply_filters( 'wp_authenticate', false, $username, $password );
  if ( false !== $pre ) return $pre;
  .. normal wp_auth logic here ..
}

Then, to hook in your function, instead of replacing the original, you
simply create your own and hook it in, like so:

function my_authenticate($value, $username, $password) {
.. do my authentication...
...oh noes, I can't authenticate, let's fall back to normal logic...
return false;
}
add_filter('wp_authenticate', 'my_authenticate',10,3);

This is the same method used in a few other places, like in the
get_option function with the pre_option_* filter. Obviously, this only
works for functions that don't return true/false normally, but most
every function has some kind of invalid value that can be returned to
trigger it.

Alternatively, one could use Exceptions. Raise the exception, catch
it, then do the default logic if it's the exception you're looking
for. This would be the ideal way, but I'm not sure exceptions are 100%
PHP4 compatible in all cases.

-Otto


More information about the wp-hackers mailing list