WordPress过滤器没有被添加 [英] Wordpress filter not being added

查看:101
本文介绍了WordPress过滤器没有被添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用apply_filters的插件,如下所示:

I have a plugin that uses apply_filters like this:

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

在主题的functions.php中,我这样做:

In my theme's functions.php, I do:

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

但是功能addAttachmentMeta从不运行.

如何更改我的apply或添加过滤器语句以使其生效,以便调用addAttachmentMeta?

How can I alter my apply or add filter statements to make it so that addAttachmentMeta gets called?

这是我根据有关如何添加其他附件元字段的教程编写的自定义插件.整个来源在这里: http://pastebin.com/7NcjDsK5 .正如我在评论中提到的那样,我知道它正在运行并且可以正常工作,因为我可以在此插件文件中添加其他字段,但是不能使用过滤器,因为不会添加过滤器.

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. 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.

我可以在apply_filters语句之前和之后看到var_dumps,但是我用add_filter指向的函数永远不会被调用.

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.

推荐答案

根据订购WordPress'核心加载,所有插件加载并执行后,function.php被调用.

According to the order WordPress' core loads, function.php gets called after all plugins are loaded and executed.

您需要确保在调用add_filter()之后插件中的apply_filters()运行.否则,在应用"过滤器的时候,add_filter()根本没有被调用.

You need to make sure the apply_filters() in your plugin runs AFTER your add_filter() is called. Otherwise at the point where your filters are 'applied', add_filter() simply hasn't been called yet.

您可以做的是使用一个钩子使您的插件的一部分在functions.php加载后运行.您可以使用 add_action('after_setup_theme', 'function_name') 挂钩.

What you could do is use a hook to make that part of your plugin run after functions.php has loaded. You could use the add_action('after_setup_theme', 'function_name') hook.

将插件文件的最后三行包装到一个函数中,并在functions.php运行之后执行它.

Wrap the last three lines of your plugin file inside a function and execute it after functions.php runs.

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');

这篇关于WordPress过滤器没有被添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆