Advertisement Block in the Article Text

The best thing about a book is that it doesn't stop at the most interesting place for an advertising insertion...

Advertisement within the article

I know this is the most sophisticated and probably the most hated way for users to insert an advertisement block into the text of a post, but in some rare cases, it would be foolish to shun this method, in my opinion.

By the way, regarding advertising on blogs, a well-known blogger - Sosnovskiy raised the topic. I fully agree with him on this issue and do not agree for a second with those who wrote in the comments that his article is an attempt to justify himself.

And now, on the topic of: how to insert an advertisement into the content

I present a hack for WordPress, thanks to which you can easily insert an advertisement block into the text of an article, for example, a Google AdSense or Yandex.Direct advertisement block.

I don't remember where, but somewhere I saw the implementation of this task, only it was described how to insert an advertisement block into the text of an article after the <!--more--> tag. But what if the author of the site does not use this tag in principle? How then to insert the advertisement on all pages? The answer to this question is in my code:

<?php
/**
 * Inserts an advertisement block into the first line break or the first closing
 * tag `</p>` or `</table>` after a certain number of characters.
 *
 * @param number $nu After how many characters to look for a line break and insert the advertisement?
 *
 * version: 0.3
 */
add_filter( 'the_content', 'kama_content_advertise', -10 );

function kama_content_advertise( $text, $num = false ) {
	if( ! is_singular() ){
		return $text;
	}

	if( ! $num ){
		$num = 400;
	}

	ob_start();
	?>

	<div class="kama-inline-ads" style="float:right; margin:0 0 1em 1.5em;">
		advertising code here
	</div>

	<?php
	$adsense = ob_get_clean();

	// Uncomment if you need to insert the block just before the `<!--more-->` tag
	# return str_replace('<!--more-->', $adsense.'<!--more-->', $text);

	return preg_replace( '~[^^]{' . $num . '}.*?(?:\r?\n\r?\n|</p>|</table>)~su', "\${0}$adsense", trim( $text ), 1 );
}

Replace the "advertising code here" with a code with your advert.

The code inserts an advertising block into the first encountered line break or closing tag </p> or the </table> tag after a certain number of characters (specified in the $nu variable). That is, in this case, 400 characters will be skipped in the text of the article, after which the first line break (or the </p> tag) will be found and the advertising block will be inserted before it.

If you need to insert an advertising block before the more tag

Also, just in case, I have provided for a situation where you need to insert an advertising block before the <!--more--> tag, for this, uncomment the penultimate line.

Advertisement only for old posts

If there is a need to display an advertisement only for posts that are, say, 7 days old, use this code:

add_filter('the_content', 'kama_content_advertise', -10);

function kama_content_advertise( $text ){
	$days = 7; // after how many days after the post publication date to display the advertisement.
	$nu = 400; // after how many characters to look for a line break and insert the advertisement?

	$offset = (int) time() - (int) strtotime($GLOBALS['post']->post_date);
	if($offset < (3600*24*$days) ) return $text;

	//Advertising code
	$adsense = '
		<div style="float:right;margin:0 0 10px 15px;">
		advertising code here
		</div>
	';

	return preg_replace('@([^^]{'.$nu.'}.*?)(\r?\n\r?\n|</p>)@', "\\1$adsense\\2", trim($text), 1);
}

Placing an ad in this way can be convenient when you don't want to distract your regular readers with ads in new articles. After all, it is the regular readers who are the first to come to read your next masterpiece.