How to put Google adsense ad in header

How to put Google adsense ad (728*90) in header since its an JS html code. Thanks

Hello there,

To alter the default image ads with other code, you need to override the function that wraps the site branding area. The easiest way to do this is by using the My Custom Functions plugin. Once this plugin activated, navigate to Appearance > Custom Functions. And then insert the following PHP code.


function greatmag_site_branding() {
	$header_ad_image 	= get_theme_mod('header_ad_image');
	$header_ad_url		= get_theme_mod('header_ad_url');
	?>
		<div class="site-branding vhome3 row m0">
			<div class="container">
				<div class="main-logo">
					<div class="media">
						<?php if ( function_exists( 'the_custom_logo' ) && has_custom_logo() ) : ?>
						<div class="media-left">
							<?php the_custom_logo(); ?>
						</div>
						<?php endif; ?>
						<div class="media-body">
							<?php
							$description = get_bloginfo( 'description', 'display' );
							if ( $description || is_customize_preview() ) : ?>
								<p class="site-description site-slogan"><?php echo $description; /* WPCS: xss ok. */ ?></p>
							<?php
							endif;
							if ( is_front_page() && is_home() ) : ?>
								<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
							<?php else : ?>
								<p class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
							<?php
							endif;
							?>
						</div>
					</div>
				</div>
				<?php if ( $header_ad_image ) : ?>
					<div class="header-ad">
						<a target="_blank" href="<?php echo esc_url($header_ad_url); ?>"><img src="<?php echo esc_url($header_ad_image); ?>"/></a>
					</div>	
				<?php endif; ?>
			</div>
		</div><!-- .site-branding -->
	<?php
}

From the code above, replace this code block:


<?php if ( $header_ad_image ) : ?>
  <div class="header-ad">
    <a target="_blank" href="<?php echo esc_url($header_ad_url); ?>"><img src="<?php echo esc_url($header_ad_image); ?>"/></a>
  </div>	
<?php endif; ?>

with:


<div class="header-ad">
<!-- Insert your Google Adsense code here --> 
</div>

Don’t forget to turn on this new function.

Regards,
Kharis

Hi there,
Thanks for the help, but I installed the plug in which was fine but when I entered the above code and did the changes I got a Parse error!

Hello there,

Did you copy the code from email or get it directly from my thread on our support forum?

Regards,
Kharis

Hello,
I answer to your question I have copied thecode from the above thread. Nothing seems to be appearing, script below:

function greatmag_site_branding() {
$header_ad_image = get_theme_mod(‘header_ad_image’);
$header_ad_url = get_theme_mod(‘header_ad_url’);
?>
<div class=“site-branding vhome3 row m0”>
<div class=“container”>
<div class=“main-logo”>
<div class=“media”>
<?php if ( function_exists( ‘the_custom_logo’ ) && has_custom_logo() ) : ?>
<div class=“media-left”>
<?php the_custom_logo(); ?>
</div>
<?php endif; ?>
<div class=“media-body”>
<?php
$description = get_bloginfo( ‘description’, ‘display’ );
if ( $description || is_customize_preview() ) : ?>
<p class=“site-description site-slogan”><?php echo $description; /* WPCS: xss ok. */ ?></p>
<?php
endif;
if ( is_front_page() && is_home() ) : ?>
<h1 class=“site-title”>" rel=“home”><?php bloginfo( ‘name’ ); ?></h1>
<?php else : ?>
<p class=“site-title”>" rel=“home”><?php bloginfo( ‘name’ ); ?></p>
<?php
endif;
?>
</div>
</div>
</div>
<div class=“header-ad”>
<script type=“text/javascript”>
google_ad_client = “ca-pub-2769158218328338”;
google_ad_slot = “4654634002”;
google_ad_width = 728;
google_ad_height = 90;
</script>
<!-- One Touch Solution ad -->
<script type=“text/javascript"
src=”//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</div>
</div><!-- .site-branding -->
<?php

Hello there,

Thank you for your confirm. I found some bugs in your code. Please use this one instead. To do self debugging in order to correct the code, please read this codex.

Regards,
Kharis

Many thanks,
I have replaced with the above script and this still does not work?

Hello there,

Could you verify whether your Google ads code works well? Check it with a text widget and insert it into sidebar through Appearance > Widgets.

Regards,
Kharis

Many Thanks for the help and advise, managed ti sort it our placed asychronous code instead and cleared all caches. many thanks for your assistance.
Kind Regards
Barry Coyle

hello do you have any code for aligning the featured image in a post from centre to left.
Many Thanks

Sorry to be a pain but I have found another issue with you template the custom sidebar does not show it defaults to sidebar, you cant seem to use side bar for home page and custome sidebar in a post using page builder - any suggestions. Please refer also to my previous message regarding featured image
Many Thanks

Hello there,

To left align the featured image in single post page, add this CSS code into Appearance > Customize > Additional CSS in your dashboard.


.attachment-greatmag-single {
  float: left;
}

Regards,
Kharis

Many Thanks for the left float code, can you look at the last issue I sent over regarding the sidebar issue, custom sidebars dont work it defaults the main sidebar, for example I can use the side bar for home page, I have then created a custom page to use on a particular post, I can only use one or the other?
Thanks
Barry

Hello Barry,

You’ll still need the default sidebar since there is no route to replace the inner pages’ sidebar. To display different widgets, you can use the Widget Logic plugin and use is_page() conditional tag to restrict the selected widget visibility on your page.

Regards,
Kharis

Hi,

When implementing this code (I use Snippets which works the same way has My Custom Functions, I get this error

Don’t Panic

The code snippet you are trying to save produced a fatal error on line 46:

Cannot redeclare greatmag_site_branding() (previously declared in /home/voyages/public_html/cielquebecois.com/wp-content/themes/greatmag/inc/functions/functions-header.php:60)
The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.

Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.

Can you help me? If not, is there a simple way to just create a widget space in this area?

Thanks

Hi,

I tried with Custom function plugin and it worked. Thanks

Great! You figured that out!

Please let us know in a new topic if you have any further questions, or if we can provide you with any other assistance.

Regards,
Kharis