{"id":5884,"date":"2017-03-02T15:00:06","date_gmt":"2017-03-02T13:00:06","guid":{"rendered":"https:\/\/www.reliablesoft.net\/?p=5884"},"modified":"2021-06-27T19:35:45","modified_gmt":"2021-06-27T16:35:45","slug":"add-google-adsense-wordpress","status":"publish","type":"post","link":"https:\/\/www.reliablesoft.net\/add-google-adsense-wordpress\/","title":{"rendered":"How to Add Google AdSense Ads to WordPress (With and Without a Plugin)"},"content":{"rendered":"
To start making money with Google AdSense<\/a><\/strong>, you need to add the AdSense Ads to your website.<\/p>\n When it comes to WordPress websites or blogs, there are two recommended ways to do this.<\/p>\n First, is through the use of the official Google AdSense plugin and second by making some simple changes to your WordPress files (my preferred way).<\/p>\n Let\u2019s see these two methods in more details.<\/p>\n The Google AdSense team has created a plugin for WordPress users, to make it easier to add AdSense to their websites.<\/p>\n Update March, 2017:<\/strong> Google has announced<\/strong><\/a> that the AdSense Plugin is going away so it’s better to go straight to the part of adding AdSense to your website manually.<\/p><\/blockquote>\n You can download and install the plugin from the WordPress repository, here is the link<\/strong><\/a>.<\/p>\n There are detailed instructions on how to configure the plugin here<\/strong><\/a>.<\/p>\n <\/p>\n Note:<\/strong> If you decide to use the plugin, make sure that you remove any AdSense ad placements that you added to your website without the plugin.<\/p>\n Although the plugin is easy to use, I prefer to add AdSense ads manually to my websites since you have more control on where the code will appear and you don\u2019t have to worry about making changes to your theme or templates.<\/p>\n The plugin enables you to add the Ads without making changes to code but this means that if you change theme or the layout of your pages, you will have to configure the plugin again (unless you have used the manual placement<\/strong><\/a> options).<\/p>\n In the case of adding the code manually (see below) to your theme, it\u2019s a matter of copy\/paste the code from one theme to the other.<\/p>\n My preferred way to add AdSense to WordPress, is the manual way. It does involve making changes to the code but once you do it for one website, you can easily re-use the code for other websites.<\/p>\n On a typical WordPress blog, you can place AdSense ads in the following places:<\/p>\n How to place AdSense Ads in a Widget<\/strong><\/p>\n In this case, adding AdSense ads is a matter of copy\/paste the code provided by AdSense to a \u2018text widget\u2019, in the appropriate widget area.<\/p>\n The steps to follow:<\/p>\n Hint:<\/strong> Use a name the accurately describes the ad type and position. For example, \u2018Home Page, Top Banner, Responsive\u2019.<\/em><\/p>\n <\/p>\n Copy the Ad code<\/em> provided by Adsense.<\/p>\n <\/p>\n Paste the code to a Text Widget<\/em> and place it to the appropriate Widget Area.<\/p>\n <\/p>\n You can now SAVE the widget and in a few minutes, AdSense will start showing on your website.<\/p>\n You can follow the exact same procedure, whenever you want to add Adsense to any widget in WordPress.<\/p>\n Note:<\/strong> You can wrap the Adsense code in a <div> and set the width to 100% and add other styling properties to make the ads look good (see the example above).<\/p>\n This is an example of Adsense Ads in the Sidebar.<\/p>\n <\/p>\n IMPORTANT:<\/strong> The examples below are based on the Genesis Framework<\/strong><\/a>. If you are NOT using a genesis child theme, then you need to change the hook specified in the ADD_ACTION line (first part of the code) to match the hooks available in the core WordPress installation. View the complete list of WordPress Actions<\/a><\/strong>.<\/p>\n IMPORTANT 2:<\/strong> If you make a mistake in your functions.php<\/em> file, your WordPress website will not load so make sure that you make a backup of your file before editing.<\/p>\n To add AdSense to appear at the top of your posts, you will need to add the code below to your functions.php<\/em> file.<\/p>\n Note:<\/em><\/strong> Before continuing to the next step, you need to follow the procedure above to create a new AD Unit in your AdSense Account.<\/em><\/p>\n Use an FTP client and locate the functions.php<\/em> file (the location of the file is at: \/wp-content\/themes\/yourtheme\/functions.php<\/em>)<\/p>\n Edit the file and add the code below. The above code will add to any single page, category page or search page an AdSense ad that is wrapped with a css class (adsense-top).<\/p>\n Notice how I changed the AdSense code to force the ads to appear as 300×250 when the viewed on mobile (below 420px resolution).<\/p>\n Note: You need to use your own AdSense Code for the Ads as provided by AdSense.<\/strong>\u00a0<\/strong><\/p>\n Let\u2019s say that you want ads to appear after the second paragraph in all your blog posts. You can accomplish this by adding the code below to your functions.php<\/em> file.<\/p>\n Things you should know about the above code:<\/p>\n Here is an example of a full-width AdSense ad unit shown after the second paragraph.<\/p>\n <\/p>\n To add AdSense below your posts, you simply need to use the same logic like the \u201cTop of Posts\u201d but change the hook so that the ads will appear below the posts.<\/p>\n Here is the code template to use: <\/p>\n If you find the above code confusing, then go with the first option and use the official Google AdSense plugin to add the ads to your WordPress blog.<\/p>\n It\u2019s easier to use and doesn\u2019t require editing your website files. If you feel confident with editing your functions.php<\/em> file then you can use the code templates above and have more control on your Ads.<\/p>\n Either way, it\u2019s always recommended to use RESPONSIVE AD UNITS so that your ads will show properly on your desktop and mobile websites.<\/p>\n In case something is not clear, let me know in the comments.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":" To start making money with Google AdSense, you need to add the AdSense Ads to your website. When it comes to WordPress websites or blogs, there are two recommended ways to do this. First, is through the use of the official Google AdSense plugin and second by making some simple changes to your WordPress files […]<\/p>\n","protected":false},"author":2,"featured_media":5894,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","mc4wp_mailchimp_campaign":[]},"categories":[284],"tags":[296,131,295],"_links":{"self":[{"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/posts\/5884"}],"collection":[{"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/comments?post=5884"}],"version-history":[{"count":1,"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/posts\/5884\/revisions"}],"predecessor-version":[{"id":29113,"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/posts\/5884\/revisions\/29113"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/media\/5894"}],"wp:attachment":[{"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/media?parent=5884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/categories?post=5884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reliablesoft.net\/wp-json\/wp\/v2\/tags?post=5884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}How to add Google Adsense Ads to my WordPress Website? (with a Plugin)<\/h2>\n
Add AdSense Ads by Editing your WordPress Files (Without a Plugin)<\/h2>\n
\n
\n
How to Add AdSense to WordPress Without a Plugin?<\/h2>\n
How to Add AdSense Ads at the top of a page (below the header)<\/h3>\n
\n
\nadd_action('genesis_before_content', 'header_ad');
\nfunction header_ad() {
\nif (is_page() || is_category() || is_search() ) { ?><div class=\"adsense-top\"><style type=\"text\/css\">
\n.adslot_4 { width: 1000px; height: 90px; }
\n@media (max-width:420px) { .adslot_4 { width: 300px; height: 250px; } }<\/style>
\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>
\n<!-- XXXXXXXX -->
\n<ins class=\"adsbygoogle adslot_4\"
\nstyle=\"display:inline-block\"
\ndata-ad-client=\"ca-pub-XXXXXXXXXX\"
\ndata-ad-slot=\"XXXXXXX\"
\n<\/ins>
\n<script>
\n(adsbygoogle = window.adsbygoogle || []).push({});
\n<\/script>
\n<\/div>
\n<?php }}
\n<\/code><\/p>\nAdd AdSense After the 2ndst<\/sup> Paragraph Within Blog Posts<\/h3>\n
\n\/\/ADD ADSENSE AFTER 2ND PARAGRAPH
\nadd_filter( 'the_content', 'prefix_insert_inpost_ads' );
\nfunction prefix_insert_inpost_ads( $content ) {
\n$ad_code =\u2019 <div class=\"googleadsrectangle\"><\/strong>
\n<style type=\"text\/css\"><\/strong>
\n.adslot_1 { width: 650px; height: 280px; }<\/strong>
\n@media (max-width:420px) { .adslot_1 { width: 300px; height: 250px; } }<\/strong>
\n<\/style><\/strong>
\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><\/strong>
\n<!-- SAMPLE ADD --><\/strong>
\n<ins class=\"adsbygoogle adslot_1\"<\/strong>
\n\u00a0\u00a0\u00a0\u00a0 style=\"display:inline-block\"<\/strong>
\n\u00a0\u00a0\u00a0\u00a0 data-ad-client=\"ca-pub-XXXXXXXXXXXX\"<\/strong>
\n\u00a0\u00a0\u00a0\u00a0 data-ad-slot=\"XXXXXXXX\"<\/strong>
\n<\/ins><\/strong>
\n<script><\/strong>
\n(adsbygoogle = window.adsbygoogle || []).push({});<\/strong>
\n<\/script><\/strong>
\n<\/div><\/strong>';
\nif ( is_single() && ! is_admin() ) {
\nreturn prefix_insert_after_paragraph( $ad_code, 2, $content );
\n}
\nreturn $content;
\n}
\nfunction prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
\n$closing_p = '<\/p>';
\n$paragraphs = explode( $closing_p, $content );
\nforeach ($paragraphs as $index => $paragraph) {
\nif ( trim( $paragraph ) ) {
\n$paragraphs[$index] .= $closing_p;
\n}
\nif ( $paragraph_id == $index + 1 ) {
\n$paragraphs[$index] .= $insertion;
\n}
\n}
\nreturn implode( '', $paragraphs );
\n}
\n<\/code><\/p>\n\n
Add AdSense Ads Below WordPress Posts<\/h3>\n
\n
\nadd_action('genesis_after_entry_content', 'google_bottom_ads');
\nfunction google_bottom_ads() {
\nif ( is_single() ) { ?>
\n<div class=\"googleadsrectanglebottom\">
\n<style type=\"text\/css\">
\n.cs-posts-bottom-resp { width: 650px; height: 280px; }
\n@media (max-width:420px) { .cs-posts-bottom-resp { width: 300px; height: 250px; } }
\n<\/style>
\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>
\n<!-- XXXXXXXXXXXX -->
\n<ins class=\"adsbygoogle cs-posts-bottom-resp\"
\nstyle=\"display:inline-block\"
\ndata-ad-client=\"ca-pub-XXXXXXXXXXXX\"
\ndata-ad-slot=\"XXXXXXXX\"><\/ins>
\n<script>
\n(adsbygoogle = window.adsbygoogle || []).push({});
\n<\/script>
\n<\/div>
\n<?php } };
\n<\/code>
\nHere is an example of an AdSense Ad unit shown below a blog post.<\/p>\nConclusion<\/h2>\n