2

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
    you probably do the remove too early, what is the return value of the remove? should be true if a filter was removed. – Mark Kaplun Oct 5 '14 at 7:58
  • Its always false. I just placed the code above in my theme's function.php. I can tell it's happening too soon, so how can I make the remove come after the add filter? – Nathan Oct 5 '14 at 14:39
3

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.

  • I tried it in the init function, but it still returns false. Updating original question. – Nathan Oct 5 '14 at 18:10
  • this time you removed the priority, IIRC you have to have it to the correct value – Mark Kaplun Oct 6 '14 at 13:08
  • OK I am traveling right now. I will try it when I get back and report results. I thought I had the same priority value when removing ( wordpress gives it a 20), but can't recall. I will double check Friday. Thanks. – Nathan Oct 7 '14 at 18:21
  • I updated the original post with what I have executing now. But it still tries to remove the filter before it has been added. – Nathan Oct 11 '14 at 14:26
  • 1
    the media.php file in which it is added is loaded from the wp_editor function. In theory you can hook on a later filter which is being used there to remove it but then you will depend on order of execution which doesn't logically have to remain the same in future versions of WP – Mark Kaplun Oct 11 '14 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.