How to show content without html tags in feed of wordpress?

I want to remove all html tags from my custom wordpress feed. Is it possible? How to do it? I tried to use str.replace(/</?[^>]+>/gi, '') but it doesn’t help me at all. Here is the code i used to create custom feed. I am using Yoast SEO Plugin.

<?php
/*
Template Name: Custom Feed
*/

$numposts = 10;

function yoast_rss_date( $timestamp = null ) {
  $timestamp = ($timestamp==null) ? time() : $timestamp;
  echo date(DATE_RSS, $timestamp);
}

function yoast_rss_text_limit($string, $length, $replacer = '...') { 
  $string = strip_tags($string);
  if(strlen($string) > $length) 
    return (preg_match('/^(.*)W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;   
  return $string; 
}

function rss_noiframe($content) {
    $content = preg_replace( '/<iframe(.*)/iframe>/is', '', $content );
    $content = preg_replace( '/<script(.*)/script>/is', '', $content );
    return $content;
}

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


$posts = query_posts('showposts='.$numposts.'&cat=7');

$lastpost = $numposts - 1;

header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>


<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:media="http://search.yahoo.com/mrss/"
 xmlns:atom="http://www.w3.org/2005/Atom"
 xmlns:georss="http://www.georss.org/georss">

<channel>
  <title>My Website Feed</title>
  <link>http://www.mywebsite.com/</link>
  <description>The latest blog posts from mywebsite.com.</description>
  <language>en-us</language>
  <pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
  <lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
  <managingEditor>admin@mywebsite.com (Admin Admin)</managingEditor>
 <atom:link href='http://pubsubhubbub.superfeedr.com/' rel='hub' /> 
<atom:link href="http://www.mywebsite.com/feed/" rel="self" type="application/rss+xml" />

<?php foreach ($posts as $post) { ?>
    <?php
        global $post;
        $author_id=$post->post_author;
    ?>

    <item>
            <title><?php echo get_the_title($post->ID); ?></title>
        <dc:creator><?php the_author_meta( 'user_nicename', $author_id ); ?></dc:creator>
            <link><?php echo get_permalink($post->ID); ?></link>
            <description><?php echo '<![CDATA['.rss_noiframe($post->post_content).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
        <pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
            <guid><?php echo get_permalink($post->ID); ?></guid>
<?php
$categories = get_the_category();
foreach ( $categories as $category ) { 
?>      
        <category><?php echo '<![CDATA['.$category->name.']';  ?></category> <?php } ?>


    </item>
<?php } ?>

</channel>
</rss>

Related posts

1 comment

  1. As Qirel mention I replaced

    <description><?php echo '<![CDATA['.rss_noiframe($post->post_content).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    

    TO

            <description><?php echo '<![CDATA['.strip_tags(rss_noiframe($post->post_content)).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    

    Thank You

Comments are closed.