Title: wp_attach_theme_preview_middleware
Published: August 8, 2023
Last modified: August 20, 2026

---

# wp_attach_theme_preview_middleware()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_attach_theme_preview_middleware/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/wp_attach_theme_preview_middleware/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_attach_theme_preview_middleware/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_attach_theme_preview_middleware/?output_format=md#changelog)

Adds a middleware to `apiFetch` to set the theme for the preview.

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

This adds a `wp_theme_preview` URL parameter to API requests from the Site Editor,
so they also respond as if the theme is set to the value of the parameter.

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

    ```php
    function wp_attach_theme_preview_middleware() {
    	// Don't allow non-admins to preview themes.
    	if ( ! current_user_can( 'switch_themes' ) ) {
    		return;
    	}

    	wp_add_inline_script(
    		'wp-api-fetch',
    		sprintf(
    			'wp.apiFetch.use( wp.apiFetch.createThemePreviewMiddleware( %s ) );',
    			wp_json_encode( sanitize_text_field( wp_unslash( $_GET['wp_theme_preview'] ) ), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
    		),
    		'after'
    	);
    }
    ```

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

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

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

Adds extra code to a registered script.

  | 
| [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.

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

Returns whether the current user has the specified capability.

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

Sanitizes a string from user input or from the database.

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

Removes slashes from a string or recursively removes slashes from strings within an array.

  |

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

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

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

## User Contributed Notes

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