Good ad spaces on sites and blogs running on WordPress can be found not only on the homepage but also within each article. Especially if the text is written with a specific banner or several specific advertisements in mind. This is when the question arises: how to insert ads into a WordPress article as the author wants?
There are many ways. Working with ad placement in articles can be done through codes or with the help of plugins. Plugins are not liked by all webmasters, many believe that “gimmicks” weigh down the site. However, many plugins allow you to manage a huge array of advertising on large and branching sites.
The path with plugin installation is still good if the user has not yet gained enough experience and is afraid to get into the depths of the code. If knowledge and experience are sufficient, and “the hand is stuffed”, then working with codes opens up wide opportunities. Let’s start with them – the most difficult ways to insert ads into a WordPress article.
Inserting ad code into an article after a certain number of characters
The first method is suitable for small articles. Open the function.php file. The code installed here automatically works in all articles for which settings are specified; the code will only need to be installed once.
So, the code itself:
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 1000 characters
$enable_length = 1000;
// insert after the first </p> after 500 characters
$after_character = 500;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
$text = '
<!-- YOUR AD CODE IS HERE -->
';
array_splice($after_content, 1, 0, $text);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
add_filter('the_content', 'inject_ad_text_after_n_chars');
Next, we start configuring the parameters we need. We edit the value 1000. If everything is left as is, then the ad block will be shown when the text volume is at least 1000 characters.
Pay attention to the number 500. It indicates the amount of text after which the ad will be shown in the article. In addition, the code provides for displaying ads when a paragraph indentation appears (the </p> tag).
Special code for small articles is relevant because it is not always advisable to show banners here at all, and this should be done carefully so that the ad does not “override” the main content of the article.
Adding ads to each WordPress article after the h2 heading
If you need to insert ads after the <h2> headings, try the second method.
Again, you need to enter function.php and place this code:
function ads_h2($the_content) {
$ads='
<!-- YOUR AD CODE IS HERE -->
';
if (is_single()) {
$the_content=preg_replace('#<h2(.*?)</h2>#','<h2\1</h2><div 8 class="ads_content">'.$ads.'</div>',$the_content);
}
else {
}
return $the_content;
}
12 add_filter('the_content', 'ads_h2');
Here, <h2> can be replaced with <h3>, and the ad will “move” to this level accordingly.
Inserting ads after a paragraph
If you want to broadcast ads after a specific paragraph (in all articles), open single.php.
Here, instead of the line<?php the_content();?> you need to install this:
<?php
$paragraphAfter= 3; //display after the first paragraph
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++ ) {
if ($i == $paragraphAfter) { ?>
<div><!-- YOUR AD CODE IS HERE --></div>
<?php }
echo $content[$i] . "</p>";
} ?>
In this case, the ad will be displayed after the third paragraph (or after the one specified in the $paragraphAfter variable).
Adding ads to any location of a WordPress article
But what if you want the ads to be in a specific individual location in each article in WordPress? After all, the text can smoothly “lead” the reader to a certain banner, pique their interest, and stimulate them to click. Then we choose the following path. We will need function.php again. Copy and paste the following code:
function myads1() {
return '
<!-- YOUR AD CODE IS HERE -->
';
}
add_shortcode('adv1', 'myads1');
And what’s next? It’s simple. When writing an article, you need to be attentive and remember to insert the shortcode into the desired part of the text in HTML: [adv1]
That’s it.
The provided codes are suitable for installing Google Adsense ads.
Good to know: WordPress: How to easy hide posts from specific categories on the homepage?
Displaying ads in WordPress articles using plugins
Plugins can be installed on WordPress to easily place ad blocks within your articles. This method becomes very relevant when your site grows, has a lot of ads, and they are varied. Plugins allow you to quickly and comfortably manage all the banners. Here are a few examples.
New Adman
A very easy-to-use plugin that doesn’t overload your site. It inserts Google Adsense code, for example, into every article at the beginning, middle, or end. You can also use it to add ads to the front page before the first post. You can add ads using shortcodes in the desired location within your WordPress blog article. The plugin is free.
Shortcoder
The lightweight and non-intrusive Shortcoder plugin makes it easy to add ads to your WordPress article using shortcodes. It allows you to insert the necessary codes directly into the article’s body, where you need the ad blocks. After installation and setup, a new button for this plugin appears in the WordPress text editor menu. By clicking on it, you can choose the code for the desired ad and insert it into the text. Done.
There are many other tools for managing ads in WordPress that make it easy to place ads in the right articles and in the necessary parts of the text.
AdRotate
The AdRotate plugin allows you to automate the display of ads, turn off banners at the right time, group them according to certain characteristics, and also provides complete statistics.
Advertisement Management
Advertisement Management allows you to add ads to the block without touching the code, directly from the admin panel. It allows you to choose the location for the ad block and is easy to use.
OIO Publisher
The paid OIO Publisher plugin is an option for those who earn serious money through advertising on their website. The plugin is very powerful, giving webmasters complete control over all advertising.
There are a wide range of options for placing ads in articles on WordPress, and this article does not cover them all.