wp_is_xml_request() WordPress Function

The wp_is_xml_request() function checks whether the current request is an XMLHttpRequest (AJAX). This is useful for checking if an AJAX request is made, and then performing certain actions if it is. For example, you could use this function to return a different response if an AJAX request is made, or to run a different set of code.

wp_is_xml_request() #

Checks whether current request is an XML request, or is expecting an XML response.


Return

(bool) True if Accepts or Content-Type headers contain text/xml or one of the related MIME types. False otherwise.


Top ↑

Source

File: wp-includes/load.php

function wp_is_xml_request() {
	$accepted = array(
		'text/xml',
		'application/rss+xml',
		'application/atom+xml',
		'application/rdf+xml',
		'text/xml+oembed',
		'application/xml+oembed',
	);

	if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
		foreach ( $accepted as $type ) {
			if ( false !== strpos( $_SERVER['HTTP_ACCEPT'], $type ) ) {
				return true;
			}
		}
	}

	if ( isset( $_SERVER['CONTENT_TYPE'] ) && in_array( $_SERVER['CONTENT_TYPE'], $accepted, true ) ) {
		return true;
	}

	return false;
}


Top ↑

Changelog

Changelog
VersionDescription
5.2.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.