admin管理员组

文章数量:1336304

I am new at WordPress and just made my first plugin to add some custom fields to WooCommerce. Depending on these custom fields, I want to redirect my product page.

The active theme has a template override for single-product.php. I added some conditional logic there and used wp_redirect() to make it work. And that works just fine.

But when the theme will be updated, this conditional logic will be lost. Is it possible to override a template file using a plugin, but only if conditions are met?

Something like:

$redirectPage = true;
if ($redirectPage) { // don't use template, redirect to some page (this part works)
   $targetUrl = ...;
   wp_redirect($targetUrl, 301);
   exit();
} else {
   // now we want to keep normal behavior
   // use excisting single-product.php file from theme
   // how to do it?
}

Any insights if this is possible? Can someone point me in the right way? I understand I can make use of a subtheme but I would prefer to handle everything inside a plugin. Thanks.

I am new at WordPress and just made my first plugin to add some custom fields to WooCommerce. Depending on these custom fields, I want to redirect my product page.

The active theme has a template override for single-product.php. I added some conditional logic there and used wp_redirect() to make it work. And that works just fine.

But when the theme will be updated, this conditional logic will be lost. Is it possible to override a template file using a plugin, but only if conditions are met?

Something like:

$redirectPage = true;
if ($redirectPage) { // don't use template, redirect to some page (this part works)
   $targetUrl = ...;
   wp_redirect($targetUrl, 301);
   exit();
} else {
   // now we want to keep normal behavior
   // use excisting single-product.php file from theme
   // how to do it?
}

Any insights if this is possible? Can someone point me in the right way? I understand I can make use of a subtheme but I would prefer to handle everything inside a plugin. Thanks.

Share Improve this question edited May 23, 2020 at 21:04 TVBZ asked May 22, 2020 at 18:08 TVBZTVBZ 1298 bronze badges 3
  • 1 template_include is a filter hook to replace template files. WooCommerce serves their template files (single product, shop, category archive etc) from their plugin folder (unless theme had overridden those templates). Dig into their source code and you will figure out how to do it. – Shazzad Commented May 22, 2020 at 18:34
  • Hi.. As mentioned in original question. Template folder has it's own single-product.php file. So yes, the template folder has an override. I will look more into template_include. At first glance I don't see it offer a solution. – TVBZ Commented May 22, 2020 at 20:47
  • Thanks for the tip @Shazzad.. It led me to the solution. – TVBZ Commented May 22, 2020 at 22:00
Add a comment  | 

1 Answer 1

Reset to default 1

Okay.. After some more reading I came to a working solution.

First we make the plugin listen IF the type of page is a product page. Then IF that is true, conditional logic can be implemented. This way it's update proof and it also works on any other template. Great!

Here 's the code I added in plugin (somewhat minified to the basics):

add_action( 'wp', 'redirect_single_product_page' );

function redirect_single_product_page() {
    if (is_product()) { // if the page is a single product page
        $redirectPage = true; // some dummy value for conditional logic
        if ($redirectPage) {
            $target_url = ...; // construct a target url
            wp_redirect($target_url, 301); // permanent redirect
            exit();
        }
    }
}

If the page does not meet the conditional logic, normally expected behavior applies.

本文标签: phpCan a plugin redirect product page based on IF condition