1

I have a product page where you can create your own product and with each choice you make the image that is shown changes.

Once the user clics on a button I want to use a shortcode to get the title and the description, of the currently shown image, from the Media section of Wordpress.

Now I have found a lot of posts with this code (that you have to insert in functions.php)

function wp_get_attachment( $attachment_id ) {

    $attachment = get_post( $attachment_id );
    return array(
        'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'caption' => $attachment->post_excerpt,
        'description' => $attachment->post_content,
        'href' => get_permalink( $attachment->ID ),
        'src' => $attachment->guid,
        'title' => $attachment->post_title
    );
}

And then the function was called with this code

$attachment_meta = wp_get_attachment($attachment_id);

Edit: I found the code on this post here

But the data gathered by this is only about the product page (so the title and the description of the page) and it's not what I'm looking for.

Also the only way I found to get the attachment_id of an image on the page is with this article here But it does so manually and I need to get it done in the shortcode...

Here's the whole code

add_shortcode( 'afficher_produits_devis2', function(){
    $attachment_meta = wp_get_attachment($attachment_id);
    echo $attachment_meta['caption'];
} );

function wp_get_attachment( $attachment_id ) {

    $attachment = get_post( $attachment_id );
    return array(
        'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'caption' => $attachment->post_excerpt,
        'description' => $attachment->post_content,
        'href' => get_permalink( $attachment->ID ),
        'src' => $attachment->guid,
        'title' => $attachment->post_title
    );
}

EDIT 2

I may have found a solution for this conondrum with this post from @Ruvee but now I have to find a way to parse the html of the page and get the URL of the image.

EDIT 3

I found out why I could only get the thumbnail data, turns out the shortcode activated directly when I loaded the product page and not when I clicked the button. So it only activated once and it always got the first image to appear (the thumbnail one).

I corrected the mistake and now it works as I wanted Thanks @Ruvee

Ruvee
  • 8,611
  • 4
  • 18
  • 44
Visual
  • 13
  • 7
  • Does this answer your question? [How to get an image metadata such as alt text, caption and description from media library in wordpress](https://stackoverflow.com/questions/69761934/how-to-get-an-image-metadata-such-as-alt-text-caption-and-description-from-medi) – Ruvee Nov 02 '21 at 14:41
  • _"But the data gathered by this is only about the product page "_ - then what `$attachment_id` contained, was probably the post ID of that page, and not of any actual attachment. But your question lacks context, you have only shown us the function, but not where from / with what parameter you actually call it. – CBroe Nov 02 '21 at 14:41
  • What is wp_get_attachment ? From where you get this $attachment_id ? You share pice of code that its not related to your question. – Snuffy Nov 02 '21 at 14:43
  • There is no reason to use any special function for this. Attachments are still `posts`, you can use `get_post($id)` and it will return all of the data you are looking for - $id is the image id. – disinfor Nov 02 '21 at 15:36
  • I've edited the question @CBroe, you can find the source. Also I didn't find how to get the attachment_id of an image without doing so manually in the back-office, so yeah it's possible the function used the id of the product page – Visual Nov 02 '21 at 15:39
  • @disinfor that's interesting, but I need to get the image id in the shortcode and not insert it manually – Visual Nov 02 '21 at 15:41
  • You need to show us your shortcode too. Did you write the shortcode? We have no idea how you're trying to use the function you pasted from the other SO question. What you have here is only part of what we need to see. – disinfor Nov 02 '21 at 15:42
  • @disinfor I did not write the shortcode, I added the source and the whole code I intend to use for this in the question. I hope it makes it clearer – Visual Nov 02 '21 at 15:48
  • @Ruvee I'm looking into it, it seems very detailed. I have some problems with the back-office at the moment but I'll try those when I can – Visual Nov 02 '21 at 15:55
  • `$attachment_meta = wp_get_attachment($attachment_id);` - a variable `$attachment_id` does not exist in this scope, so the function will likely default to the global post id at this point. If you are not passing any arguments with your shortcode, then how is what you called _"the currently shown image"_ supposed to be identified? – CBroe Nov 03 '21 at 07:04
  • That's what I'm looking for. I don't have lot of experience in PHP, that's why I'm struggling to find an answer. – Visual Nov 03 '21 at 09:55
  • @Ruvee Thanks for the answer but a part of my question remain, the image on the page change a lot... how can I get the image url without inserting it manually in the code ? – Visual Nov 03 '21 at 14:52
  • @Visual There are several ways explained in the answer one of which is to grab the metadata without using the url of the image. You could use the second way, in the wordpress loop, which does not require you to provide the url for your image. – Ruvee Nov 03 '21 at 15:00

1 Answers1

0

"I have to find a way to parse the html of the page and get the URL of the image."

No that's not necessary! Like I said in this answer second way of grabbing the metadata without the actual image url is to use id of the image. You could get id of the image by using get_post_thumbnail_id function.

So your shortcode would be something like this:

add_shortcode( 'afficher_produits_devis2', function(){
    global $post;
    $image_id = get_post_thumbnail_id($post->ID);
    $image_caption = get_post_field('post_excerpt', $image_id);
    echo $image_caption;
} );
Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • But doesn't that take only the thumbnail of the page? – Visual Nov 03 '21 at 16:23
  • It depends on where you want to call it. You didn't mention any details. You could use `et_post_thumbnail_id` function in the wordpress loop as well. This `$image_id = get_post_thumbnail_id(get_the_id());` would do the same thing without using `global $post;`. – Ruvee Nov 03 '21 at 16:25
  • In your product page, you don't even need to use a shortcode to get this task done! You're making it harder for yourself. You could hook into the query for your product page and then use the second solution in my answer. It's very simple. – Ruvee Nov 03 '21 at 16:31
  • Yes I know, but when I try this it only get the data of the original thumbnail and not the one of the alternative image – Visual Nov 03 '21 at 16:32
  • Found out my mistake! – Visual Nov 03 '21 at 16:41