• I’m hoping to share images to facebook using their attachment URL. I see this can be done by including a longdesc attribute on the img tag giving the attachment page URL.

    I can prove this works by:
    – adding the share-image class to the coblock masonry gallery
    – inspect the image and edit the data-link attribute renaming it to longdesc
    – clicking the facebook share now tries to share the attachment page and shows the correct image

    My issue is how to go about adding a longdesc attribute to the image tags.

    I’ve tried adding filters like so:

    function eis_add_longdesc($html, $id, $caption, $title, $align, $url) 
    {
      $html = str_replace( " src=", " longdesc={$url} src=", $html );
      return $html;
    }
    add_filter( 'image_send_to_editor', 'eis_add_longdesc', 999 );

    and:

    function eis_add_longdesc2($html, $id, $alt, $title) 
    {
      $html = str_replace( " src=", " longdesc= src=", $html );
      return $html}
    add_filter('get_image_tag', 'eis_add_longdesc2');

    Both made no difference. I know the code in each case was incomplete, but I should’ve seen longdesc appear as an attribute of the img tag and did not.

    I’m open to any solution including trying to edit the caption through code to add a facebook share I can create manually. I’ve attempted this also with no luck. I’m not a trained wordpress developer, but am quite good at coding and have been dabbling for a few months.

    • This topic was modified 5 years, 2 months ago by rdebruyn.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Thread Starter rdebruyn

    (@rdebruyn)

    I found an imperfect solution:

    function eis_add_longdesc3( $content ) 
    {
      //should replace data-link in coblock gallery images to longdesc so addtoany image share works
      return str_replace( " data-link=", " longdesc=", $content);
    }
    add_filter( 'the_content', 'eis_add_longdesc3' );

    I would rather be able to add the longdesc attribute to all images, but in the end, this filter was the only one that modified the output html. Since the data-link attribute already had the attachment page URL on images from the coblock masonry gallery, I could make all those images shareable. Since the page being shared is the attachment page, it picks up the og meta info from that attachment page.

    So, in the end, I have a partial solution. I would still take any advice for better or alternative solutions.

Viewing 1 replies (of 1 total)
  • The topic ‘sharing images – how do I add longdesc to img tag?’ is closed to new replies.