简体   繁体   English

帮助正确的php语法

[英]Help with correct php syntax

I was working on some php functions and I got confused with php syntax. 我当时正在研究某些php函数,但对php语法感到困惑。 Here is the function. 这是功能。

Is this correct? 这个对吗? to use add_filter inside function_exists check 在function_exists内部使用add_filter检查

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 还是这个正确,在function_exists外部使用add_filter检查

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. 如果重要的话,我正在研究Wordpress。

What you are trying to do is: 您正在尝试做的是:

  • Check if function disable_password_reset() exists, and if not, create it. 检查功能disable_password_reset()存在,如果不存在,则创建它。
  • Hook disable_password_reset() to a Wordpress filter. disable_password_reset()挂钩到Wordpress过滤器。

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' ); 然后add_filter ( 'allow_password_reset', 'disable_password_reset' ); will not be executed if disable_password_reset() already exists. 如果disable_password_reset()已经存在,将不会执行。 If you don't want that, then you should call add_filter() outside the if block, as in your second example. 如果您不希望这样做,则应在if块外调用add_filter() ,如第二个示例所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM