Using WordPress ‘removable_query_args’ PHP filter

The ‘removable_query_args’ WordPress PHP filter allows you to modify the list of query variable names that can be removed from a URL in WordPress.

Usage

To use this filter, you need to add a custom function to your theme’s functions.php file or a plugin. This function will hook into the ‘removable_query_args’ filter and modify the list of removable query arguments. Here’s a basic code example:

function my_custom_removable_query_args( $removable_query_args ) {
// Add or modify the removable query arguments here
return $removable_query_args;
}
add_filter( 'removable_query_args', 'my_custom_removable_query_args' );

Parameters

  • $removable_query_args (string[]): An array of query variable names to remove from a URL.

Examples

Example 1: Add a custom query argument to the list

function add_custom_removable_query_arg( $removable_query_args ) {
// Add 'my_custom_arg' to the list of removable query arguments
$removable_query_args[] = 'my_custom_arg';
return $removable_query_args;
}
add_filter( 'removable_query_args', 'add_custom_removable_query_arg' );

Explanation: This example adds ‘my_custom_arg’ to the list of removable query arguments, allowing it to be removed from URLs.

Example 2: Remove a default removable query argument

function remove_default_removable_query_arg( $removable_query_args ) {
// Remove 's' (search query) from the list of removable query arguments
$key = array_search( 's', $removable_query_args );
if ( false !== $key ) {
unset( $removable_query_args[ $key ] );
}
return $removable_query_args;
}
add_filter( 'removable_query_args', 'remove_default_removable_query_arg' );

Explanation: This example removes the ‘s’ query argument (search query) from the list of removable query arguments, preventing it from being removed from URLs.

Example 3: Clear all removable query arguments

function clear_all_removable_query_args( $removable_query_args ) {
// Clear the entire list of removable query arguments
return array();
}
add_filter( 'removable_query_args', 'clear_all_removable_query_args' );

Explanation: This example clears the entire list of removable query arguments, preventing any query arguments from being removed from URLs.

Example 4: Add multiple custom query arguments

function add_multiple_custom_removable_query_args( $removable_query_args ) {
// Add multiple custom query arguments to the list
$custom_args = array( 'custom_arg1', 'custom_arg2', 'custom_arg3' );
$removable_query_args = array_merge( $removable_query_args, $custom_args );
return $removable_query_args;
}
add_filter( 'removable_query_args', 'add_multiple_custom_removable_query_args' );

Explanation: This example adds multiple custom query arguments to the list of removable query arguments, allowing them to be removed from URLs.

Example 5: Conditionally add a query argument

function conditionally_add_removable_query_arg( $removable_query_args ) {
// Add 'conditional_arg' only for logged-in users
if ( is_user_logged_in() ) {
$removable_query_args[] = 'conditional_arg';
}
return $removable_query_args;
}
add_filter( 'removable_query_args', 'conditionally_add_removable_query_arg' );

Explanation: This example adds the ‘conditional_arg’ query argument to the list of removable query arguments, but only for logged-in users. If a user is not logged in, the ‘conditional_arg’ will not be added to the list and won’t be removed from URLs.