Reputation: 2307
I have a 404 page in my theme but I am not using that page. I have created a new 404 page in WordPress using wpbakery page builder. I need to know how can I redirect users on the new 404 page without a plugin?
Upvotes: 0
Views: 8266
Reputation: 11282
Crate 404page in the admin.
create a custom page template for that page.
add your custom 404 content
open 404.php file in your theme.
add this below code at the top of that file.
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".home_url('/404page/'));
exit();
try to find something that not found and you will be redirected to your custom 404 page
also, you can try this action hooks for redirect to custom 404 page. put this code in your function.php file. this is the replace option of above point number 5)
add_action( 'template_redirect', 'redreict_to_custom_404_page' );
function redreict_to_custom_404_page(){
// check if is a 404 error
if( is_404() ){
wp_redirect( home_url( '/404page/' ) );
exit();
}
}
Or if you want to create 404 page with WP bakery
Create a private 404page and build with WP bakery in the admin.
open 404.php file and get content of 404page by code below
$page_id = 123; // 404page id
$page = get_post( $page_id );
$content = $page->post_content;
echo $content;
Upvotes: 0
Reputation: 379
You can use plugin 404page.
Or some code adapted from this plugin:
add_filter(
'404_template',
static function () {
global $wp_query;
$wp_query = new WP_Query();
$wp_query->query('page_id='.$pageID);
$wp_query->the_post();
$template = get_page_template();
rewind_posts();
add_filter(
'body_class',
static function ($classes) {
if (!in_array('error404', $classes, true)) {
$classes[] = 'error404';
}
return $classes;
}
);
return $template;
},
999
);
Upvotes: 3