What is causing my 404 page rendering to malfunction when adding wp_footer or header?

On my local machine, I have a simple 404 page set up.

I made changes to my .htaccess file:

ErrorDocument 404 /wordpress/wp-content/themes/test_theme/404.php

The 404 page loads properly, but as soon as I include wp_head();, the layout breaks. The background color still appears fine, but the rest of the content doesn't display.

<!DOCTYPE html>
<html lang='en-gb'>
<head>
<title>404 Error</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<link href='http://fonts.googleapis.com/css?family=Monoton' rel='stylesheet' type='text/css'>
<style type="text/css">
body {
  background-color: #111;
}

.board {
  position: absolute;
  top: 50%;
  left: 50%;
  height: 150px;
  width: 500px;
  margin: -75px 0 0 -250px;
  padding: 20px;
  font: 75px/75px Monoton, cursive;
  text-align: center;
  text-transform: uppercase;
  text-shadow: 0 0 80px red, 0 0 30px firebrick, 0 0 6px darkred;
  color: red;
}

#error {
  color: white;
  text-shadow: 0 0 80px white, 0 0 30px green, 0 0 6px blue;
}

 </style>
 <?php wp_head(); ?>
</head>
<body>
<div class="board"><p id="error">error</p><p id="code">404</p></div>
<h2><?php _e( 'This is somewhat embarrassing, isn’t it?', 'twentythirteen' ); ?></h2>
<p><?php _e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentythirteen' ); ?></p>
<?php get_search_form(); ?>
</body>
</html>

I'd like to incorporate some JavaScript using:

if(is_404()){               wp_register_script('404',get_template_directory_uri().'/js/jquery.novacancy.min.js', false, NULL, true);
    wp_enqueue_script('404');
}

However, when I add wp_footer();, it causes issues and seems like WordPress is interfering with my 404 page layout.

Answer №1

The issue arises from directly loading the theme file, which fails to load all the necessary WordPress files and functions. Consequently, both wp_footer(); and wp_header(); functions are not available.

It is important to note that WordPress automatically handles 404 errors, so removing the line added to the .htaccess file will restore proper functionality.

For more information on creating an Error 404 Page in WordPress, please refer to this article: Creating an Error 404 Page in WordPress

Answer №2

It seems that cale_b has already provided an answer to your question, but I can offer some additional information. The wp_head and get_footer functions are specific to WordPress, so you cannot use them without first calling get_header or including them in the wp_blog_header.php file.

If you only require these functions without the full header (perhaps for a customized header or no header at all), I suggest including wp_blog_header.php at the beginning of your PHP file, before any other WordPress functions are called.

You can locate wp_blog_header.php in the root directory of your WordPress installation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Reveal unseen information on the webpage upon clicking a link

I have successfully implemented a fixed header and footer on my webpage. The goal is to make it so that when a user clicks on a link in either the header or footer, the content of that page should appear dynamically. While I have explored various styles, ...

Button design featuring inverted half circles on each side created using HTML and CSS

Thinking of designing a unique button with inverted half circles on either sides. I attempted to make a half-circle and include it at the button's end, but the result didn't turn out quite like I hoped. Envisioning something similar to this: b ...

Tips for creating CSS3 animations in Angular triggered by mouse clicks

I'm working with a span that utilizes a Bootstrap icon. My goal is to fade out and fade in the same element (span) on click, while toggling the class (icon). There's a boolean variable called showLegend which determines whether or not to animate ...

Can CSS be used to dynamically change the color of an element based on a specific value?

Is it possible to dynamically change the background color of table cells using only CSS based on their values? For example, I am looking to achieve a similar effect as in Excel with conditionally formatted values: https://i.stack.imgur.com/6BsLB.png If ...

Is the table row changing in size when a filter script class is applied?

When trying to filter table rows by category and adding the class filterTr to the element <tr>, I noticed that the width of the <tr> element changes, causing it to no longer align with the <th> element above. I am unsure why this resizin ...

What is the best way to arrange an unordered list in a horizontal layout?

I have four lists that I want to display side by side. The first row should show a Doctor and Patient side by side, the second row should show a Pharma Company and Employee side by side. However, in my code, this layout is not working as intended. .tree ...

What is the best way to download an updated social media post for a Django user?

Objective A user has the ability to edit their own posts by clicking on the edit button, making edits, and then saving the changes. Issue Editing a social media post does not result in the changes being saved. Overview Create a new post similar to how ...

Display upon hovering, conceal with a button located within a popup container

There seems to be an issue with the code below. Even though it works perfectly in jsfiddle, it breaks in my Chrome and other browsers right after displaying the ".popup" div. Can anyone point out what I might be doing wrong? I found similar code on this si ...

CSS for creating a vertical dropdown in a Bootstrap horizontal navigation bar

I recently delved into the world of twitter bootstrap and decided to construct a website from scratch using it. I've hit a roadblock while trying to develop a horizontal navigation bar with vertical dropdowns where each <li> creates a new row (c ...

Exploring ways to incorporate zero padding exclusively for mobile devices through Bootstrap

On mobile sm devices, I am trying to achieve padding: 0, but so far nothing has worked. I referenced this post on Stack Overflow and also attempted the code below: The default padding of 15px on the right and left for col-*-12 is causing issues. I only wa ...

I am experiencing an issue wherein after using jquery's hover() function, the CSS :hover state is not

Following the jquery hover function, the css hover feature ceases to work. In my html, there are multiple svg elements. A jquery script is applied where hovering over a button at the bottom triggers all svg elements to change color to yellow (#b8aa85). S ...

When using ^(.*)$ in the .htaccess file to redirect to index.php/$1, it may result in the error message "

While running Code Igniter, I encountered an issue with my .htaccess file: <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?$1 [L] Redi ...

The Navigation Bar is positioned too distant towards the Right side

Update: The issue has been resolved. Thank you for the assistance. I'm facing a problem with my navigation bar on my website. I can't seem to get it to span the entire width of the page while staying centered. The navigation bar is sticky and fu ...

jQuery: Manipulating and accessing DOM elements through attributes: querying and creating them

I have some inquiries regarding jQuery, specifically in relation to attributes: Is there a way to list or duplicate all attributes of a DOM element using a jQuery or DOM API call? Although the jQuery.attr() API allows this with known attribute names, is ...

Problem with rendering mailer templates in Outlook

I am experiencing an issue with my mailer template that is constructed using divs, with a width of 650px. While it displays perfectly in browsers, Outlook is not recognizing the specified width and instead displaying it at 100% width. Additionally, setting ...

How to troubleshoot WordPress Ajax function not getting form data

I am currently working on a form in my plugin where I need to send data to my AJAX action function using the standard WP Ajax techniques. Here is the basic structure of my form: <form role="form" id="signup_widget_form" method="post" action="#"> ...

Tips for preventing Chrome from masking the background image and color on an autofill input

Google Chrome Browser has caused the background-color and background-image effects to be removed from the Username and Password input fields. Before autocomplete https://i.stack.imgur.com/Ww7Hg.png After autocomplete https://i.stack.imgur.com/hbG2C.png ...

Unable to apply .png image using the content property in a Symfony project

I have a png image located in src/AppBundle/Resources/public/img To set the png image using the css content property, I used this code: content: url(../../public/assets/img/optional-checked.png); However, I encountered an error: GET http://127.0.0.1:80 ...

Display the selected item when it is chosen | Utilizing Jquery

I am facing a challenge where I want a particular div to be visible only when a specific item is chosen. Here is the work I have done so far: View Progress In the HTML comments, it is mentioned that div5 should appear when option 2 is selected, but by de ...

Tap on a division to alternate the class of an image

I'm facing a problem with changing an icon when I click on a div. I'm trying to create an accordion element with an arrow icon (using Iconoir) that switches between classes "iconoir-nav-arrow-right" and "iconoir-nav-arrow-down" each time it' ...