I'm trying to create a WordPress shortcode (the WordPress part of it isn't that relevant) 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 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?
UPDATE:
I ran debug on the site and found out from the log that there was an extra } in there. Now the site isn't crashing, but the content being echoed is blank... Code updated above
