0

I was working on some php functions and I got confused with php syntax. Here is the function.

Is this correct? to use add_filter inside function_exists check

if ( ! function_exists( 'disable_password_reset' ) ) :
  function disable_password_reset() {
    return false;
  }
  add_filter ( 'allow_password_reset', 'disable_password_reset' );
endif;

or this one is correct, to use add_filter outside function_exists check

if ( ! function_exists( 'disable_password_reset' ) ) :
  function disable_password_reset() {
    return false;
  }
endif;
add_filter ( 'allow_password_reset', 'disable_password_reset' );

I was working on Wordpress if that matters.

3
  • Second or first. First ensures your filter uses this very function definition and not an existing one. Second uses any function definition but may double your filter. Make sure you include_once / require_once the file :) Commented Oct 19, 2012 at 20:25
  • @Claudrian Hm? How would the second one double the filter? It won't use any function definition, there's only one function definition for disable_password_reset, either an existing one or the newly created one (but not both). Commented Oct 19, 2012 at 20:37
  • YannisRizos yes, you are right. It wont use both definitions because the way function is defined. Commented Oct 19, 2012 at 20:43

1 Answer 1

3

What you are trying to do is:

  • Check if function disable_password_reset() exists, and if not, create it.
  • Hook disable_password_reset() to a Wordpress filter.

If you do this:

if ( ! function_exists( 'disable_password_reset' ) ) :
  function disable_password_reset() {
    return false;
  }
  add_filter ( 'allow_password_reset', 'disable_password_reset' );
endif;

then add_filter ( 'allow_password_reset', 'disable_password_reset' ); will not be executed if disable_password_reset() already exists. If you don't want that, then you should call add_filter() outside the if block, as in your second example.

Sign up to request clarification or add additional context in comments.

11 Comments

OK. Let me explain what I understood, if a function doesn't exist then it does not matter which one I use, both will work fine and work exactly same. The only difference is when a function already exist. Is the right?
@Roberthue function_exists() is typically used when you have at least a rough idea of what the function does, and you have some reason to believe it might not exist. For example I only remember using it to check for native functions, that although existed in the version of PHP I used locally, they might not exist in one of the various environments my code would be deployed in (and if it actually didn't exist, I either thrown an error or - as you do - emulated the function).
@Roberthue Now I don't know what the actual disable_password_reset() is supposed to do, but it probably isn't just return false. The filter will work with either function, the actual one and the emulated one, but obviously it won't work in exactly the same way.
Actually there is no function named disable_password_reset() and I custom create it to disable what Wordpress does by default if someone rest password (it resets user password). And I don't want users to reset password, that's why I added return false, to do nothing when someone hit password reset button.
@Roberthue If there's no function called disable_password_reset() then why are you checking if it exists in the first place?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.