【问题标题】:Overriding a function in WordPress覆盖 WordPress 中的函数
【发布时间】:2018-06-08 21:41:45
【问题描述】:

我正在尝试覆盖 WooCommerce 函数 load_country_states()。我正在创建一个自定义 WordPress 插件来做到这一点。我在some answers 中读到,不能简单地覆盖 PHP 中的函数。在插件中重写 WordPress 主题/插件中定义的函数的最佳方法是什么?我已阅读this。

要修改的函数:

/**
 * Load the states.
 */
public function load_country_states() {
    global $states;

    // States set to array() are blank i.e. the country has no use for the state field.
    $states = array(
        'AF' => array(),
        'AT' => array(),
        'AX' => array(),
        'BE' => array(),
        'BI' => array(),
        'CZ' => array(),
        'DE' => array(),
        'DK' => array(),
        'EE' => array(),
        'FI' => array(),
        'FR' => array(),
        'IS' => array(),
        'IL' => array(),
        'KR' => array(),
        'NL' => array(),
        'NO' => array(),
        'PL' => array(),
        'PT' => array(),
        'SG' => array(),
        'SK' => array(),
        'SI' => array(),
        'LK' => array(),
        'SE' => array(),
        'VN' => array(),
    );

    // Load only the state files the shop owner wants/needs.
    $allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() );

    if ( ! empty( $allowed ) ) {
        foreach ( $allowed as $code => $country ) {
            if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) {
                include( WC()->plugin_path() . '/i18n/states/' . $code . '.php' );
            }
        }
    }

    $this->states = apply_filters( 'woocommerce_states', $states );
}

修改后应该是这样的:

/**
 * Load the states.
 */
public function load_country_states() {
    global $states;
    // Load only the state files the shop owner wants/needs.
    $allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() );

    if ( ! empty( $allowed ) ) {
        foreach ( $allowed as $code => $country ) {
            if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) {
                include( WC()->plugin_path() . '/i18n/states/' . $code . '.php' );
            }
        }
    }

    $this->states = apply_filters( 'woocommerce_states', $states );
}

【问题讨论】:

    标签: php wordpress plugins


    【解决方案1】:

    实际上你不能简单地覆盖一个函数。只有当它被 if ( ! function_exists() ) 条件包围时才有可能。在您的情况下,它似乎是一个类方法,并且要覆盖它(在普通 PHP 中),您应该扩展该类并覆盖该方法。在您的 WordPress 场景中,它更简单,因为您可以使用简单的过滤器。你的代码应该是这样的:

    public function so48005823_load_country_states( $states ) {
        // make your magic here and 
        // don't forget to return the $states var
    
        return $states;
    }
    add_filter( 'woocommerce_states', 'so48005823_load_country_states' );
    

    确保不要简单地复制/粘贴代码,因为$this 变量仅在类范围内可用。您可以将此功能放入一个简单的插件中,它就会开始工作。

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      我尝试阅读更多关于此的内容,并提出了这个可行的解决方案。我们可以使用 WordPress 过滤器将 hack 添加到特定事件中。我修改了代码,现在可以使用了。

          /**
           * Load the states.
           */
      function load_country_states_modified() {
              global $states;
              // Load only the state files the shop owner wants/needs.
              $foobar = new WC_Countries;  // correct
              $allowed = array_merge( $foobar->get_allowed_countries(), $foobar->get_shipping_countries() );
              if ( ! empty( $allowed ) ) {
                  foreach ( $allowed as $code => $country ) {
                      if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) {
                          include( WC()->plugin_path() . '/i18n/states/' . $code . '.php' );
                      }
                  }
              }
      
              //$this->states = apply_filters( 'woocommerce_states', $states );
          }
          add_filter( 'woocommerce_states', 'load_country_states_modified' );
      

      【讨论】:

      • 请注意,WordPress 过滤器期望某些东西作为返回(对于没有返回的钩子,我们使用操作来代替)。我很高兴看到您的代码确实有效,但在一般情况下,原始类的 states 属性可能不会收到正确的值;)如果您因此遇到一些问题,您可以添加一个参数到你的函数并返回它而不改变它的值。
      猜你喜欢
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      相关资源
      最近更新 更多