• pattyok

    (@pattyok)


    I would like all of my images to behave the same. I would like to add the caption shortcode around all images regardless of whether they have a caption or not. Using the image_send_to_editor filter I can add the shortcode, but it only works in the Text tab, when switching between tabs it is lost. Is there something I am missing?

    add_filter('image_send_to_editor',  'add_image_caption', 20, 8);
    
    function add_image_credit($html, $id, $caption, $title, $align, $url, $size, $alt) 
    {
        if (empty($caption)) {
          $html = '[caption id="attachment_' . $id . '" align="align' . $align . '"]' . $html . '[/caption]';
        }
        return $html;
    }
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s a mismatch in function names in what you posted (a copy pasta error I’m sure), but the problem remains after correcting for that. The best filter to use is actually “media_send_to_editor”. It’s the last filter before the response is sent to the client. Unfortunately, it does not solve the problem. I’ve confirmed the correct HTML with shortcode is received client side, so it’s not a PHP/server side issue. Some sort of JS script is stripping the shortcode in visual tab when the attachment has no caption. I don’t know much of editor scripts, so I’m unable to find the cause.

    Instead of inserting the caption shortcode, what if you inserted the actual HTML that the shortcode generates? For HTML5, this would be the figure and figcaption tags with associated attributes.

    Or for that matter, you should be able to setup your CSS so non-captioned images have the same styling and spacing as captioned images, if that is your intent for all of this. Worst case would be needing to add another img class attribute for non-captioned images, which would allow CSS to apply the spacing used with figure and figcaption tags even though the tags do not exist.

Viewing 1 replies (of 1 total)
  • The topic ‘image_send_to_editor with shortcode only works in text tab’ is closed to new replies.