Title: wp_replace_in_html_tags
Published: August 3, 2015
Last modified: August 20, 2026

---

# wp_replace_in_html_tags( string $haystack, array $replace_pairs ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#user-contributed-notes)

Replaces characters or phrases within HTML elements only.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#parameters)󠁿

 `$haystack`stringrequired

The text which has to be formatted.

`$replace_pairs`arrayrequired

In the form array(`'from'` => `'to'`, …).

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#return)󠁿

 string The formatted text.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#source)󠁿

    ```php
    function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
    	// Find all elements.
    	$textarr = wp_html_split( $haystack );
    	$changed = false;

    	// Optimize when searching for one item.
    	if ( 1 === count( $replace_pairs ) ) {
    		// Extract $needle and $replace.
    		$needle  = array_key_first( $replace_pairs );
    		$replace = $replace_pairs[ $needle ];

    		// Loop through delimiters (elements) only.
    		for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
    			if ( str_contains( $textarr[ $i ], $needle ) ) {
    				$textarr[ $i ] = str_replace( $needle, $replace, $textarr[ $i ] );
    				$changed       = true;
    			}
    		}
    	} else {
    		// Extract all $needles.
    		$needles = array_keys( $replace_pairs );

    		// Loop through delimiters (elements) only.
    		for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
    			foreach ( $needles as $needle ) {
    				if ( str_contains( $textarr[ $i ], $needle ) ) {
    					$textarr[ $i ] = strtr( $textarr[ $i ], $replace_pairs );
    					$changed       = true;
    					// After one strtr() break out of the foreach loop and look at next element.
    					break;
    				}
    			}
    		}
    	}

    	if ( $changed ) {
    		$haystack = implode( $textarr );
    	}

    	return $haystack;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/formatting.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/formatting.php#L757)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/formatting.php#L757-L797)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_html_split()](https://developer.wordpress.org/reference/functions/wp_html_split/)`wp-includes/formatting.php` |

Separates HTML elements and comments from the text.

  |

| Used by | Description | 
| [wpautop()](https://developer.wordpress.org/reference/functions/wpautop/)`wp-includes/formatting.php` |

Replaces double line breaks with paragraph elements.

  | 
| [WP_Embed::autoembed()](https://developer.wordpress.org/reference/classes/wp_embed/autoembed/)`wp-includes/class-wp-embed.php` |

Passes any unlinked URLs that are on their own line to [WP_Embed::shortcode()](https://developer.wordpress.org/reference/classes/wp_embed/shortcode/) for potential embedding.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.2.3](https://developer.wordpress.org/reference/since/4.2.3/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#comment-content-6932)
 2.    [Masoud Najjar Khodabakhsh](https://profiles.wordpress.org/masoudnkh/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/#comment-6932)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_replace_in_html_tags%2F%23comment-6932)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_replace_in_html_tags%2F%23comment-6932)
 4.  Replace “products” with “services” in text
 5.      ```php
         $content = 'Welcome to our website! We offer a variety of products.';
     
         $replacements = array(
           'products' => 'services', // Replace "products" with "services"
         );
     
         $new_content = wp_replace_in_html_tags($content, $replacements);
     
         echo $new_content;
         ```
     
 6.  Output:
      Welcome to our website! We offer a variety of services.
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_replace_in_html_tags%2F%3Freplytocom%3D6932%23feedback-editor-6932)
 8.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/?output_format=md#comment-content-6933)
 9.    [Masoud Najjar Khodabakhsh](https://profiles.wordpress.org/masoudnkh/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/wp_replace_in_html_tags/#comment-6933)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_replace_in_html_tags%2F%23comment-6933)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_replace_in_html_tags%2F%23comment-6933)
 11. Add Attributes to Existing Elements:
 12.     ```php
         $content = ''; // Missing alt attribute
     
         $replacements = array(
           ' '<img alt="description of the image"', // Add alt attribute
         );
     
         $new_content = wp_replace_in_html_tags($content, $replacements);
     
         echo $new_content;
         ```
     
 13.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_replace_in_html_tags%2F%3Freplytocom%3D6933%23feedback-editor-6933)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_replace_in_html_tags%2F)
before being able to contribute a note or feedback.