Title: wp_remove_surrounding_empty_script_tags
Published: November 8, 2023
Last modified: August 20, 2026

---

# wp_remove_surrounding_empty_script_tags( string $contents ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Removes leading and trailing _empty_ script tags.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#description)󠁿

This is a helper meant to be used for literal script tag construction within `wp_get_inline_script_tag()`
or `wp_print_inline_script_tag()`.
It removes the literal values of “” and “” from
around an inline script after trimming whitespace. Typically this is used in conjunction
with output buffering, where `ob_get_clean()` is passed as the `$contents` argument.

Example:

    ```php
    // Strips exact literal empty SCRIPT tags.
    $js = '<script>sayHello();</script>;
    'sayHello();' === wp_remove_surrounding_empty_script_tags( $js );

    // Otherwise if anything is different it warns in the JS console.
    $js = '<script type="module">console.log( "hi" );</script>';
    'console.error( ... )' === wp_remove_surrounding_empty_script_tags( $js );
    ```

### 󠀁[See also](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#see-also)󠁿

 * [wp_print_inline_script_tag()](https://developer.wordpress.org/reference/functions/wp_print_inline_script_tag/)
 * [wp_get_inline_script_tag()](https://developer.wordpress.org/reference/functions/wp_get_inline_script_tag/)

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

 `$contents`stringrequired

Script body with manually created SCRIPT tag literals.

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

 string Script body without surrounding script tag literals, or original contents
if both exact literals aren’t present.

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

    ```php
    function wp_remove_surrounding_empty_script_tags( $contents ) {
    	$contents = trim( $contents );
    	$opener   = '<SCRIPT>';
    	$closer   = '</SCRIPT>';

    	if (
    		strlen( $contents ) > strlen( $opener ) + strlen( $closer ) &&
    		strtoupper( substr( $contents, 0, strlen( $opener ) ) ) === $opener &&
    		strtoupper( substr( $contents, -strlen( $closer ) ) ) === $closer
    	) {
    		return substr( $contents, strlen( $opener ), -strlen( $closer ) );
    	} else {
    		$error_message = __( 'Expected string to start with script tag (without attributes) and end with script tag, with optional whitespace.' );
    		_doing_it_wrong( __FUNCTION__, $error_message, '6.4' );
    		return sprintf(
    			'console.error(%s)',
    			wp_json_encode(
    				sprintf(
    					/* translators: %s: wp_remove_surrounding_empty_script_tags() */
    					__( 'Function %s used incorrectly in PHP.' ),
    					'wp_remove_surrounding_empty_script_tags()'
    				) . ' ' . $error_message
    			)
    		);
    	}
    }
    ```

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

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

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

Encodes a variable into JSON, with some confidence checks.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [_doing_it_wrong()](https://developer.wordpress.org/reference/functions/_doing_it_wrong/)`wp-includes/functions.php` |

Marks something as being incorrectly called.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#)

| Used by | Description | 
| [WP_Customize_Manager::remove_frameless_preview_messenger_channel()](https://developer.wordpress.org/reference/classes/wp_customize_manager/remove_frameless_preview_messenger_channel/)`wp-includes/class-wp-customize-manager.php` |

Removes customize_messenger_channel query parameter from the preview window when it is not in an iframe.

  | 
| [WP_Customize_Manager::customize_pane_settings()](https://developer.wordpress.org/reference/classes/wp_customize_manager/customize_pane_settings/)`wp-includes/class-wp-customize-manager.php` |

Prints JavaScript settings for parent window.

  | 
| [wp_post_preview_js()](https://developer.wordpress.org/reference/functions/wp_post_preview_js/)`wp-includes/functions.php` |

Outputs a small JS snippet on preview tabs/windows to remove `window.name` when a user is navigating to another page.

  | 
| [login_footer()](https://developer.wordpress.org/reference/functions/login_footer/)`wp-login.php` |

Outputs the footer for the login page.

  | 
| [login_header()](https://developer.wordpress.org/reference/functions/login_header/)`wp-login.php` |

Outputs the login page header.

  | 
| [WP_Customize_Manager::customize_preview_settings()](https://developer.wordpress.org/reference/classes/wp_customize_manager/customize_preview_settings/)`wp-includes/class-wp-customize-manager.php` |

Prints JavaScript settings for preview frame.

  | 
| [WP_Customize_Manager::wp_die()](https://developer.wordpress.org/reference/classes/wp_customize_manager/wp_die/)`wp-includes/class-wp-customize-manager.php` |

Custom wp_die wrapper. Returns either the standard message for UI or the Ajax message.

  | 
| [wp_customize_support_script()](https://developer.wordpress.org/reference/functions/wp_customize_support_script/)`wp-includes/theme.php` |

Prints a script to check whether or not the Customizer is supported, and apply either the no-customize-support or customize-support class to the body.

  | 
| [WP_Widget_Categories::widget()](https://developer.wordpress.org/reference/classes/wp_widget_categories/widget/)`wp-includes/widgets/class-wp-widget-categories.php` |

Outputs the content for the current Categories widget instance.

  | 
| [WP_Widget_Archives::widget()](https://developer.wordpress.org/reference/classes/wp_widget_archives/widget/)`wp-includes/widgets/class-wp-widget-archives.php` |

Outputs the content for the current Archives widget instance.

  |

[Show 5 more](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_remove_surrounding_empty_script_tags/?output_format=md#)

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

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

## User Contributed Notes

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