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?
$feat_image = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
echo esc_url( $feat_image );
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With