1

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;
2

Use get_the_content(). You just need to use it cleverly. By default, get_the_content() returns the raw, non-formatted post_content field from the post object. In order to get formatted text, you need to run the result from get_the_content() though the the_content filters. This is exactly what the_content() does by default.

You can adjust your code to the following

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

EDIT

From comments, you should upgrade to at least PHP 5.6. Short array syntax only works from PHP 5.4, so that all means that you have a dinosaur version of PHP. This is a huge security risk to your site as all versions below PHP 5.5 is not supported anymore. Take note, PHP 5.5 will reach EOL in July, all updates, accept security updates, have been stopped already

PRE PHP 5.4 version

$the_content = get_the_content();
$bad_tags    = array( '/<a title=\"(.*?)\" href=\"(.*?)\">/', '/<\/a>/' );
$strip_tags  = preg_replace( $bad_tags, "" , $the_content );
echo apply_filters( 'the_content', $strip_tags );
  • Thanks Pieter, that worked, although ['/<a title=\"(.*?)\" href=\"(.*?)\">/', '/<\/a>/'] was giving me an error so I changed it back to array('/<a title=\"(.*?)\" href=\"(.*?)\">/', '/<\/a>/') and it works lovely! – The Sumo Feb 1 '16 at 12:05
  • My pleasure. Upgrade to PHP 5.6. Short array only works from PHP 5.4, so you have a really old PHP version which you should not use due to security issues – Pieter Goosen Feb 1 '16 at 12:07
  • I'm running version 5.5 at the moment. Always dubious about updating php versions with wordpress as it often breaks stuff! – The Sumo Feb 1 '16 at 12:30
  • Actually, I just checked. It was my code editing software that was showing the syntax error. Your original answer works fine with php 5.5 so I have marked it as answered. Thanks. – The Sumo Feb 1 '16 at 12:35
  • Most probably dreamweaver I suspect. The old version throws errors everywhere if you use PHP 5.4+ syntax ;-) – Pieter Goosen Feb 1 '16 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.