WP-Mix

A fresh mix of code snippets and tutorials

Restore Removed Query String Paramaters in the WP Admin Area

Developing my WordPress plugins recently, I noticed that Chrome was removing the query-string parameters from the URL as displayed in the browser’s address bar. Likewise Firefox and others were not showing the full URL in the address bar, which was making development difficult, as I was testing URL redirects involving query parameters at the time.

The Problem

Specifically, I was working with GET variables, for example something like the following URL with a couple of parameters:

https://example.com/wp-admin/admin.php?page=bbq_license&settings-updated=true

..kept returning as this in the browser:

https://example.com/wp-admin/admin.php?page=bbq_license

The odd part was that, for a just a brief moment, I could see the part of the URL that was getting removed. So the page was loading with the correct parameters, but they somehow were being removed or hidden after page load. I thought maybe JavaScript was involved..

The Reason

After some searching around for more information or clues, I discovered this Make WordPress Ticket, suggesting something like this:

url = the canonical URL for the address
	if (typeof history.replaceState === 'function') { // check html5 functionality support
	data = {dummy:true};
	history.replaceState(data,'',url);
}

So it turns out that the missing/removed query-string parameters is not a Chrome, Firefox, or any other browser issue, but rather something that the WordPress team implemented intentionally. If you read through the Trac ticket, you can understand the reasoning behind such a decision, to change it up so that one-time use URLs do not mess with anything else.

So I get it, unfortunately there are some real downsides, such as:

  • Removing parameters breaks reliant on-page functionality
  • Requires additional resources and processing to fully resolve
  • Requires developers to take extra steps during development
  • The user cannot view the entire URL, which is important
  • Can be confusing to users who rely on the address bar
  • Changes general/default functionality for fringe case

To me this intentional removal of extra query-string parameters does more harm than good, as it should be left up to developers to properly test and implement their own compatibility and page-processing solutions. Basically just misguided over-engineering type thinking IMO. Like when Google and Firefox played with completely removing the Address Bar several years ago (which turned out to be a total disaster).

Bottom line: this poor decision could have (and should have) been avoided by sticking to one of the Golden Rules of the Web:

Don’t mess with URLs.

The Solution

Fortunately, the WordPress team understands that removing URI parameters is not always desirable, and thus added a filter hook for developers to modify the default “remove-parameter” behavior:

add_filter('removable_query_args', 'custom_function_name'); 

This hook enables us to modify the array of query-string variables that are to be removed. You can learn more about the removable_query_args hook at the WordPress hook directory.

So the solution to anyone who wants to NOT remove any query parameters, and thus display the complete, original URL in the address bar. Here is a drop-in function that just works:

function shapeSpace_enable_admin_query_params($removable_url_params) {
	
	unset($removable_url_params);
	
	$removable_url_params = array();
	
	return $removable_url_params;
	
}
add_filter('removable_query_args', 'shapeSpace_enable_admin_query_params'); 

This simple function intercepts the array of parameters to be removed, and replaces it with an empty array. So the result is that no parameters will be removed, and the full URL will remain intact.

Allow specific parameters

Or, if you want to allow not all, but only specific parameters, the above function can be modified like so:

function shapeSpace_allow_admin_query_params($removable_url_params) {
	
	if (($key = array_search('settings-updated', $removable_url_params)) !== false) {
		
		unset($removable_url_params[$key]);
		
	}
	
	return $removable_url_params;
	
}
add_filter('removable_query_args', 'shapeSpace_allow_admin_query_params');

Remove specific parameters

And lastly, here is an example showing how to add your own parameters to the array, so that they also will be removed (along with whatever parameters are removed by default):

function shapeSpace_remove_admin_query_params($removable_url_params) {
	
	$remove_params = array('parameter-name');
	
	return array_merge($remove_params, $removable_url_params);
	
}
add_filter('removable_query_args', 'shapeSpace_remove_admin_query_params');

Using the above techniques, it is possible to add, remove, or restore any and all query-string parameters displayed in the Admin Area for any URL(s).

Closing thoughts

Although the filter hook completely resolves the issue, removing parts of the URL is a backwards way of solving compatibility and other JavaScript-related problems. But really it’s not a big deal, just wanted to document the issue for anyone wondering why URL parameters are getting removed on pages in the WordPress Admin Area.

Also heads up: Probably best advice is to use the above filter and techniques only for development and testing purposes. If you absolutely must include in a plugin, make sure to use conditional logic so that it’s run only your own plugin page.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress