Porcellino80
Porcellino80

Reputation: 447

How to change get_the_post_thumbnail_url output with filter or raw php

This is my question:

I want to change the output of this function:

$thumb = get_the_post_thumbnail_url();

The URL will return something like:

http://mywebsite.local/wp-content/uploads/myimage.png

My goal is just to change the first part of the URL with something like:

http://myurl.local/wp-content/uploads/myimage.png

I can always use str_replace:

$otherthumb = str_replace('http://mywebsite.local', 'http://myurl.local', $thumb);

But I was hoping to find something already baked.

Any suggestion?

Thank you in advance!

Upvotes: 1

Views: 1424

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can use wordpress wp_get_attachment_image_src filter hook.

function change_image_url( $image, $attachment_id, $size, $icon ){
    $otherthumb = str_replace('http://mywebsite.local', 'http://myurl.local', $image[0]);
    return $otherthumb;
}
add_filter( 'wp_get_attachment_image_src', 'change_image_url', 10, 4 );

Upvotes: 2

Related Questions