# Replace Words in Content on Display

> Swaps words or phrases in post titles, content and excerpts when pages are shown, without editing the posts. Useful after a rebrand or a product rename.

- Type: PHP
- Runs: Frontend only
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: replace, rebrand, text
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/find-and-replace-text/

## Code

```php
// Text to find => replacement.
$wpide_replacements = array(
    'Old Brand Name' => 'New Brand Name',
    'old-product'    => 'new-product',
);

$wpide_replace_text = function ( $text ) use ( $wpide_replacements ) {
    if ( ! is_string( $text ) || '' === $text || is_admin() ) {
        return $text;
    }

    foreach ( $wpide_replacements as $find => $replace ) {
        $result = preg_replace( '/' . preg_quote( $find, '/' ) . '(?![^<]*>)/u', str_replace( array( '\\', '$' ), array( '\\\\', '\\$' ), $replace ), $text );
        $text   = null === $result ? $text : $result;
    }

    return $text;
};

add_filter( 'the_title', $wpide_replace_text );
add_filter( 'the_content', $wpide_replace_text, 30 );
add_filter( 'the_excerpt', $wpide_replace_text );
```

## Notes

Replacements are case-sensitive and skip HTML tags and attributes. The text saved in the database is not changed.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Replace Words in Content on Display" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
