Title: Replacing Code in Parsing Posts
Last modified: August 18, 2016

---

# Replacing Code in Parsing Posts

 *  Resolved [Sean](https://wordpress.org/support/users/aristophanes/)
 * (@aristophanes)
 * [20 years, 1 month ago](https://wordpress.org/support/topic/replacing-code-in-parsing-posts/)
 * I’m not sure if I’m phrasing my question correctly, but I would like to know 
   how I could use php to replace certain code in a post with something else. For
   example, if I make a post with tags in it (say, `<x>`), will I be able to write
   something in the post loop which will replace `<x>` with `<y>` or even eliminate
   it altogether?
 * My specific case is with the [WP Print plugin](http://www.lesterchan.net/portfolio/programming.php).
   There are tags in my posts which I want to appear when the visitor is viewing
   it onscreen, but which I don’t want to print. So when the plugin parses (is that
   the right word?) the post information, I would like to write in an exclusion 
   rule to take out the tags _and_ their contents. If I’m not mistaken, this rules
   out the use of `str_replace`.
 * The plugin seems to use a standard post loop.
 * Thanks for taking the time to read this.
 * (Also, is there a way to preserve tabs and spaces inside the backticks when posting
   here?)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Austin Matzko](https://wordpress.org/support/users/filosofo/)
 * (@filosofo)
 * [20 years, 1 month ago](https://wordpress.org/support/topic/replacing-code-in-parsing-posts/#post-388799)
 * Yes, you can do this, using the “the_content” filter hook. (See [here](http://codex.wordpress.org/Plugin_API)
   for more on the filter hooks, and [here](http://wordpress.org/support/topic/72388?replies=4#post-378524)
   for an example of using “the_content” to replace something).
 *  Thread Starter [Sean](https://wordpress.org/support/users/aristophanes/)
 * (@aristophanes)
 * [20 years, 1 month ago](https://wordpress.org/support/topic/replacing-code-in-parsing-posts/#post-388825)
 * Thanks very much filosofo.
 *  Thread Starter [Sean](https://wordpress.org/support/users/aristophanes/)
 * (@aristophanes)
 * [20 years, 1 month ago](https://wordpress.org/support/topic/replacing-code-in-parsing-posts/#post-388933)
 * I’ve had a chance to study the links you gave me closer – thanks, they were very
   helpful. However I don’t see a php function I could use that will replace not
   just the tags, but their contents as well. Doesn’t `preg_replace` just replace
   one thing for another?
 *  [Austin Matzko](https://wordpress.org/support/users/filosofo/)
 * (@filosofo)
 * [20 years, 1 month ago](https://wordpress.org/support/topic/replacing-code-in-parsing-posts/#post-389006)
 * > However I don’t see a php function I could use that will replace not just the
   > tags, but their contents as well. Doesn’t preg_replace just replace one thing
   > for another?
 * I’m not really sure what you mean: filtering “the_content” will allow to replace
   anything appearing within your post, tags and all.
 * preg_replace uses regular expressions, so you can have it search and replace 
   patterns, not just strings. In other words, you can set it up to replace everything
   between tags <x> and </x>, including the tags themselves, if you want.
 *  Thread Starter [Sean](https://wordpress.org/support/users/aristophanes/)
 * (@aristophanes)
 * [20 years, 1 month ago](https://wordpress.org/support/topic/replacing-code-in-parsing-posts/#post-389177)
 * Oh, ok – thanks:) I must have misread the php website.
 *  Thread Starter [Sean](https://wordpress.org/support/users/aristophanes/)
 * (@aristophanes)
 * [20 years, 1 month ago](https://wordpress.org/support/topic/replacing-code-in-parsing-posts/#post-389235)
 * For those new to PHP like me, this code will remove embedded tags and their contents.
   You can stick it in wherever you want the php to apply (in my case, within another
   plugin).
 * `function remove_embedded ($content) {
    return preg_replace('/<tag>(.*)</tag>/',"",
   $content); } add_filter('the_content','remove_embedded');
 * To understand the code you will want to read the notes appended to [this page](http://au3.php.net/preg_replace).
 * Thanks again filosofo.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Replacing Code in Parsing Posts’ is closed to new replies.

## Tags

 * [embed](https://wordpress.org/support/topic-tag/embed/)
 * [embedded](https://wordpress.org/support/topic-tag/embedded/)
 * [exclude](https://wordpress.org/support/topic-tag/exclude/)
 * [strip](https://wordpress.org/support/topic-tag/strip/)
 * [tags](https://wordpress.org/support/topic-tag/tags/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [Sean](https://wordpress.org/support/users/aristophanes/)
 * Last activity: [20 years, 1 month ago](https://wordpress.org/support/topic/replacing-code-in-parsing-posts/#post-389235)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
