I suggest you to use fetch_feed()
instead. Example:
$rss_feed = fetch_feed( 'https://canpl.ca/feed/rss2/' );
if ( ! is_wp_error( $rss_feed ) ) {
echo '<ul>';
foreach ( $rss_feed->get_items( 0, 5 ) as $item ) {
$enclosure = $item->get_enclosure();
$enc_url = $enclosure ? $enclosure->get_link() : 'No enclosure found';
echo '<li>' . $item->get_title() . "<br>Enclosure URL: $enc_url</li>";
}
echo '</ul>';
}
However, to answer what’s asked in the question:
WordPress does not strip the tag.. It’s the MagpieRSS
class that doesn’t parse that tag.
So if you want to use fetch_rss()
to fetch the feed, then you would have to edit the wp-includes/rss.php
file… and modify the feed_start_element()
method in the MagpieRSS
class, in order to be able to have enclosure
be parsed by that class. You can find an example here (that still worked in WordPress v5.8.3) except that you would add the code there to the rss.php
file and not rss_parse.inc
(which does not exist in WordPress).
But the rss.php
file is actually deprecated, and if you enabled WordPress debugging, you’d see a notice saying, “Deprecated: rss.php is deprecated since version 3.0.0! Use wp-includes/class-simplepie.php instead. in …“.
So as I said earlier, you should use fetch_feed()
. 🙂