• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Want to add an ad widget after certain number of paragraphs via a function

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Want to add an ad widget after certain number of paragraphs via a function

This topic is: resolved

Tagged: Add Widget, the_content

  • This topic has 5 replies, 3 voices, and was last updated 7 years, 11 months ago by David Borrink.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • June 13, 2018 at 7:54 pm #220809
    David Borrink
    Participant

    I want to put an ad widget in my content area on pages and a few specifically category posts. I wrote up a function that contains a snippet I found at https://www.doitwithwp.com/inserts-ads-after-a-few-paragraphs-wordpress/. I placed it within a function that would filter the_content to get the results I want, but nothing is happening and I can't figure why. Here is the code...

    // Register adbar widget for to go after a specific paragraph on pages and only a specific category of posts
        register_sidebar( array(
                'name' => __( 'Below Post Title (unit studies) and Page Title Ad Space', 'tto' ),
                'id' => 'post-page-below-title-ad-space',
                'description' => __( 'PAGES AND UNIT STUDY POSTS. Will appear after a specific number of paragraphs.', 'tto' ),
                'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                'after_widget' => '</aside>',
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',
        ) );    
    
    // place widget after specified paragraph on pages and only posts with the unit-study-activities category
    
    if( is_page() || is_category('unit-study-activities') ) {
        
        add_filter( 'genesis_entry_content' , 'sb_widget_in_the_content' );
        
        function sb_widget_in_the_content () {
            // next line is the paragraph number to specify...
            $show_after_p = 2;
            $content = apply_filters('the_content', $post->post_content);
                if(substr_count($content, '<p>') > $show_after_p)
                {
    	           $contents = explode("</p>", $content);
    	           $p_count = 1;
    	           foreach($contents as $content) {
    		          echo $content;
    		      if($p_count == $show_after_p) {
    		          ?>
                        <div class="content-ad-space">
                            <?php if ( is_active_sidebar( 'post-page-below-title-ad-space' ) ) : ?>
                           <?php dynamic_sidebar( 'post-page-below-title-ad-space' ); ?>
                            <?php endif; ?>
                            </div><!-- .content-ad-space--> 
                        <?php
    		              }
    		         echo "</p>";
    		         $p_count++;
                    } 
    	    }
            else {
            the_content();
            }
    
        }
        
        }
    http://No site to share. It's an internal setup
    June 14, 2018 at 3:52 am #220817
    Victor Font
    Moderator

    First, genesis_entry_content is not a filter, it's an action. You're using it incorrectly.

    The rest of your code function isn't going to work with add_filter. Apply_filters requires that some value be returned to it by the add_filter. They work hand-in-hand with each other. Your code doesn't have any return statements at all.

    Next, I wouldn't use apply_filters in functions.php. I would use add_filter('the_content', 'sb_widget_in_the_content'). You may have to add a priority to get this to work properly.

    The function declaration would be function sb_widget_in_the_content( $content ).

    Move the conditional (if), into the function. Process the content that meets the conditional, last line in the function is return $content.

    You'll have to do examine the content that is passed to the function to make sure it actually has paragraph tags to count. If not, the function won't work. I recommend the Kint debugger for this.


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    June 14, 2018 at 5:57 am #220822
    Brad Dalton
    Participant

    Here's some working examples https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content


    Tutorials for StudioPress Themes.

    June 14, 2018 at 7:29 am #220824
    David Borrink
    Participant

    Thanks, Victor and Brad, I always appreciate your input on things. I made a bunch of changes from your suggestions, Victor, and thanks for the working examples, Brad. Here's what I came up with...

    // Register adbar widget for to go after a specific paragraph on pages and only a specific category of posts
        register_sidebar( array(
                'name' => __( 'Below Post Title (unit studies) and Page Title Ad Space', 'tto' ),
                'id' => 'post-page-below-title-ad-space',
                'description' => __( 'PAGES AND UNIT STUDY POSTS. Will appear after a specific number of paragraphs.', 'tto' ),
                'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                'after_widget' => '</aside>',
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',
        ) );    
    
    // place widget after specified paragraph on pages and only posts with the unit-study-activities category
    
     
        add_filter( 'the_content' , 'sb_widget_in_the_content' );
        
        function sb_widget_in_the_content ($content) {
           
            if( is_page() || in_category('unit-study-activities') )
            // next line is the paragraph number to specify...
            $show_after_p = 2;
            $content = apply_filters('the_content', $post->post_content);
                if(substr_count($content, '<p>') > $show_after_p)
                {
    	           $contents = explode("</p>", $content);
    	           $p_count = 1;
    	           foreach($contents as $content) {
    		          echo $content;
    		      if($p_count == $show_after_p) {
    		          ?>
                        <div class="content-ad-space">
                            <?php if ( is_active_sidebar( 'post-page-below-title-ad-space' ) ) : ?>
                           <?php dynamic_sidebar( 'post-page-below-title-ad-space' ); ?>
                            <?php endif; ?>
                            </div><!-- .content-ad-space--> 
                        <?php
    		              }
    		         echo "</p>";
    		         $p_count++;
                    } 
    	    }
            return $content;
        }

    And I got this error with so much to unpack I don't even know where to start! LOL. Maybe you can decipher this in a simpler way because it's way too complicated.

    Fatal error: Uncaught Error: Call to undefined function add_filters() in /Applications/MAMP/htdocs/wordpress/wp-content/themes/sallieborrink-2018/functions.php:349 Stack trace: #0 /Applications/MAMP/htdocs/wordpress/wp-includes/class-wp-hook.php(286): sb_widget_in_the_content('<p>1 This is a ...') #1 /Applications/MAMP/htdocs/wordpress/wp-includes/plugin.php(203): WP_Hook->apply_filters('<p>1 This is a ...', Array) #2 /Applications/MAMP/htdocs/wordpress/wp-includes/post-template.php(240): apply_filters('the_content', '1 This is a tex...') #3 /Applications/MAMP/htdocs/wordpress/wp-content/themes/genesis/lib/structure/post.php(361): the_content() #4 /Applications/MAMP/htdocs/wordpress/wp-includes/class-wp-hook.php(286): genesis_do_post_content('') #5 /Applications/MAMP/htdocs/wordpress/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #6 /Applications/MAMP/htdocs/wordpress/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #7 /Applications/MAMP/htdocs/wordpress/wp-content/themes/genesis/lib/structure in /Applications/MAMP/htdocs/wordpress/wp-content/themes/sallieborrink-2018/functions.php on line 349

    The "1 This is a...." is the first paragraph in the test post. I have 12 numbered paragraphs to help me determined the placement of my inserted ad widget.

    June 14, 2018 at 9:08 am #220826
    Victor Font
    Moderator

    You don't need this in the function: $content = apply_filters('the_content', $post->post_content);


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

    June 14, 2018 at 7:59 pm #220845
    David Borrink
    Participant

    That's it! It works perfectly. Just the way I wanted it to.

    Thank you!

    And now I need to brush up a little more on actions and filters and the proper understanding of them in the content.

  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Want to add an ad widget after certain number of paragraphs via a function’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble