Redirect product – page – role based with IF condition in wordpress through functions.php or a plugin

Picture of Amitpal Singh
Amitpal Singh
April 26, 2023
Redirect product – page – role based with IF condition in wordpress through functions.php or a plugin

Quick Solution:


add_action( 'wp', 'redirect_single_product_page' );
function redirect_single_product_page() {
    if (!is_user_logged_in() && current_user_can('subscriber') && is_page('order-form')) { // if the page is a single product page
        $redirectPage = true; // some dummy value for conditional logic
        if ($redirectPage) {
            $target_url = 'https://domain.com/'; // construct a target url
            wp_redirect($target_url, 301); // permanent redirect
            exit();
        }
    }
} 

In today’s world, creating a website has become an essential part of any business. WordPress is one of the most popular content management systems (CMS) used for creating websites. It is user-friendly, and anyone can easily create and customize a website with the help of various plugins and themes available. Redirecting pages based on roles is an essential feature of any website. In this article, we will discuss how to redirect a product page based on roles with IF condition in WordPress through functions.php or a plugin.

Why Redirect a Product Page Based on Roles?

There can be many reasons for redirecting a product page based on roles. For example, you might want to show a different page for a customer who has purchased a product, or you might want to show a different page to a customer who is not logged in. You might also want to show different pages to different user roles, such as subscribers, editors, and administrators.

How to Redirect a Product Page Based on Roles with IF Condition?

WordPress allows you to redirect pages based on roles using the IF condition. The IF condition is a programming concept that allows you to execute a block of code only if a certain condition is met. Here are the steps to redirect a product page based on roles with IF condition:

Step 1: Determine the User Role

The first step is to determine the user role. You can use the WordPress function current_user_can() to check the user role. This function takes a parameter that specifies the user role you want to check. For example, if you want to check if the user is an administrator, you can use the following code:

if ( current_user_can( 'administrator' ) ) {
    // Code to execute if the user is an administrator
} 

Step 2: Redirect the Page

Once you have determined the user role, the next step is to redirect the page. You can use the WordPress function wp_redirect() to redirect the page. This function takes a parameter that specifies the URL you want to redirect to. For example, if you want to redirect the user to a page called “product-page” if the user is an administrator, you can use the following code:

if ( current_user_can( 'administrator' ) ) {
    wp_redirect( 'https://example.com/product-page' );
    exit;
} 

The exit function is used to terminate the current script execution. This function ensures that no further code is executed after the redirect.

Step 3: Add the Code to functions.php or a Plugin

Once you have the code to redirect a product page based on roles, you need to add it to the WordPress theme’s functions.php file or a plugin. Here is an example of how to add the code to the functions.php file:

function redirect_product_page() {
    if ( current_user_can( 'administrator' ) ) {
        wp_redirect( 'https://example.com/product-page' );
        exit;
    }
}
add_action( 'template_redirect', 'redirect_product_page' ); 

In this example, we have added the code to the template_redirect action. This action is triggered after the WordPress template is loaded, but before any content is output. This ensures that the redirect is executed before any content is displayed.

 

Conclusion

Redirecting a product page based on roles is an essential feature of any website. WordPress allows you to do this using the IF condition and the wp_redirect() function. You can add this code to the functions.php file or a plugin to ensure that the redirect is executed correctly. Remember to always test your code before deploying it to a live website to ensure that it works as expected.

Share this post:

How to Attribute?

Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
for Example: Website, Social Media, Blogs, ebooks , newsletter, etc.
Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
Copied!

Got a Question? Check out our FAQ Section.

Your action, our appreciation

It encourage us to give you more valuable content on website.