Home » Functions » wp_get_referer

Function Name: wp_get_referer

If you are using a form on your website and you need to redirect the user back to the original page or URL they came from, then the wp_get_referer() function can come in handy. This function retrieves the referring page from which the user arrived at the current page.

The wp_get_referer() function is used in WordPress to get the URL of the referring page. It checks the $_SERVER[‘HTTP_REFERER’] variable to see if it is set and contains a value. If it is set, the function returns the URL contained in the variable. If it is not set, the function returns a default value.

This function is often used in WordPress plugins or themes when you need to redirect users back to the page they came from after they have submitted a form or completed some other action.

Usage Example:

In the following example, we’ll use the wp_get_referer() function to redirect users back to the referring page after submitting a form.

if ( isset($_POST['submit_form']) ) {
    // Process form data here
    wp_redirect( wp_get_referer() );

In this example, when the user submits the form, the wp_redirect() function is used to redirect them back to the page they came from using the wp_get_referer() function.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.