Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any away to get absolute path of product image/featured image in Wordpress/WooCommerce

How can i get absolute path of product Image or featured image of any post/product page?

I tried to like this:

$product = wc_get_product( $postid);
$product->get_image('large'); //output like <img src="url of image" alt="xyz"/>

Then i tried to get src path from image tag (from above output) using php prag_match function. then split it by domain url and got remaining path and created absolute path.

$filename = getcwd().'/wp-content/uploads/2016/02/xyz.jpg';

It works for me but it seems complex and managed thing. Please let me know if there any easiest way?

like image 770
vin Avatar asked Oct 21 '25 02:10

vin


1 Answers

$feat_image = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
echo esc_url( $feat_image );
  • https://wordpress.org/support/topic/getting-a-post-featured-image-url
  • https://wordpress.org/support/topic/retrieveing-featured-image-url
  • https://developer.wordpress.org/reference/functions/wp_get_attachment_url/

To get the system path, Konstantin Kovshenin suggests the following:

$url       = wp_get_attachment_url( $post_ID );
$uploads   = wp_upload_dir();
$file_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
  • https://kovshenin.com/2011/attachments-filename-and-directory-in-wordpress/
  • https://developer.wordpress.org/reference/functions/wp_upload_dir/
like image 140
admcfajn Avatar answered Oct 23 '25 17:10

admcfajn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!