Welcome to WebmasterWorld Guest from 54.166.172.180

Forum Moderators: rogerd & travelin cat

How to remove header from 404 page?

remove header 404 page

     
2:50 pm on Oct 25, 2017 (gmt 0)

New User

5+ Year Member

joined:May 16, 2012
posts:23
votes: 0


Hello, I am trying to find a way to remove the header from my "404 NOT FOUND" page. The reason why is that my Adsense code is in the header file and appears on every page of my site. But Google does not want any ads on a 404 page. Thank you very much for your help.

Here is the 404.php code:

<?php 
get_header(); ?>
<!--Start Content Grid-->
<div class="grid_24 content">
<div class="content-wrap">
<div class="fullwidth">
<div class="content-info">
</div>
<h1> <?php echo ('Not Found'); ?> </h1>
<p> <?php echo ( 'Apologies, but the page you requested could not be found. Perhaps searching will help.'); ?> </p>
<?php get_search_form(); ?>
</div>
</div>
</div>
<div class="clear"></div>
</div>
<?php get_footer(); ?>
8:17 pm on Oct 25, 2017 (gmt 0)

New User

joined:Aug 21, 2016
posts:17
votes: 0


You remove the get_header() call?
9:50 pm on Oct 25, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14721
votes: 616


remove the get_header() call?

Which in turn means you remove the surrounding <php markup, since that's all it exists for.

Incidentally ... Is there a reason for this kind of thing?
<h1> <?php echo ('Not Found'); ?> </h1>
Or this?
<div class="content-info">
</div>
Is this a hard-coded page? There seems to be a lot of extranea.
11:50 pm on Oct 25, 2017 (gmt 0)

New User

5+ Year Member

joined:May 16, 2012
posts:23
votes: 0


Thank you for your replies. If I remove the call for the header, then the CSS doesn't load properly. I believe this is because call to header does many things, including loading CSS. How can I get around this issue?
3:40 am on Oct 26, 2017 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8589
votes: 258


You'll want to create a special header named something like header-404.php or header-no-adsense.php that includes only what you want.

Then you would call get_header with and argument as in
<?php get_header('header-404.php'); ?>

See: [codex.wordpress.org...]
or
[developer.wordpress.org...]
5:55 am on Oct 26, 2017 (gmt 0)

New User

5+ Year Member

joined:May 16, 2012
posts:23
votes: 0


Thank you very much. Could you possibly provide sample code for a special header? I am sorry to trouble you.
1:46 pm on Oct 26, 2017 (gmt 0)

New User

5+ Year Member

joined:May 16, 2012
posts:23
votes: 0


After further review,I think I understand! I made an exact copy of my header.php file, except that I removed the adsense code. Then I renamed it header-404.php. Now I add this call to my 404.php file, correct?

<?php get_header('header-404.php'); ?>
2:17 pm on Oct 26, 2017 (gmt 0)

New User

joined:Aug 21, 2016
posts:17
votes: 0


Yes and no. It really depends on your code.

What is get_header() and where is it defined? I'd generally avoid code duplication. What you could do is to add a boolean argument to that function which determines whether the ad code is added or not. But again a lot will depend on your setup.

Can you post get_header?

Something of that sort might work and would not require any changes to existing code.
function get_header($includeAds = true)
{
echo 'whatever';
if ($includeAds) echo 'ad code';
echo 'more whatever'
}
7:28 pm on Oct 26, 2017 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8589
votes: 258


Yes, that is correct, assuming that your custom 404 page is already getting called and working as expected. Very easy to test :-)

What is get_header() and where is it defined?


This is a standard Wordpress function as outlined in the docs I linked to. The standard way to change out one header for the other is to have multiple header.php files (again, see the docs linked above)

Do NOT alter the get_header() function. Do NOT alter Wordpress core. Doing so will 1) cause instability and 2) get overwritten every time a WP update gets pushed out.

Of course you can have a header.php file with logic in there that would strip it down based on conditions, but that's more complex and less standard
8:10 pm on Oct 26, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14721
votes: 616


This is a standard Wordpress function as outlined in the docs I linked to.
Without changing any other code, can the function be used either with or without an argument? Got a nasty idea “argument” is not the correct term, but then as we all know I only speak about three words of php.
8:18 pm on Oct 26, 2017 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8589
votes: 258


1. "argument" is the correct term and so is "function." So that's two words of php right there and I guess "php" would be your third ;-)

2. Yes, the argument can be passed with or without an argument. The default is pass without an argument and let it default to header.php. The argument overrides that.

So the standard method in WP for changing out your header is to duplicate your header.php file (or create a new one from scratch) and then pass the new file name as an argument.

PS - want to learn a fourth word? Try this thread,
[webmasterworld.com...]
especially the post from 5:35 pm on Jan 9, 2005 on "scope"
1:31 am on Oct 27, 2017 (gmt 0)

New User

5+ Year Member

joined:May 16, 2012
posts:23
votes: 0


I found this in the Wordpress Codex:

Different headers for different pages:

<?php
if ( is_home() ) :
get_header( 'home' );
elseif ( is_404() ) :
get_header( '404' );
else :
get_header();
endif;
?>


The file names for the home and 404 headers should be header-home.php and header-404.php respectively.

Therefore, I think I made a mistake. I should replace header-404 with just 404 in the get_header call.
1:34 am on Oct 27, 2017 (gmt 0)

New User

5+ Year Member

joined:May 16, 2012
posts:23
votes: 0


I can now confirm that this works! The adsense ads are no longer showing on my 404 "Not Found" page.

<?php get_header('404'); ?>
2:21 am on Oct 27, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14721
votes: 616


Hurrah! You don't have to figure out how to invent a wheel, because someone has already done it for you.
especially the post from 5:35 pm on Jan 9, 2005 on "scope"

Heh, I remember that one. In normal languages, variables are global unless you expressly declare them to be local. php, for reasons best known to itself, has chosen to do it the other way around ;)
1:38 pm on Oct 27, 2017 (gmt 0)

New User

5+ Year Member

joined:May 16, 2012
posts:23
votes: 0


A big "thank you" to everyone on this thread for your comments and assistance!
1:53 am on Oct 28, 2017 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8589
votes: 258


Glad you got it sorted.

I should replace header-404 with just 404 in the get_header call


All that matters is that the filename is the same as the name you supply to the get_header() function. You could call it qwer9083asdfr3.php if you wanted. The advantage of naming it header-something.php is that when you list the files in that directory alphabetically, all of your headers appear together and you know exactly which files might be involved. Can save you some trouble down the road.

If you wanted to approach it like neroux suggested, you could also maintain a single header.php file and have something like

if (!is_404()) {
[adsense code goes here]
}

>>In normal languages, variables are global unless you expressly declare them to be local

What language are you thinking of? I've never seen a language where a variable inside a function defaults to global. Certainly that would not be true in Python, C, C++, FORTRAN, Pascal. I'm not sure if it's true for English and French or not... I'll have to think about that.
5:07 am on Oct 28, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14721
votes: 616


I've never seen a language where a variable inside a function defaults to global.
Javascript. Forget a single "var" declaration, and interesting things happen. And then there are the assorted dialects of BASIC that never agree on exactly how variables work--but if you don't declare it somewhere in the general vicinity of a function, it ends up being global.
4:51 pm on Oct 28, 2017 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8589
votes: 258


Wait, Javascript is our standard for a "normal" language! :-)

Seriously, though, the way variable scope works in PHP is more similar to most "normal" languages than in JS where you can just reference a global variable by accident if you fail to instantiate it with a var statement inside the function. In most languages, to access a global variable inside a function, you have to take positive action, not forget to do something!
5:57 pm on Oct 28, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14721
votes: 616


Y'know, I realized as I was typing that that would come out wrong :)

But I comfort myself with the knowledge that I have never called HTML a "language".

:: vague mental association with punchline of Sanskrit joke ::
3:42 am on Oct 30, 2017 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8589
votes: 258


But I comfort myself with the knowledge that I have never called HTML a "language".


First response: Ha ha!

Second response: But wait, HTML stand for Hypertext Markup Language. Granted, it's not a programming language, but it is a language... a dialect of SGML ;-)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members