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 Answer
Reset to default 1Okay.. 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742406120a4437882.html
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:34template_include
. At first glance I don't see it offer a solution. – TVBZ Commented May 22, 2020 at 20:47