How to insert Google AdSense Ads in GeneratePress without Plugin?

Do you want to insert Google AdSense Ads in GeneratePress without Plugin? If yes then, you are on the right page.

There are many ways to insert Google AdSense Advertisement in WordPress using plugins. But in this guide, we are going to insert Google AdSense code without a plugin.

Furthermore, you’ll learn

  • What is Google AdSense?
  • How to use GeneratePress Elements?
  • How to use Code Snippets?
  • Inserting Google AdSense in the GeneratePress Header.
  • Display Ads after Post Title.
  • Inserting Ads after content in a single post.
  • Adding AdSense between paragraphs.
  • Setting Up Auto Ads in GP Theme.
  • Show Ads in the GeneratePress sidebar.
Insert Google AdSense Ads in GeneratePress without Plugin

What is Google AdSense?

Google AdSense is an advertising platform owned and maintained by Google that allows website owners to insert and display ads on their websites. Google helps creators to monetize their blogs/websites with AdSense.

Google AdSense Advertising Platform

AdSense is the number 1 advertising platform available on the internet that pays a good sum of money.

AdSense can be implemented on your website with the help of Auto Ads or can be placed manually. You can use display ads, text ads, matched content ads, etc in your blog/website.

Insert Google AdSense Ads in GeneratePress Free Theme

If you are using a free version of the GP theme then you can find plenty of Google AdSense plugins to insert the AdSense Ads.

These are some free plugins available in the WordPress repository. You can find many more paid plugin too that helps to insert Google AdSense code.

I haven’t tested the plugin personally, so please read reviews about the plugin before implementing it on your site.

Insert Google AdSense Ads in GeneratePress without Plugin

I am using GeneratePress Premium Theme and I am writing this tutorial on the behalf of the premium theme. In GeneratePress Premium Theme, you can use the plugin method too but we are going to insert an Advertisement code without any AdSense plugin.

In this method, we are going to use GeneratePress Premium Elements Modules. Let’s get started.

Adding a New Hook in GeneratePress
  • Go to Appearance > GeneratePress > Elements > Activate.
  • Go to Appearance > Elements > Add New.
  • Choose Element Type as Hook.
  • Add a Hook Title.
  • Follow the following Method.

Place AdSense Ads in GeneratePress Header

The header part of the site is a great place to show ads. Usually, we go for big banner ads in the header section of GP Theme. The best ad units to place in the header is Leaderboard Ads i.e. 728 x 90 also, you can try 970 x 90.

720 x 90 Leaderboard
970 x 90 Leaderboard
  • Go to AdSense > Ad > Ad Units > Display Ads
  • Go to Ad Size > Fixed Size > 728 x 90 or 970 x 90
  • Create an Ad and Copy the AdSense code.
  • Now, Go to Appearance > Elements
  • Choose Element Type as Hook.
  • Name a Hook.
  • Paste the AdSense code.
  • Now go to Elements Settings > Hook and choose after_header.
  • Go to Location and choose the Entire Site.
  • Hit Publish button.
Header Ads Elements in GeneratePress
Inserting Hook after_header in GeneratePress Theme
Display Rules in Entire Site in GeneratePress Theme

Now you can see the Ads in the Header. In the same way, you can insert different ad networks code to display ads on your site.

Display AdSense Ads After Post Title in GeneratePress

Another popular and most widely used place to display AdSense Ads is after the post title on a single page. Here in GeneratePress, we are going to use Elements modules. Generally, I go with the Display Ads unit in AdSense.

Display AdSense Ads After Post Title in GeneratePress
  • Go to AdSense > Ad > Ad Units > Display Ads
  • Go to Ad Size > Responsive.
  • Create an Ad and Copy the AdSense code.
  • Now, Go to Appearance > Elements
  • Choose Element Type as Hook.
  • Name a Hook.
  • Paste the AdSense code.
  • Now go to Elements Settings > Hook and choose after_entry_title.
  • Go to Location and choose All Singular.
  • Hit Publish button.

Insert AdSense Ads After Entry Content in GeneratePress

Usually, I go with the link ad to place Ads after content in a single post but you can place any kind of Google AdSense Ads Unit.

Insert AdSense Ads After Entry Content in GeneratePress
  • Go to AdSense > Ad > Ad Units > Link Ads
  • Go to Ad Size > Responsive (Horizontal)
  • Create an Ad and Copy the AdSense code.
  • Now, Go to Appearance > Elements
  • Choose Element Type as Hook.
  • Name a Hook.
  • Paste the AdSense code.
  • Now go to Elements Settings > Hook and choose after_entry_content.
  • Go to Location and choose All Singular.
  • Hit Publish button.

Insert Ads in Between Content in GeneratePress

If you write long content then inserting Ads between certain paragraphs is always the best way to generate additional income.

Insert Ads in Between Content in GeneratePress WordPress

For this, you need to create a child theme and use the following code in functions.php or you can use a Code Snippets plugin. This plugin helps to insert code to functions.php without messing with other codes.

Here we are going to insert the AdSense code after the fourth paragraph. so Let’s display some ads using code in functions.php

  • Go to Plugins > Add New > Search
  • Search Code Snippets > Install > Activate
  • Code Snippets > Add New
  • Give a Title to a Snippet.
  • Paste the following Code and Hit Activate Button
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = ' Paste your AdSense Code Inside this quote';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 4, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}

You can use the same code to display Ads by changing numbers from 4th to 5th or others accordingly.

Setup Auto AdSense Ads in GeneratePress

If you don’t want to place manually and want one code to work everywhere on your site then Auto AdSense Ads is a great way to start. It is a set and forgets type of Ad. Auto Ads display Ads automatically wherever possible.

Auto AdSense Ads in GeneratePress Theme

so how to display auto AdSense ads in generatepress? Here is the method

  • Go to AdSense
  • Navigate Ads > Auto Ads
  • Click Setup Auto Ads
  • Copy the code snippet
  • Go to Appearance > Elements > Add New
  • Choose Element Type as Hook and Name it.
  • Paste the Auto Ads code
  • Go to Hook Settings and choose wp_head
  • Go to Display Rules, Choose Location as Entire Site and Exclude Page/All Pages.

Display AdSense Ads in GeneratePress sidebar

Generally, you can go with 300×250 or 300×600 ad sizes to show ads in the sidebar. The medium rectangular (300×250) ad size and Large Skyscraper (300×600) are the most popular and highest-performing AdSense Ads units.

Display AdSense Ads in GeneratePress sidebar

So let’s display ads in the GeneratePress sidebar.

  • Go to AdSense > Ad > Ad Units > Display Ads
  • Go to Ad Size > Fixed Size > 300×250 or 300×600
  • Create an Ad and copy the AdSense code.
  • Go to Appearance > Widgets.
  • Add a Text Widget to the sidebar.
  • Paste the code (Text Editor) and Hit the Save button.

Check Out: GeneratePress WordPress Theme Review

Conclusion

I think now you can insert Google AdSense Ads in GeneratePress without a plugin. If you are not familiar with code then you can use a plugin method to display Ads on your site.

Furthermore, if you have any questions/ideas then please feel free to comment below.

Share:

I am obsessed with WordPress and write articles about WordPress Speed Optimization, SEO, and Blogging. I mostly publish tutorials on GeneratePress, Astra, Kadence, and GenerateBlocks.

44 thoughts on “How to insert Google AdSense Ads in GeneratePress without Plugin?”

  1. Hi, Suraj sir I want to create a child theme like yours using generatepress. Can you make a detailed guide on it? I have commented on your previous post but you didn’t reply to me. Plz, tell me if you can or not? Love from Maharashtra ❤️

    Reply
    • Hi Rakesh, I didn’t get your comment on my blog previously.
      Are you using free GeneratePress Theme or Paid one?
      If you are using Paid theme then you can get awesome support in GeneratePress Forum at generatepress.com/support
      If you are using free then you need to purchase the paid theme.

      Thank you.

      Reply
  2. Hi Suraj, Hope you are doing well. Its awesome written. Got a lot of information and will apply also.

    I just installed GeneratePress but I wanna know which template theme of GeneratePress you are using on this site. I mean its cool. Have you build is entirely yourself with page builders? if yes do let me know about the builder too.

    Moreover

    Reply
    • Hi Shoaib, Thank you for your nice words.
      I am using GeneratePress Premium Theme, I am not using any page builder. I have imported Marketer site library and customize it.

      Thank You

      Reply
  3. 1. How can I add thumbnail in featured or recent posts in GeneratePress premium?
    2. Is there any option to customise tags and categories section at the end of single post for a rich look?

    Reply
  4. 1. Yes, you can add it, go to wp show posts and there is images section. Enable it with proper height and width.
    You can follow the screenshot Images section in WP Show Posts
    Also, You can follow related posts article
    2. For the second question, I can’t understand. Can you share a screenshot of what you want to customize?

    Reply
  5. Hallo. First, thank you for sharing. It so helped me to insert ads in my blog.

    I’ve got a question. How can I put the ads to before the last paragraph? Because your code it’s only for the selected paragraph. I hope you understand about my question. Thank you.

    Reply
    • Hi Reza, I think you can get code in StackOverflow to display ads just before the last paragraph.
      Search it. If you can’t find it, I will help you.
      Thanks

      Reply
  6. Yoast SEO offers integrated breadcrumb navigation. But, you need to insert the PHP code on your header or single.php file in order to display the breadcrumb structure. If you are using Yoast SEO plugin with GeneratePress theme, you can add the breadcrumb without modifying PHP files .

    Reply
    • Hi Naim, Auto Ads can increase your revenue but it can make your UX ugly. Please test Auto Ads for some time and if you feel bad then replace it with Manual Ads.

      I am using Manual Ads on one of my sites.
      Thanks

      Reply
    • Hi, GeneratePress has an inbuilt CSS class to hide into different devices.
      You can use hide-on-mobile and hide-on-tablet CSS classes.
      Thanks

      Reply
  7. Hai Suraj I want insert adsense after first paragraph and featured image in single post , so using your code in instead of 4 , I can change it to 1 right. Thank You

    Reply
  8. Hey Suraj,

    thanks for the great article! I was wondering if your method would work with Google Ad Manager (formerly known as DFP) too. Since the tool is so powerful (show your own ads, implement different ad networks) i’m looking for a non-plugin solution just like yours for adsense. And do you have any experiences with lazyloading the Ads? Google core web vitals update is around the corner, and i’m a bit hesitant implementing ads for that reason. Nevermind if you aint got time to answer, thanks for sharing your knowledge!

    Cheers, Ben

    Reply
    • Hi Ben,
      I haven’t tried working with GAM so if I got to try it out then I will write a tutorial about it.
      I am using Elements from GPP to insert ads to my other sites you can use it for plugin-less solution.
      I have lazy-loaded AdSense for CWV but my revenue got dropped so I removed it.

      Thanks

      Reply
  9. Suraj Katwal bro I need your help for adding AdSense ad before and after on the list block. I am using plugin ad inserter but there is no option to show ads on the before or after list. It allows only before paragraph and after paragraph. Any solution do you recommend?

    Reply
  10. hi Suraj! thanks for your tutorial! how can I register multiple widget areas between paragraphs inside single post article content?

    thank you!

    Reply
    • Hi Abdullah,
      You can insert it, I have given the code in this tutorial too. Check Insert Ads in Between Content in GeneratePress section.
      Thanks

      Reply
  11. Hello. Thanks for the quality guide. I couldn’t do this. For example, how can I add an ad in the text after both the 4th paragraph and the 8th paragraph. When I add the code you shared twice, I get an error in the code snippets and function.php file. Thank you.

    Reply
  12. I also want to ask one thing. To get rid of the Google CLS problem, is it enough to do the layout as you described? What else would you recommend?

    Reply
  13. Here is a code, i created this , if you want to insert different adsense ads banner before each h2 tags in your wordpress post.
    function insert_ads_before_h2($content) {
    // An array of ads code
    $ads_codes = array(
    ‘Ads code 1 goes here’,
    ‘Ads code 2 goes here’,
    ‘Ads code 3 goes here’,
    );

    // Counter to keep track of the current ad
    $counter = 0;

    // Insert the ads code before each h2 tag
    $content = preg_replace_callback(‘//’, function($matches) use (&$counter, $ads_codes) {
    return $ads_codes[$counter++] . $matches[0];
    }, $content);

    return $content;
    }

    // Use the function to filter the content of the post
    add_filter(‘the_content’, ‘insert_ads_before_h2’);

    Reply

Leave a Comment