Skip to main content
2 of 2
deleted 33 characters in body
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

Strip links from the_content

I'm trying to strip all the a href tags from the_content(); when displayed on the front end. If I use get_the_content(); and preg_replace it works fine, however, get_the_content doesn't show formatting such as <p> etc. When used with the_content();, it doesn't strip the links out.

This strips out the links, but doesn't show the formatting such as <p>:

$the_content = get_the_content();
$bad_tags = array('/<a title=\"(.*?)\" href=\"(.*?)\">/', '/<\/a>/');
$strip_tags = preg_replace($bad_tags, "", $the_content);
echo $strip_tags;

This DOESN'T strip out the links but does show the formatting:

$the_content = the_content();
$bad_tags = array('/<a title=\"(.*?)\" href=\"(.*?)\">/', '/<\/a>/');
$strip_tags = preg_replace($bad_tags, "", $the_content);
echo $strip_tags;
The Sumo
  • 384
  • 2
  • 4
  • 19