WordPress Hooks

Redirection can be extended and customised using WordPress actions and filters.

To use these you will need to add the hooks to your own plugin or to your theme’s functions.php file.

Support is provided for the plugin, not for custom PHP code. It is your responsibility to ensure that the PHP code is correct and does what you need. There are many, many, websites detailing how to code for WordPress.

Filters

A WordPress filter returns data, and is used in Redirection to modify various bits of data. To use a filter:

add_filter( 'filter_name', function( $param1, $param2 ) {
  return 'your custom value';
}, 10, 2 );

Full details can be found on the WordPress filter page.

Request Information

The following WordPress filters are available for customisation of a server requests.

Name: redirection_request_url
Modify the requested URL
$url
Requested URL (string)
return
Modified URL (string)
Name: redirection_request_agent
Modify the requesting client’s user agent
$agent
Requester’s user agent (string)
return
Modified user agent (string)
Name: redirection_request_referrer
Modify the requesting client’s referrer
$referrer
Requester’s referrer (string)
return
Modified referrer (string)
Name: redirection_request_ip
Modify the requesting client’s IP address
$ip
Requester’s IP address (string)
return
Modified IP (string)
Name: redirection_request_cookie
Modify the requesting client’s cookie value
$value
Requester’s cookie value (string)
$name
Cookie name
return
Modified cookie (string)
Name: redirection_request_header
Modify a header value from the requesting client
$value
Header value (string)
$name
Header name (string)
return
Modified header value (string)
Name: redirection_request_headers
Return all the client HTTP headers
$headers
Array of header => value
return
Modified header value (array)
Name: redirection_request_headers_ignore
Return a list of header values that are ignored
$headers
Array of header names
return
Modified header names (array)
Name: redirection_request_method
Return the current HTTP request method
$method
Request method (string)
return
Modified request method
Name: redirection_request_server
Return the current server name
$server
Server name (string)
return
Modified server name
Name: redirection_request_accept_language
Return the client’s accept-language
$lang
Language (string)
return
Modified language

Logging

The following WordPress filters are available for customisation of logged data:

Name: redirection_404_data
Data to be inserted into the 404 table
$data
Log data (array of database values)
return
Modified data. Return false to skip logging
Name: redirection_log_data
Data to be inserted into the log table
$data
Log data (array of database values)
return
Modified data. Return false to skip logging
Name: redirection_log_404
Determine if the current 404 page should be logged
$log
Should the page be logged (boolean)
return
true if it should be logged, false otherwise
Name: redirection_redirect_counter
Determine if a redirect should increase the hit counter
$inc
Should the hit counter be increased (boolean)
return
true if it should, false otherwise

Redirect source and target

Name: redirection_url_source
The original URL used before matching a request
$url
URL (string)
return
URL (string), or false to stop a redirection
Name: redirection_url_target
The target URL after a request has been matched (and after any regular expression captures have been replaced)
$url
URL (string)
return
URL (string), or false to stop a redirection

Actions

Name: redirection_log
Fired when the page is logged
$log
Log data (array)
Name: redirection_do_nothing
Fired when a ‘do nothing’ action happens
No parameters
redirection_remove_existing
Fired when a post changes permalink and we need to clear existing redirects that might affect it
$url
Modified URL
$post_id
ID of post
redirection_monitor_created
Fired when a redirect is created for a monitor post type. Supplied with the new redirect, old post, and post ID
$redirect
Red_Item object for newly created redirect
$url
URL of post before it was modified
$post_id
ID of post being modified
redirection_redirect_updated
Action fired when a redirect is updated or created
$id
ID of redirect item
$redirect
Red_Item redirect object
redirection_redirect_deleted
Action fired when a redirect is deleted
$redirect
Red_Item redirect object

Management Filters

redirection_permalink_changed
Return true if a post’s permalink has changed, or false otherwise
$changed
true if changed, false otherwise
$before
Previous permalink
$after
New permalink
return
true or false
redirection_monitor_types
Modify what post types are monitored by Redirection
$types
Array of post types
Return
Array of post types
redirection_create_redirect
Modify redirect data before a redirect is created
$redirect
Redirect data
Return
Modified data
redirection_update_redirect
Modify redirect data before a redirect is updated
$data
Redirect data
Return
Modified data
redirection_validate_redirect
Validate data for a redirect
$data
Redirect data
Return
Modified data
red_default_options
Modify the default options
$options
Default options
Return
Modified defaults
redirection_save_options
Modify options before they are saved
$options
Options being saved
Return
Modified options

Additionally, if the target URL is a number without any slashes then Redirection will treat it as a post ID and redirect to the full URL for that post.

HTTP Headers

  • redirection_header_site – Array of HTTP headers that Redirection is adding from the ‘site’ location
  • redirection_header_redirect – Array of HTTP headers that Redirection is adding from the ‘redirect’ location (will include those from ‘site’)