Created
July 20, 2024 11:45
-
-
Save Qubadi/74e13293a7ebd23058ee005f9c66497d to your computer and use it in GitHub Desktop.
Enhance WordPress 404 Page Using Elementor Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Description: | |
1. Copy the following PHP code and create a PHP snippet using your snippet plugin. Paste the code into the plugin and save it. | |
2. Create a template page using Elementor Templates | |
3. Replace the actual Elementor template ID in this line: $elementor_404_template_id = 8209;. Change the ID number 8209 to your | |
template page ID, and save/update the code. | |
This code snippet customizes the default 404 error page in WordPress by using a specified Elementor template. | |
It hooks into the template_include filter and checks if the current page is a 404. If it is, it attempts to load and | |
display a custom 404 template created with Elementor (using the provided template ID). If successful, it outputs the | |
custom template content, otherwise, it falls back to the default template. This allows for a more visually appealing | |
and branded error page on your WordPress site. | |
____________________________________________- | |
add_filter('template_include', 'custom_404_template_with_elementor', 99); | |
function custom_404_template_with_elementor($template) { | |
if (is_404()) { // Check if it's a 404 page | |
$elementor_404_template_id = 8209; // Replace with your actual Elementor template ID | |
// Check if Elementor Plugin exists and the template ID is for an existing Elementor template | |
if (class_exists('\Elementor\Plugin') && \Elementor\Plugin::$instance->documents->get($elementor_404_template_id)) { | |
$elementor_404_template_content = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display($elementor_404_template_id); | |
if (!empty($elementor_404_template_content)) { | |
// Output header | |
get_header(); | |
// Echo the Elementor template content | |
echo $elementor_404_template_content; | |
// Output footer | |
get_footer(); | |
exit; // Prevent the default 404 template from loading | |
} | |
} | |
} | |
// Return the default template if not a 404 page or if the Elementor content is empty | |
return $template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment