• Blog
  • Blogzone
  • Camolin
  • Collon
  • Contact Me
  • Home
  • Ivory
  • Ryzen
  • Trex
No Result
View All Result
Vicky.Dev
  • Tutorials
  • Themes
  • Plugins
  • SEO
  • Tech
  • Blockchain
  • Misc
  • Contact Me
  • Tutorials
  • Themes
  • Plugins
  • SEO
  • Tech
  • Blockchain
  • Misc
  • Contact Me
No Result
View All Result
Vicky.Dev
No Result
View All Result

wp_redirect();  A Beginner’s Guide.

Vicky Bhandari by Vicky Bhandari
November 22, 2022
in Tutorials
0
wp_redirect image

Photo by Carlos Muza

Do you want to redirect your WordPress website? Or you are just curious to learn for knowledge? You will learn everything in this article about wp_redirect. Because this is a situation we all have to face, at some point in our life. So it’s better to keep you prepared for this kind of situation. Don’t worry this article will help you learn everything about wp_redirect.

Table of Contents

  • What is wp_redirect in WordPress?
  • When do you need to use redirect in WordPress ?
  • How to use wp_redirect() function?
  • wp_redirect with the help of a plugin.
  • How to use wp_redirect manually?
    • Conclusion

What is wp_redirect in WordPress?

wp_redirect is a method to send users from one link/address to another. This function makes sure to drop your visitors in the right place. However to make you under this function better. We will keep everything simple and straight.

When do you need to use redirect in WordPress ?

Are you thinking about when it is needed to use the wp_redirect function? Let’s understand this by an example. Suppose you as a car dealer. You sell two types of cars. The first type is diesel/petrol engine cars. And the second one is electric cars.

For this, you have two different showrooms in different places. When buyers asks for diesel/petrol cars you send them a diesel/petrol showroom. And vice versa.The same thing happens with websites in WordPress. You send users to the right destination as per the criteria.

How to use wp_redirect() function?

There are two ways to redirect in WordPress. The first method is a simple one. You can use this with the help of a plugin. The second method is to do this manually. To do it manually you need to have some technical knowledge. Otherwise, you can break your website.

wp_redirect with the help of a plugin.

There are plenty of plugins that will do the work for you. They may have a little different layout but more or less they all work the same. There are different types of redirects in WordPress. However plugins for all redirects are available. You can select as per your requirements.

These are some plugins:

  • Redirection
  • Safe Redirect Manager

How to use wp_redirect manually?

This method is specifically for developers. Because you need to have coding knowledge to do this. Before that let’s discuss the parameters used in the wp redirect function.

  • $location: It is a string(required). It’s the desired location/path where you want to redirect your URL.
  • $status: It is the HTTP response status code to use. It shows the status of the existing URL. It isn’t mandatory to use. By default, the value is ‘302’, meaning the URL is moved temporarily.
  • $x_redirect_by: This shows the names of the application. For instance, we are talking about WordPress. The default value for this is WordPress.

Here is an example:

wp_redirect(  $location, $status = 302, $x_redirect_by = 'WordPress' ): bool
function wp_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {
    global $is_IIS;

    /**
     * Filters the redirect location.
     *
     * @since 2.1.0
     *
     * @param string $location The path or URL to redirect to.
     * @param int    $status   The HTTP response status code to use.
     */
    $location = apply_filters( 'wp_redirect', $location, $status );

    /**
     * Filters the redirect HTTP response status code to use.
     *
     * @since 2.3.0
     *
     * @param int    $status   The HTTP response status code to use.
     * @param string $location The path or URL to redirect to.
     */
    $status = apply_filters( 'wp_redirect_status', $status, $location );

    if ( ! $location ) {
        return false;
    }

    if ( $status < 300 || 399 < $status ) {
        wp_die( __( 'HTTP redirect status code must be a redirection code, 3xx.' ) );
    }

    $location = wp_sanitize_redirect( $location );

    if ( ! $is_IIS && 'cgi-fcgi' !== PHP_SAPI ) {
        status_header( $status ); // This causes problems on IIS and some FastCGI setups.
    }

    /**
     * Filters the X-Redirect-By header.
     *
     * Allows applications to identify themselves when they're doing a redirect.
     *
     * @since 5.1.0
     *
     * @param string $x_redirect_by The application doing the redirect.
     * @param int    $status        Status code to use.
     * @param string $location      The path to redirect to.
     */
    $x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location );
    if ( is_string( $x_redirect_by ) ) {
        header( "X-Redirect-By: $x_redirect_by" );
    }

    header( "Location: $location", true, $status );

    return true;
}

Source : wordpress.org

Examine the example properly and you will understand everything. First, there is a variable named $location which is the place where we want to divert the link. Then there is a variable $status. This is showing the status of the redirect ‘302’ which means it is moved temporarily. At last, the $x_redirect_by is showing the application name we are using. In our case it is WordPress.

bool: It will return true or false depending on the redirect status. It returns false if the redirect is canceled. Otherwise, the return will be true.

Conclusion

Now you know everything about wp_redirect function. From understanding the meaning to doing it manually. This covers problems for both a regular WordPress user and a developer at the same time.

I hope this beginner’s guide to wp_redirect article helped you. I always look forward to providing you with the best I can. So if you like this article, you can check our other articles such as get_post_meta and get_terms and get_the_terms also. Thank you for reading the article.

 

 

Previous Post

get_terms and get_the_terms In WordPress ? A Beginners Guide.

Next Post

Airi WordPress theme. A Complete Review.

Next Post
airi WordPress theme

Airi WordPress theme. A Complete Review.

© 2022 Vicky Bhandari. All Rights Reserved.

No Result
View All Result
  • Tech
  • Themes
  • Tutorials
  • Misc
  • SEO
  • Plugins
  • Blockchain

© 2022 Vicky Bhandari. All Rights Reserved.