Community Forums › Forums › Archived Forums › General Discussion › Want to add an ad widget after certain number of paragraphs via a function
Tagged: Add Widget, the_content
- This topic has 5 replies, 3 voices, and was last updated 6 years, 5 months ago by David Borrink.
-
AuthorPosts
-
June 13, 2018 at 7:54 pm #220809David BorrinkParticipant
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...
http://No site to share. It's an internal setup// 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(); } } }
June 14, 2018 at 3:52 am #220817Victor FontModeratorFirst, 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 #220822Brad DaltonParticipantHere's some working examples https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
June 14, 2018 at 7:29 am #220824David BorrinkParticipantThanks, 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 #220826Victor FontModeratorYou 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 #220845David BorrinkParticipantThat'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.
-
AuthorPosts
- The topic ‘Want to add an ad widget after certain number of paragraphs via a function’ is closed to new replies.