show-ads-between-images

If you’re running a web novel site using the Madara theme, you may want to auto insert ads between paragraphs in Text Chapter to optimize monetization. Instead of manually adding ads or relying on third-party plugins, you can use a simple code snippet to automate the process. This method ensures that your ads blend naturally into the reading experience, improving engagement without disrupting the flow of the story.

Why Auto Insert Ads in Text Chapters?

Automatically placing ads within web novel chapters offers several benefits:

Better Visibility – Ads placed between paragraphs are more likely to be seen by readers than sidebar ads.
Higher Click-Through Rates (CTR) – Readers engaged in the story may be more inclined to interact with ads.
Non-Intrusive Monetization – Ads blend seamlessly into the content, avoiding pop-ups or distractions.
No Plugin Required – A lightweight solution that keeps your website fast and optimized.

How to Auto Insert Ads Between Paragraphs in Text Chapter

To insert ads automatically in text-based chapters, add the following code snippet to your child theme’s functions.php file:

php
add_filter('the_content', 'madara_auto_insert_ads');

function madara_auto_insert_ads($content){
$manga_id = get_the_ID();
$chapter_slug = get_query_var('chapter');
$chapter_type = get_post_meta($manga_id, '_wp_manga_chapter_type', true);

// Apply only to text-based chapters
if ($chapter_type == 'text') {
$tag = 'p';
preg_match_all('{<' . $tag . '[^>]*>(.*?)</' . $tag . '>}', $content, $matches, PREG_PATTERN_ORDER);

if (count($matches[0]) > 0) {
// Choose where to insert the ad (after the 3rd paragraph)
$paragraph_index = 2;
$ads_content = '<p><img src="https://yoursite.com/banner-ads.jpg" alt="Ad Banner"/></p>';

if (isset($matches[0][$paragraph_index])) {
$content = str_replace_first($matches[0][$paragraph_index], $matches[0][$paragraph_index] . $ads_content, $content);
}
}
}
return $content;
}

// Replace only the first occurrence
function str_replace_first($from, $to, $content) {
$from = '/' . preg_quote($from, '/') . '/';
return preg_replace($from, $to, $content, 1);
}

How to Customize Ad Placement

This code provides flexibility in ad placement:

  • Change the Ad Type – Modify $ads_content to display different types of ads, such as text, image banners, or even JavaScript-based ads.
  • Control the Position – Adjust $paragraph_index to determine after which paragraph the ad appears.
  • Target Specific Chapters – Show different ads for specific manga or novel chapters:
php
if ($manga_id == 123 && $chapter_slug == 'chapter-1') {
$ads_content = '<p>Exclusive Ad for Chapter 1</p>';
}

Best Practices for Inserting Ads in Web Novels

To maximize ad performance without disrupting the reading experience:

Avoid Excessive Ads – Too many ads can frustrate readers and lead to higher bounce rates.
Use Relevant Ads – If possible, display ads related to the novel’s genre or audience interests.
Test Ad Placement – Experiment with different paragraph positions to find what works best.
Monitor Performance – Track ad engagement and adjust placement based on analytics.

Final Thoughts

By using this method, you can auto insert ads between paragraphs in Text Chapter without relying on plugins. This solution ensures seamless ad integration, enhances monetization, and maintains a smooth reading experience for users.

For more Madara theme customization guides, check out our Web Novel Monetization Tips or learn more about Ad Placement Strategies.

SHARE THIS POST

1 Comment

  1. Darkcry

    This not work ? give a error

    Your PHP code changes were rolled back due to an error on line 38 of file wp-content/themes/madara-child/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘{‘, expecting ‘)’

    and this is the line 38
    preg_match_all(‘{]*>(.*?)}’, $content, $matches, PREG_PATTERN_ORDER);

Leave a Reply

Your email address will not be published. Required fields are marked *

*

3 × one =