How do I get attachment_id?

Question

This is trivial, but I can’t really find an answer, I’ve seen some hints like search in database etc., but this is madness.

Basically, I have a metabox with upload button that allows you to upload or pick existing image from Media Library, that’s how I get the URL of this image:

$my_image = get_post_meta($post->ID, '_my_image_metabox', true);

Of course I do want exact size of this image to be displayed so I need to use:

wp_get_attachment_image_src($attachment_id, 'desired-size');

The question is how do I get attachment_id of _my_image_metabox? I’ve been searching literally everywhere and there are no sane answers, how is wp_get_attachment_image_src() supposed to work if there’s not a single line on getting $attachment_id in Codex whatsoever? 🙂

[updated]

var_dump($my_image) returns:

array(1) { [0]=> string(86) "http://localhost/theme/wp-content/uploads/2014/10/my_image.jpg" }

Saving the data is pretty complicated as I use my own metabox generating class, something like this: https://github.com/nyordanov/SmartMetaBox/blob/master/SmartMetaBox.php

How do I inject the proper attachment_id to my_image then? I’d add I have multiple (10) fields like this one and would like every single one to have separate, unique attachment_id.

0
, Wordpressor 1 year 2020-07-15T16:11:19-05:00 0 Answers 66 views 0

Leave an answer

Browse
Browse