add_query_arg() and remove_query_arg(): build URLs safely
Admin redirects with ?status=saved, filter links that keep the other filters, URLs without tracking parameters: building query strings by hand with ?, & and parse_url() breaks as soon as the URL already has a query string or a value needs encoding. WordPress has two helpers for it, and one detail about their output that has caused many security bugs.
Problem: After saving a settings form, the plugin must redirect back to the same admin page with status=saved added and an old error parameter removed, and the page shows “clear filters” links.
Solution: Use add_query_arg() to add or replace parameters and remove_query_arg() to remove them. Their result is not escaped: pass it through esc_url() before printing it into HTML.
Adding and replacing
<?php // One parameter to a given URL $url = add_query_arg( 'status', 'saved', admin_url( 'options-general.php?page=ha-settings' ) ); // .../wp-admin/options-general.php?page=ha-settings&status=saved // Several at once $url = add_query_arg( array( 'status' => 'saved', 'tab' => 'general' ), admin_url( 'options-general.php' ) ); // An existing parameter is replaced $url = add_query_arg( 'paged', 3, 'https://example.com/shop/?paged=1&orderby=price' ); // https://example.com/shop/?paged=3&orderby=price
Without the URL argument, both functions work on the current request URL ($_SERVER['REQUEST_URI']). Values are not URL-encoded automatically; encode values that can contain spaces or & with rawurlencode().
Removing
<?php $clean = remove_query_arg( 'error', $url ); $clean = remove_query_arg( array( 'utm_source', 'utm_medium', 'utm_campaign' ), $url );
A redirect after saving
<?php
add_action( 'admin_post_ha_save_settings', function () {
check_admin_referer( 'ha_save_settings' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Not allowed.', 'textdomain' ), 403 );
}
update_option( 'ha_setting', sanitize_text_field( wp_unslash( $_POST['ha_setting'] ?? '' ) ) );
$back = wp_get_referer() ? wp_get_referer() : admin_url( 'options-general.php?page=ha-settings' );
wp_safe_redirect( add_query_arg( 'status', 'saved', remove_query_arg( 'error', $back ) ) );
exit;
} );
For redirects, the URL is not escaped for HTML; wp_safe_redirect() checks the host, as explained in wp_redirect() and wp_safe_redirect().
Printing a link
<?php $clear = remove_query_arg( array( 'filter_cat', 'filter_tag' ) ); echo '<a href="' . esc_url( $clear ) . '">' . esc_html__( 'Clear filters', 'textdomain' ) . '</a>';
The security detail
Called without a URL, the functions return the current URL with all its parameters, as the visitor sent them. Printed without escaping, a link like ?x=”><script>… injects HTML into your page. In April 2015 dozens of popular plugins had exactly this XSS bug, and a coordinated security release fixed them together. The rule since then: every result of add_query_arg() and remove_query_arg() that goes into HTML gets esc_url().
NOTE: Escape at output, not when building. Keep the raw URL for redirects and HTTP requests (esc_url_raw() if it must be cleaned), and use esc_url() only in href, src and action attributes: it also converts & to &, which is right in HTML but breaks a Location header.