Using preg_replace to remove youtube iframe from feed

时间:2018-11-16 02:30:22

标签: iframe youtube rss preg-replace feed

For posts with Youtube URLs I am trying to remove the iframe from the RSS feed generated by our Wordpress site.

I simply want the youtube URL to be fed into the receiving Wordpress sites (as it appears in the original) so the receiving site can convert it to a preview (as these sites are controlled by us).

The RSS feed currently generated by the content contains this:

<content:encoded><![CDATA[<p><iframe width="525" height="295" src="https://https://www.www.youtube.com/embed/H51j54Z8yl8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p></content:encoded> 

But it should be like this (right?):

<content:encoded><![CDATA[<p>https://www.youtube.com/watch?v=H51j54Z8yl8</p></content:encoded>

Question 1: How do I use preg_replace to replace the first part with a space (if the 525 Width and 295 Height changes)? Is there a "wild card" value I can use?

function rss_noiframe($content) {
    $content = preg_replace( '<iframe width="525" height="295" src="', '', $content );

    return $content;
}

add_filter('the_excerpt_rss', 'rss_noiframe');
add_filter('the_content_feed', 'rss_noiframe');

Question 2: If I am not barking up the wrong tree, how do I create an additional preg_replace in this above code to:

Replace the 2nd part:
"embed/"

With:
"watch?v="

And replace the 3rd part:
"?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>"

with:
"" ?

Question 3: Oh, and where do I put this code (I am not a coder) - however I would prefer someone to make a plugin to do this as I am not confident with Child Themes etc.

0 个答案:

没有答案
相关问题