Getting the referring URL in WordPress using wp_get_original_referer

The wp_get_original_referer function in WordPress is designed to retrieve the original referer from the current HTTP request. This function is a part of WordPress’s API and is used to determine the page from which the user navigated to the current page.

When a user visits a webpage, the HTTP request usually includes a “Referer” header. This header indicates the URL of the page from which the user came. However, in some cases, the referer might be changed during the course of the user’s navigation. For instance, if the user is redirected to a different page, the referer would be updated to the URL of the redirecting page, not the original page from which the user came.

The wp_get_original_referer function provides a way to bypass this issue by retrieving the original referer, i.e., the URL of the very first page from which the user came, regardless of any subsequent redirections. This can be useful in various scenarios, such as tracking user navigation patterns, debugging, or providing personalized user experiences based on their navigation history.

Parameters

The wp_get_original_referer function in WordPress does not accept any parameters.

Return Value

The wp_get_original_referer function returns a string containing the original referer URL if the function is successful. In case of failure, it returns false.

Examples

How to Check if a Referer Exists

The following code snippet uses the wp_get_original_referer function to check if a referer exists.

$referer = wp_get_original_referer();
if ($referer) {
 echo "The referer URL is: " . $referer;
} else {
 echo "No referer found";
}

In this example, we first call the wp_get_original_referer function and assign its return value to the $referer variable. The function returns the original referer URL if it exists, or false if it doesn’t. We then use an if statement to check if a referer exists. If it does, we print the referer URL. If it doesn’t, we print a message indicating that no referer was found.

How to Redirect Based on the Referer

The following code snippet uses the wp_get_original_referer function to redirect the user based on the referer.

$referer = wp_get_original_referer();
if ($referer && strpos($referer, 'example_domain.com') !== false) {
 wp_redirect('http://www.example.com');
 exit;
}

In this example, we first call the wp_get_original_referer function and assign its return value to the $referer variable. We then use an if statement to check if a referer exists and if it contains ‘example_domain.com’. If both conditions are met, we redirect the user to ‘http://www.example.com’ using the wp_redirect function.

How to Log the Referer

The following code snippet uses the wp_get_original_referer function to log the referer.

$referer = wp_get_original_referer();
if ($referer) {
 error_log("Referer: " . $referer);
}

In this example, we first call the wp_get_original_referer function and assign its return value to the $referer variable. We then use an if statement to check if a referer exists. If it does, we log the referer using the error_log function.

Conclusion

The wp_get_original_referer function in WordPress is a utility function that retrieves the original referer from the current request, if it exists. It is useful for tracking the source of a user’s visit to a specific page or site within the WordPress application. This function can be applied in various scenarios, such as analytics, user behavior tracking, and debugging, where understanding the user’s navigation path is required. Remember, it only provides information about the referer, and does not perform any actions or changes based on this information.

Related WordPress Functions