WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to replace some custom strings in my content before saving the post. I have got the $data["post_content"] which includes strings like %replaceContent:{type}% . Now I need to extract that string, read the {type} and replace the string afterwards depending on what's inside of {type}. I imagine this would best be done using regex, unfortunately I don't really know how to go about this. Ideas?

share|improve this question
up vote 1 down vote accepted

This belongs to stackoverflow. Here's a solution anyway:

$content = preg_replace_callback('/\%replaceContent:{(.*?)}\%/', 'do_replacements', $content);

function do_replacements($matches){

  $type = $matches[1]; // here's your {type}
  $replacement = "...replacement for {$type}";  

  return $replacement; 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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