Tagged: 

Viewing 18 posts - 1 through 18 (of 18 total)
  • Author
    Posts
  • #1407446

    Hi,

    I would like to use shortcodes within the image caption, both for ALB (that I use on pages only) and for classic editor (that I use for posts only).
    How can I do this please?

    z2

    The shortcode should be placed directly in the Image Caption field shown in the image together with my normal caption text – see screenshot.

    An example of a shortcode that I would like to use is this for example:

    // Add Shortcode
    function contentblock_bildanfrage() {
    return '
    <span class="bildanfrage"><a href="https://inafineart.de/kontakt/" title="Bild anfragen">Bild anfragen</a></span>
    ';
    }
    add_shortcode( 'bildanfrage', 'contentblock_bildanfrage' );

    How can I enable shortcodes for image captions please?
    Thank you.

    #1407509

    Hi BeeCee,

    Insert an image via Advanced Layout Builder, then add the image, set Image Caption to Yes, then on the Caption put [bildanfrage]
    Hope this helps.

    Best regards,
    Nikko

    #1407525

    No, this does not work, because shortcodes are not executed in image captions, it shows [Bildanfrage] as plain text.
    So something is needed to execute shortcodes in image captions.

    #1407526

    Hi BeeCee,

    I tried to add a shortcode which will output a button and put it inside the image caption and it worked, here’s the screenshot:

    Best regards,
    Nikko

    #1407544

    No, it does not work at my site, I have added the login details below.

    Note:
    Posts are made with classic editor.
    Pages are made with ALB.

    In ALB Image captions brackets like [ … ] are not allowed, there is a warning, so it can not work:

    z1

    And in posts, which I create only with classic editor, not ALB, the shortcode is not executed, it is just plain text:

    z2

    #1407565

    Hi BeeCee,

    Thanks for giving us access.
    The page made with Classic Editor does not work but the one using ALB definitely works.
    The shortcode was only returning a link inside a span tag which is what is showing in the frontend (I have also inspected it and it is correct).
    Can you tell us what you want it to achieve? or how should it look visually?

    Best regards,
    Nikko

    #1407576

    For the moment my shortcode should lead to the contact page – but this is not relevant, what content my shortcode has, it will be extended in future.
    It should be executed in the image caption and not shown as plain text as you see in my screenshots above.

    For the ALB page I have inadvertently set “overlay”, this is what I do not want, I want it below the image, i have corrected it now, but it shows only plain text. I just saw, that is the field “copyright” of media library, not the image caption, sorry, – probably my English was confusing …

    Please check the ALB page again. I have inserted the image from media library again and have added THERE [bildanfrage] to the “copyright” field.

    – – –

    For the POST (with classic editor) the shortcode should be executed – currently a link “Bild anfragen”.
    I only want that my custom shortcode is executed at this place – whatever the content of this shortcode will be.

    .

    qqq

    #1407580

    Hi BeeCee,

    I see what you meant now, unfortunately, we don’t have control over it, we would need to modify the WordPress Core files to achieve that which is not recommended as it will be removed everytime WordPress is updated.

    Best regards,
    Nikko

    #1407596

    I just have found a solution FOR THE POST at Stackexchange and it is quite simple and works, my shortcode just needs a bit more CSS, that’s all.

    aaaa

    The snippet is:

    function my_caption_shortcode($atts) {
      if (isset($atts['caption'])) {
        // avoid endless loop
        remove_filter( current_filter(), __FUNCTION__);
        // apply shortcodes
        $atts['caption'] = do_shortcode($atts['caption']);
        // restore filter
        add_filter(current_filter(), __FUNCTION__);
      }
      return $atts;
    }
    
    add_filter("shortcode_atts_caption", "my_caption_shortcode");

    Source

    But for the copyright field in media liberary image it works NOT.
    Do you have a suggestion based on this snippet for this?

    #1407599

    Hi BeeCee,

    Thanks for posting the solution that worked. :)
    As for those fields in the media library, it’s really in the WordPress Core files but someone who is an expert with WordPress might have some workaround for it.

    Best regards,
    Nikko

    #1407639

    But in the media library the “copyright” field … isn’t it a feature of ENFOLD?

    #1407685

    Hi BeeCee,

    Yes, it is added by Enfold.
    To be clear, the desired output is the image caption in the private content?
    I think the easiest solution for it is just to use caption and then if you’re using ALB, don’t use image instead use a Text Block and then use it to add the image with the caption.

    Best regards,
    Nikko

    #1407741

    Probably my English is not good enough … I mean how can I extend this code snippet (that I have placed in “Code Snippets” plugin), so that shortcodes are also executed in this “copyright” field in media library, so that I can put a shortcode in exactly this field next to the copyright name (for example):

    function my_caption_shortcode($atts) {
      if (isset($atts['caption'])) {
        // avoid endless loop
        remove_filter( current_filter(), __FUNCTION__);
        // apply shortcodes
        $atts['caption'] = do_shortcode($atts['caption']);
        // restore filter
        add_filter(current_filter(), __FUNCTION__);
      }
      return $atts;
    }
    
    add_filter("shortcode_atts_caption", "my_caption_shortcode");
    #1407822

    Hi BeeCee,

    Please try to replace it with:

    function my_caption_shortcode($atts) {
      if (isset($atts['caption'])) {
        // avoid endless loop
        remove_filter(current_filter(), __FUNCTION__);
        
        // apply shortcodes
        $atts['caption'] = do_shortcode($atts['caption']);
    	
        // retrieve copyright field value
        $attachment_id = str_replace('attachment_', '', $atts['id']);
        $copyright = get_post_meta($attachment_id, '_avia_attachment_copyright', true);
        if ($copyright) {
          $atts['caption'] .= do_shortcode($copyright);
        }
        
        // restore filter
        add_filter(current_filter(), __FUNCTION__);
      }
      
      return $atts;
    }
    
    add_filter("shortcode_atts_caption", "my_caption_shortcode");

    Best regards,
    Nikko

    #1407859

    Thank you, it works fine concerning the shortcode, working in the copyright field in media library…

    … BUT only if I add a caption to the image, too, not only a copyright + shortcode.

    When I add only someting in the “copyright” field in media library to the image, then nothing is shown with the image in frontend:

    x1
    .

    However, when I add a caption and then the copyright + shortcode (= ok) in the “copyright” field in media library, then all of this is shown:
    .

    x2

    Could you please alter your latest snippet in that way, that I do not always need an image caption for the output of the “copyright” + shortcode within the copyright-field in media library?
    Thank you.

    #1407892

    Hi BeeCee,

    You are correct, it only triggers when caption is set, since the filter was inside a caption.

    Please try this code (but it does not work well alongside caption) and the problem is also it only works during the image is added (via classic editor) and not when the image has already been added:

    function add_copyright_to_image_send_to_editor($html, $id, $caption, $title, $align, $url, $size, $alt) {
      // Get the copyright information from the attachment
      $copyright = get_post_meta($id, '_avia_attachment_copyright', true);
    
      // Append the copyright notice to the HTML markup
      if (!empty($copyright)) {
        $html .= '<div class="image-copyright">' . esc_html($copyright) . '</div>';
      }
    
      return $html;
    }
    
    add_filter('image_send_to_editor', 'add_copyright_to_image_send_to_editor', 10, 8);

    I hope this helps.

    Best regards,
    Nikko

    #1407943

    Thank you, it works so far, but I understand the problem that you have described. For this reason I will keep your first snippet, then it works fine. Thanks a lot for your help!

    #1407991

    Hi BeeCee,

    You’re welcome :)
    Thanks as well for using Enfold and have a great weekend!

    Best regards,
    Nikko

Viewing 18 posts - 1 through 18 (of 18 total)
  • The topic ‘How to use shortcodes within the image caption?’ is closed to new replies.