【问题标题】:add custom value's to a function in another wordpress plugin将自定义值添加到另一个 wordpress 插件中的函数
【发布时间】:2021-04-10 21:12:15
【问题描述】:

查看此代码:

function google_fonts() {

  $google_font_families = array(

'ABeeZee'                  => 'ABeeZee',
'Abel'                     => 'Abel',
'Abhaya Libre'             => 'Abhaya Libre',
'Abril Fatface'            => 'Abril Fatface',
  );

  return $google_font_families;

}

此功能在 WordPress 主题中 我想用插件为这个函数添加值。 怎么办?

【问题讨论】:

    标签: php wordpress function


    【解决方案1】:

    如果您能够更改该功能,您可以添加一个 WordPress 过滤器,您可以将其挂接到插件中并进行修改。

    function google_fonts() {
    $google_font_families = array(
    
        'ABeeZee'       => 'ABeeZee',
        'Abel'          => 'Abel',
        'Abhaya Libre'  => 'Abhaya Libre',
        'Abril Fatface' => 'Abril Fatface',
    );
    
    return apply_filters( 'theme_google_fonts',  $google_font_families );
    

    }

    您可以在此处找到过滤器的文档:https://developer.wordpress.org/reference/functions/apply_filters/


    有点hacky的解决方案:

    你也可以有支票

    if ( ! function_exists('google_fonts') ) {}
    

    包装函数,然后你可以在插件中覆盖它。插件代码更早触发,我认为这也可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多