Skip Navigation

[Resolved] PHP: image field type to get the attachment ID, not the attachment URL

This support ticket is created 4 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Clifford 4 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1431809

I reviewed https://toolset.com/documentation/customizing-sites-using-php/functions/#image and didn't see a built-in way to return the Attachment ID instead of the Attachment URL.

Please advise. If not available at this time, please add to your feature requests.

#1432415

Hi Clifford,

Thank you for contacting us and I'd be happy to assist.

Your observation is correct and Types Fields API ( https://toolset.com/documentation/customizing-sites-using-php/functions/#image ), doesn't offer a built-in method to get the attachment ID.

You're welcome to submit this as feature request through our dedicated form:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

For now, you can use "attachment_url_to_postid" function from WordPress ( ref: https://developer.wordpress.org/reference/functions/attachment_url_to_postid/ ) in a custom shortcode, to get the attachment ID through Attachment URL.

Example:


function get_image_id_from_url_func( $atts )
{
	$a = shortcode_atts( array(
		'url' => ''
	), $atts );

	// get the image's ID from its URL
	$image_id = attachment_url_to_postid( $atts['url'] );

	if( !empty($image_id) ){
		return $image_id;
	}
}
add_shortcode( 'get_image_id_from_url', 'get_image_id_from_url_func' );

Note: The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

After that, you'll be able to use this new shortcode, to get the ID from the image field like this:


[get_image_id_from_url url="[types field='image-field-slug' output='raw'][/types]"]

You'll replace "image-field-slug" with the actual slug of your image field.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1433003

Thanks for such a helpful response!

I saw attachment_url_to_postid() but thought it was to get the Post the Media File was attached to, not the File's own Post ID. That's what I needed.

As a suggestion, I'd request the Image field to add an 'ID' output attribute, alongside 'raw' and 'normal'