Find and replace text in a custom field when saving

I’m looking for a solution to find and replace text in a custom field when saving. I have a custom field called podcast_url where a URL is entered in a text box. The url is usually something like https://feeds.soundcloud.com/stream/432789081-dtns-dtns-3266-my-cobot-buddy.mp3. I’m trying to find a way to replace https:// with https://dts.podtrac.com/redirect.mp3/.

The code below does replace it but if I update the post, it keeps adding https://dts.podtrac.com/redirect.mp3/ to the URL like https://dts.podtrac.com/redirect.mp3/dts.podtrac.com/redirect.mp3/feeds.soundcloud.com/stream/432789081-dtns-dtns-3266-my-cobot-buddy.mp3

Code:

$url = str_replace(array(‘http://’,’https://’), ‘https://dts.podtrac.com/redirect.mp3/’, get_post_meta( get_the_ID(), ‘podcast_url’, true ));
$podcast_check = get_post_meta( get_the_ID(), ‘podcast_url’, true );
if ($url && get_post_meta( $post_id, ‘podcast_url’, true ) !== $url) {
update_post_meta( $post_id, ‘podcast_url’, $url );
}

Read more here:: Find and replace text in a custom field when saving

Leave a Reply

Your email address will not be published. Required fields are marked *