简体   繁体   中英

Wordpress filter not being added

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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