How to Clean up URL Query Arguments in WordPress Settings Pages

I seldom use the Settings API for building my plugins admin pages because I love to be in control of layout and design.

When the form for configuring my plugin is saved successfully or an error occurred, I usually add a query parameter to the page URL handy in displaying an admin notice to that effect.

WordPress 4.2 release included a nifty feature that removes query parameters handy in displaying admin notices.

Prior to this version, when a user saves my plugin settings form and then save again, double of such query parameter will be added to the URL.

For example, when a user edit a custom login form in ProfilePress plugin, a login-edited=true is added to the URL. And when they do a second time, the query parameter becomes double thus, an unexpected behavior.

An array list of query parameters removed by WordPress is returned by wp_removable_query_args() and it includes a removable_query_args filter. Thus, we can add our plugin query parameters to the list like so:


add_filter( 'removable_query_args', 'pp_remove_query_args' );

function pp_remove_query_args( $args ) {
	$args[] = 'password-reset-edited';
	$args[] = 'password-reset-added';
	$args[] = 'registration-added';
	$args[] = 'registration-edited';
	$args[] = 'login-added';
	$args[] = 'login-edited';
	$args[] = 'settings-update';
	$args[] = 'edit-profile-edited';
	$args[] = 'edit-profile-added';

	return $args;
}

Note: the query parameters in the code above are that of my ProfilePress plugin.

Don’t miss out!
Subscribe to My Newsletter
Invalid email address