Wordpress filter not being added

醉酒当歌 提交于 2019-12-22 08:05:46

问题


I have a plugin that uses apply_filters like this:

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

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

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

But the function addAttachmentMeta never runs.

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

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


回答1:


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

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.

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.

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


来源:https://stackoverflow.com/questions/19277932/wordpress-filter-not-being-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!