4.0 remove_filter for WordPress core function not working for me

I was trying to overwrite how captions are embedded into a page when editing in the admin. I simply did this:

 remove_filter( 'image_send_to_editor', 'image_add_caption', 20);
 add_filter( 'image_send_to_editor', 'update_image_add_caption', 10, 8 );

But it’s just not working. I have tried different variations of priorities, and not using them at all, but it doesn’t work. Why wouldn’t this work?

UPDATE

Here’s what I am trying right now, but $result always returns false, so the filter isn’t removed.

function childtheme_remove_filters(){
    $result = remove_filter( 'image_send_to_editor', 'image_add_caption', 20);
    add_filter( 'image_send_to_editor', 'update_image_add_caption', 20, 8 );
}
add_action( 'init', 'childtheme_remove_filters' );

1 Answer
1

From the comments the remove_filter is called too early before the core add_filter is called. Best way to avoid this is to always remove nd add your filter at the init action

add_action('init','wpse163434_init');

function wpse163434_init() {
  remove_filter.....
  add_filter.....
}

This way you are assure that the core had finished initializing and all the core actions and filters are already set.

Update: all of the above is nice and true, but there is no software without bugs and apparently some filters are added in places where it is really hard to remove them without feeling it is too much off a hack, and image_send_to_editor is one of them.

Luckily that filter applies the image_add_caption_shortcode filter and passes the original html to it so instead of removing image_send_to_editor you might get the same impact by using the image_add_caption_shortcode filter.

Leave a Comment