We recently had to add Google AdSense code on not one but two clients’ website. So, I think its best to make it a tutorial so that all WordPress site owners can do this themselves. The complexity of this tutorial is low and I am sure that most site owners can do this sort of work themselves. If you get stuck, let us know.
For this assignment, we used the example of famous WordPress Genesis theme.
Adsense is a JavaScript code provided by Google. When put inside HTML, the code creates a visual ad that can be clicked. The publishers can put the code on their websites and earn money when people click on the Ads. You can get the Google Adsense code from your Adsense Account. Adsense discussion is out of scope for this tutorial. You can find much help on the internet or Google itself.
First, we create a sidebar in functions.php of your child theme so that it appears as a widget in wp-admin. The code is given below.
//new widget top googlead
genesis_register_sidebar( array(
'id' => 'site-top-googlead',
'name' => __( 'Site - Top - Google Ad', 'news' ),
'description' => __( 'Add Google Ad code in here.', 'news' ),
) );
After writing above code, if you go to wp-admin -> appearance -> widget, you’ll see a brand new widget as shown below. But it will be empty. So drop a Text widget from the available widgets list in left and paste the code from step 1 in there.
For visual cleanliness, we enclosed the widget in the following div to make it align centered.
For our clients, we needed to show the Google ad right after header was complete. Genesis provides us with a lot of places that you can add the widget. You can see the list of hooks to find the best place for you. You can add the following code in the same child theme functions.php as before.
add_action( 'genesis_after_header', 'genesis_do_googlead' );
function genesis_do_googlead(){
dynamic_sidebar('site-top-googlead');
}
After this, just clear your cache and refresh your frontend website. Your ad should start showing right away.
If you need help of a WordPress developer with any of the steps, send us a message below.
Our support staff is available 24/7 to take support calls and messages from clients.
We involve client in all stages of software development to deliver satisfaction and peace of mind.
We hire developers that are technically strong and discuss the project thoroughly before starting.
If you are not happy with the quality of work or we fail to achieve final technical goal, we’ll not take your money.
We have worked with some clients for over 7 years. Our commitment and dedication is a matter of pride for us.
We care about your investment. We will tell it straight if you are making a bad decision.
NDA implied. No spam. Privacy guaranteed.
Best web, app, eCommerce and custom software development company. We provide services to more than 100 clients world-wide.
Contact The Right Software today to discuss the your next big idea.