0

I am trying to append a php query to the end of links from my website to a sponsor site using the_content filter and preg_replace(). I have tested my regex expression on regexr.com and it works also I have used print_r() to test that the function is being called but for some reason the links are not being changed in practice.

Here is the code I am having trouble with

add_filter('the_content', 'linkAppend');

function linkAppend($content) {
  global $referalString;

  preg_replace('/\/\/(www|launch)?\.?(solarwinds\.com)\/[^"]*/g','$&?cmp='.$referalString, $content);
  return $content;
}   

If someone could point me in the right direction or let me know where I went wrong it would be greatly appreciated.

  • You need to either assign $content = preg_replace(... or just return preg_replace(... – bonger Dec 3 '15 at 17:07
2

Turns out I had multiple problems, @bonger pointed out the first one and then I found and solved issues with the global flag on the regular expression not being valid in php and me not accounting for links without a / after the .com. Final fix looks like this:

add_filter('the_content', 'linkAppend');

function linkAppend($content) {
  global $referalString;
  $content = preg_replace('/\/\/(www|launch)?\.?(solarwinds\.com)\/?[^"]*/m','$0?cmp='.$referalString, $content);
  return $content;
}   

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.