简体   繁体   English

Wordpress过滤器未添加

[英]Wordpress filter not being added

I have a plugin that uses apply_filters like this: 我有一个使用apply_filters的插件,如下所示:

$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);

In my theme's functions.php , I do: 在我的主题的functions.php ,我做:

function addAttachmentMeta($additionalFields) {
    return $addtionalFields;
}
add_filter( 'attachment_meta_add_fields', 'addAttachmentMeta', 1, 1 );

But the function addAttachmentMeta never runs. 但函数addAttachmentMeta永远不会运行。

How can I alter my apply or add filter statements to make it so that addAttachmentMeta gets called? 如何更改我的应用或添加过滤器语句以使其调用addAttachmentMeta

Edit: This is a custom plugin that I wrote based off tutorials on how to add additional attachment meta fields. 编辑:这是一个自定义插件,我根据有关如何添加其他附件元字段的教程编写。 The whole source is here: http://pastebin.com/7NcjDsK5 . 整个来源是: http//pastebin.com/7NcjDsK5 As I mentioned in the comments, I know this is running and working because I can add additional fields in this plugin file, but not by using the filters because the filter doesn't get added. 正如我在评论中提到的,我知道这是在运行和工作,因为我可以在此插件文件中添加其他字段,但不能使用过滤器,因为过滤器没有添加。

I can see var_dumps before and after the apply_filters statement, but the function I've pointed to with add_filter never gets called. 我可以在apply_filters语句之前和之后看到var_dump,但是我用add_filter指向的函数永远不会被调用。

According to the order WordPress' core loads , function.php gets called after all plugins are loaded and executed. 根据WordPress的核心加载顺序 ,在加载并执行所有插件后调用function.php。

You need to make sure the apply_filters() in your plugin runs AFTER your add_filter() is called. 你需要确保apply_filters()在插件运行后您add_filter()被调用。 Otherwise at the point where your filters are 'applied', add_filter() simply hasn't been called yet. 否则,在“应用”过滤器的位置, add_filter()尚未被调用。

What you could do is use a hook to make that part of your plugin run after functions.php has loaded. 你可以做的是使用一个钩子来使你的插件的一部分在functions.php加载后运行。 You could use the add_action('after_setup_theme', 'function_name') hook. 您可以使用add_action('after_setup_theme', 'function_name')钩子。

Wrap the last three lines of your plugin file inside a function and execute it after functions.php runs. 将插件文件的最后三行包装在一个函数中,并在functions.php运行后执行它。

function addAttachmentMeta() {
    $additional_fields = array();
    $additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
    $am = new Attachment_Meta( $additional_fields );
}
add_action('after_setup_theme', 'addAttachmentMeta');

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

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