【问题标题】:Changing a function within an add_filters array更改 add_filters 数组中的函数
【发布时间】:2019-01-03 01:00:40
【问题描述】:

WooCommerce Composite Products插件的深处,有下面的add_filters数组:

        /**
     * Filter front-end params.
     *
     * @param  array  $params
     */
    $params = apply_filters( 'woocommerce_composite_front_end_params', array(
        'small_width_threshold'                 => 450,
        'full_width_threshold'                  => 450,
        'legacy_width_threshold'                => 450,
        'i18n_qty_string'                       => _x( ' × %s', 'qty string', 'woocommerce-composite-products' ),
        'i18n_price_string'                     => _x( ' – %s', 'price suffix', 'woocommerce-composite-products' ),
        'i18n_title_string'                     => sprintf( _x( '%1$s%2$s%3$s', 'title quantity price', 'woocommerce-composite-products' ), '%t', '%q', '%p' ),
        'i18n_selected_product_string'          => sprintf( _x( '%1$s%2$s', 'product title followed by details', 'woocommerce-composite-products' ), '%t', '%m' ),
        'i18n_free'                             => __( 'Free!', 'woocommerce' ),
        'i18n_total'                            => __( 'Total', 'woocommerce-composite-products' ) . ': ',
        'i18n_no_options'                       => __( 'No options available…', 'woocommerce-composite-products' ),
        'i18n_no_selection'                     => __( 'No selection', 'woocommerce-composite-products' ),
        'i18n_no_option'                        => _x( 'No %s', 'dropdown empty-value option: optional selection (%s replaced by component title)','woocommerce-composite-products' ),

等等等等……

我要做的是在两个函数(在本例中为i18n_clear_selectioni18n_strikeout_price_string)上使用remove_filter,并更改i18n_no_option 的第一个变量。

我试过 remove_filter( 'woocommerce_composite_front_end_params', 'i18n_strikeout_price_string' ); 什么都没做,并尝试使用下面的 sn-p(来自 this StackOverflow page):

function change_composite_product_functions($arr) {
$arr = array(
        'i18n_stirkeout_price_string'           => __(),
        'i18n_clear_selection'                  => __(),
        'i18n_no_option'                        => _x( 'Blah blah this is a test', 'dropdown empty-value options: optional selection (%s replaced by component title)','woocommerce-composite-products' ),
    );
return $arr;
}
 add_filter( 'woocommerce_composite_front_end_params', 'change_composite_product_functions', 10, 1 );

但这只是返回了一个白页——没有错误或任何东西。有没有办法隔离数组中的函数并仅删除/更改这些函数?

【问题讨论】:

  • 错误是否关闭?使用ini_set("display_errors","On");
  • 那里的代码非常有用,我不知道它存在。我得到:Fatal error: Uncaught ArgumentCountError: Too few arguments to function __() 和:ArgumentCountError: Too few arguments to function __() 所以它是我试图让 'i18n_strikeout_price_string' 和 'i18n_clear_selection' 返回 null 的位。这样做的正确方法是什么?或者更好的是,有没有办法让remove_filter 仅用于这些功能?
  • 对那么把上面的__()改成__( '' )就表示没有错误,页面加载完毕,但是插件中的数组完全被sn-p中的数组替换了——这个意味着除非我将整个数组从插件复制到 sn-p 并编辑我需要更改的位,否则插件将无法工作。有没有办法只针对我想要更改的数组中的函数?

标签: php wordpress


【解决方案1】:

请注意,“$arr”变量将填充传递给“change_composite_product_functions”函数的数据。

您的代码部分实际上是用您的新数组覆盖它。

您正在寻找数组键/项目并更改/删除它。下面快速未经测试/粗略的 sn-p:

function change_composite_product_functions($arr) {
if (array_key_exists('i18n_stirkeout_price_string', $arr)) unset($arr['i18n_stirkeout_price_string']); // Remove
if (array_key_exists('i18n_no_option', $arr)) $arr['i18n_no_option'] = 'Blah blah this is a test'; // Change
return $arr;
}
add_filter( 'woocommerce_composite_front_end_params', 'change_composite_product_functions', 10, 1 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多