Fix Relative Links and Images in Feeds
Turns relative links and image paths such as "/about/" in feed content into full URLs, so they work in feed readers and RSS emails.
Opens WPIDE on
Code
$wpide_absolute_feed_urls = function ( $content ) {
$base = untrailingslashit( home_url() );
$result = preg_replace_callback( '/\s(href|src)=(["\'])(\/(?!\/)[^"\']*)\2/i', function ( $m ) use ( $base ) {
return ' ' . $m[1] . '=' . $m[2] . esc_url( $base . $m[3] ) . $m[2];
}, $content );
return null === $result ? $content : $result;
};
add_filter( 'the_content_feed', $wpide_absolute_feed_urls, 40 );
add_filter( 'the_excerpt_rss', $wpide_absolute_feed_urls, 40 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.