Insert Ads After Every X Paragraphs PHP - php

I am currently using this code to insert Ads after the 5th paragraph and it works just fine.
//Insert ads after fifth paragraph
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<center>AD CODE</center>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 5, $content );
}
return $content;
}
// Parent Function
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 );
}
But, I would like the ads to be repeated after every 5 paragraphs, not just once. What do I do?

Instead of
if ( $paragraph_id == $index + 1 ) {
you can use
if ( $index % $paragraph_id === 0 ) {
that should do it.

Related

Please what Am i doing wrong, getting Notice of Undefined variable in PHP 8

I got a Warning message on my WordPress site trying to insert a featured image after the first paragraph. It worked perfectly without warning or error in PHP7.4 but got the below warning on PHP 8.1
Please would need some help thanks in advance.
Warning: Undefined variable $post in /www/wwwroot/.../wp-content/themes/..../functions.php on line 7
Warning: Attempt to read property "ID" on null in /www/wwwroot/.../wp-content/themes/...../functions.php on line 7
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$feat_img = get_the_post_thumbnail($post->ID, 'post-single');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( '<div class="top-featured-image">' . $feat_img . '</div>', 1, $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're trying to access a $post variable through $post->ID tho you didn't define it before.
We could first fetch the post object through get_post() then retrieve the ID, but we can do better. We can use get_the_ID() to retrieve the post ID.
<?php
add_filter( 'the_content', function ( $content ) {
if ( ! is_admin() && is_single() ) {
$thumbnail = get_the_post_thumbnail( get_the_ID(), 'post-single' );
return prefix_insert_after_paragraph( '<div class="top-featured-image">' . $thumbnail . '</div>', 1, $content );
};
return $content;
}, 20 );

Insert adverts before the first h2 tag of the post content

I am trying to insert an advert before the first h2 tag in a post. The code I have tried is below, but the advert comes after the content and not before. I'm looking to insert the advert before the content, and not after.
/*Insert ads after second paragraph of single post content.*/
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = 'This is my ads';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
//Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</h2>';
$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 );
}
Any help would be appreciated.
Try this:
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '<h2>';
$paragraphs = explode( $closing_p, $content );
if ($paragraphs) {
$paragraphs[0] = $paragraphs[0].$insertion;
}
return implode( '<h2>', $paragraphs );
}

No ads are loading on amp-mode-touch

Like the title says. I’ve successfully placed adsense ads on my AMP posts. While testing it out on my desktop via firebug, adverts are coming up as intended. However, I cannot get any ads to come up on my mobile? The pages body class changes to amp-mode-touch and the adverts div containers disappear completely.
Here is an example URL - http://www.gamersgreed.com/kingdom-come-deliverance-fps-boost-optimization/amp
And here is the code i'm using to produce the ads -
//Insert ads after third and seventh paragraph of any single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div style="text-align: center;"><amp-ad width=300 height=250
type="adsense"
data-ad-client="ca-pub-7568065840567088"
data-ad-slot="1234194857">
<div placeholder></div>
<div fallback></div>
</amp-ad>
</div>
';
$ad_code2 = $ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $ad_code, 2, $content );
$content = prefix_insert_after_paragraph( $ad_code2, 7, $content );
return $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 );
}

Adding Ads to Second Paragraph of WordPress Page

I have the following code that I'm using in my functions.php file to add a Google ad to the second paragraph of each post. As it stands, the code works but only on posts. I was hoping someone on this forum could assist with tweaking the code so this function is applied to pages as well to posts.
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>AD Code to go here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $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 );
}
Instead of is_single(), which only tests whether a single posts are shown, you should use is_singular(), which test for is_single(), is_page() or is_attachment()

How to run a function instead of text in this wordpress plugin?

Here is a simple code which shows the Adsense or any ad code after second paragraph on single post page. But I want to know how to modify it so that it can run a dynamic function.
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $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 );
}
In fact I want to replace
$ad_code = '<div>Ads code goes here</div>';
with
$ad_code = '<?php if(function_exists('echo_ald_crp')) echo_ald_crp(); ?>';
Is there any way to do it??
Well, you can't assign PHP to a string var and expect it to execute. You could create a function with the code in it, have it return something, and then set the variable equal to the function call. Like this:
function output_function() {
//do something here
return $thing_you_want_to_return;
}
function prefix_insert_post_ads( $content ) {
$ad_code = output_function();
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
That way, when the variable is assigned, it'll run your function and return what you want to be echoed out with prefix_insert_after_paragraph().

Categories