0

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.

  • 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 May 22 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 May 22 at 20:47
  • Thanks for the tip @Shazzad.. It led me to the solution. – TVBZ May 22 at 22:00
0

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.

| improve this answer | |
  • Well, you could have created a child theme and created single-product.php and modified it as per your need. This way if the theme gets updated you won't lose your code. And also it's the best practice to use a child theme for any modifications in the theme. – made2popular May 23 at 1:33
  • 2
    ob_start() is pointless here. You don't need it. – Jacob Peattie May 23 at 6:28
  • @JacobPeattie I'm not sure why.. But it didn't work without it inside the template files. I will try without in plugin. Still new at php also. Found this recommendation in some other stackexchange post. – TVBZ May 23 at 20:52
  • @made2popular True. But if we would decide to switch to a different theme it wouldn't work anymore. This way it works on all themes. :) – TVBZ May 23 at 20:53
  • @JacobPeattie Indeed it works fine without. I will read some more about output buffering. Thanks for tip! :) – TVBZ May 23 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.