How to remove all get parameters from a string in php, WordPress

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&param2=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&param2=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.

How useful is the publication?

Click on a star to rate it!

Average score 5 / 5. Number of grades: 1

No ratings yet. Rate it first.

Similar posts

How to exclude posts with parent post in wp_query, WordPress

To exclude posts that have a parent (i.e., child posts) in a WP_Query request, you can use the post_parent argument. This argument controls whether the post has a parent or not. To exclude child posts, set the condition post_parent => 0, which means that only top-level posts (posts without a parent) will be included in…
Read more