WP_REST_Templates_Controller::_sanitize_template_id() WordPress Method

The WP_REST_Templates_Controller::_sanitize_template_id() method is used to sanitize a template id before it is used in a REST request. This is done to ensure that the template id is a valid integer and that it does not contain any invalid characters.

WP_REST_Templates_Controller::_sanitize_template_id( string $id ) #

Requesting this endpoint for a template like ‘twentytwentytwo//home’ requires using a path like /wp/v2/templates/twentytwentytwo//home. There are special cases when WordPress routing corrects the name to contain only a single slash like ‘twentytwentytwo/home’.


Description

This method doubles the last slash if it’s not already doubled. It relies on the template ID format {theme_name}//{template_slug} and the fact that slugs cannot contain slashes.

Top ↑

See also


Top ↑

Parameters

$id

(string)(Required)Template ID.


Top ↑

Return

(string) Sanitized template ID.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

	public function _sanitize_template_id( $id ) {
		$id = urldecode( $id );

		$last_slash_pos = strrpos( $id, '/' );
		if ( false === $last_slash_pos ) {
			return $id;
		}

		$is_double_slashed = substr( $id, $last_slash_pos - 1, 1 ) === '/';
		if ( $is_double_slashed ) {
			return $id;
		}
		return (
			substr( $id, 0, $last_slash_pos )
			. '/'
			. substr( $id, $last_slash_pos )
		);
	}

Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.