I am trying to redirect the user to the theme 404.php template from a custom plugin if certain products are not found. Any ideas on this?
protected function get_id() {
$product_id = $_GET['product_id'];
$product = get_post( $product_id );
if ( empty( $product ) ) {
// Time to 404.
$error_page = get_404_template();
header("HTTP/1.0 404 Not Found");
include($error_page);
// neither works
get_template_part('404');
die();
}
else {
$this->product_id = $product_id;
}
}
Here's your updated code, it should properly show 404 page:
protected function get_id() {
$product_id = $_GET['product_id'];
$product = get_post( $product_id );
if ( empty( $product ) ) {
// Time to 404.
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 ); exit();
}
else {
$this->product_id = $product_id;
}
}
Let me know if that does help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.