I'm trying to create a shortcode that will search within some specified text for a link and replace it with one that I specify. For example:
[scode]Click on <a href="www.X.com">this link</a>[scode] [scode]Click on <a href="www.Y.com">this link</a>[scode]
...will both be changed to:
[scode]Click on <a href="www.Z.com">this link</a>[scode]
I'm trying to put together a function that will search for links and replace them with the one that I specify. Here's what I have right now:
// Adds [hide] shortcode for hiding content from non-registered users. function hide_text( $atts,$content) { if ( is_user_logged_in () ) { return $content; } else { $pattern = '(?<=href=("|\'))[^"\']+(?=("|\'))'; $newurl = "http://replacementurl.com"; $content = preg_replace($pattern,$newurl,$content); echo $content; } } add_shortcode( 'hide', 'hide_text' );
This just crashes the site, though. I'm not a PHP expert (much less an expert on regex), but are there at least any glaring irregularities in my code?
Or perhaps there's just a better way to go about this and I'm making it way more complicated than it needs to be?
UPDATE:
As suggested, I turned on debug log, which showed me that I had an extra }
in the code. Updated code is shown above. Now the site isn't crashing, but the link and text within the shortcode is completely erased, just blank.
;
at the end of the$newurl
line.;
in there, sorry.}
in there. I removed it, but now the link (and the text) is missing entirely, just a blank space. Will update answer.