To remove all GET parameters from a URL in PHP, including in WordPress, you can use the parse_url
function to parse the URL, and then reconstruct it without the query parameters. In WordPress, you can also use built-in functions for URL manipulation.
Example in plain PHP:
$url = 'https://example.com/page?param1=value1¶m2=value2';
$parsed_url = parse_url($url);
// Remove GET parameters
$clean_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];
echo $clean_url; // https://example.com/page
Example for WordPress:
In WordPress, you can use the remove_query_arg
function, which removes GET parameters:
$url = 'https://example.com/page?param1=value1¶m2=value2';
// Remove all parameters
$clean_url = remove_query_arg( array_keys( wp_parse_args( $_SERVER['QUERY_STRING'] ) ), $url );
echo $clean_url; // https://example.com/page
This approach removes all query parameters from the current URL.