strip_fragment_from_url() WordPress Function

This function is used to remove any fragments from a URL. A fragment is the part of a URL that comes after a "#" symbol. This function is useful for when you want to clean up a URL before sending it to another function, such as one that will check if the URL is valid.

strip_fragment_from_url( string $url ) #

Strips the #fragment from a URL, if one is present.


Parameters

$url

(string)(Required)The URL to strip.


Top ↑

Return

(string) The altered URL.


Top ↑

Source

File: wp-includes/canonical.php

function strip_fragment_from_url( $url ) {
	$parsed_url = wp_parse_url( $url );

	if ( ! empty( $parsed_url['host'] ) ) {
		$url = '';

		if ( ! empty( $parsed_url['scheme'] ) ) {
			$url = $parsed_url['scheme'] . ':';
		}

		$url .= '//' . $parsed_url['host'];

		if ( ! empty( $parsed_url['port'] ) ) {
			$url .= ':' . $parsed_url['port'];
		}

		if ( ! empty( $parsed_url['path'] ) ) {
			$url .= $parsed_url['path'];
		}

		if ( ! empty( $parsed_url['query'] ) ) {
			$url .= '?' . $parsed_url['query'];
		}
	}

	return $url;
}


Top ↑

Changelog

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