April 19, 2024
You can use WP menu builder to build menus

One of the best ways of incorporating ads is within the post content itself. This is due to the least amount of invasiveness this method of advertising. There aren’t any pop-ups or videos included, you only have to scroll down if you don’t like the ad. Especially in the realm of beginners, there are always debates centered around whether you should manually insert code or not.

Regardless of the prevalent stance, there is always code inserting included, but this shouldn’t be the center of your ad insertion. It can be complex and time-consuming, especially if you’re a beginner. Thus, we’ve dissected the best and easiest way you can add ads to your post content, in an attempt to earn money and advertise in a non-invasive way.

The keys

You won’t need to have an extensive amount of coding knowledge, because the emphasis is on functionality. In order to be able to have a wide array of ads, you will need a plugin. Thankfully, WordPress themselves have created a plugin, called Post Adverts. It’s the premier solution for creating in-text ads for your readers.

The plugin itself is free and there are many useful tips included with it. Moreover, if a company wishes to advertise on your post, they will most likely provide the necessary code, so there is no need to worry. Here’s how you can accomplish the insertion of the ad.

How to do it

When you’ve installed Post Adverts, a new menu of the same name will appear. Go Post Adverts >> Add New. There, you will be asked to enter the name of the ad, the code and the location where you want to put it. Afterward, you only have to click publish and you’re completely done!

If you dislike working with too many plugins and want to practice coding, then simply open your theme’s functions.php or a site-specific plugin and copy-paste the code provided:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<?php

 

//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 );

}

Smridhi Malhotra

No Comments

Leave a Comment