Community
Administrator
Staff member
This question has come up a few times, we typically do this using a combination of code snippets and Elementor shortcodes, but obviously you can adapt this to work with other forms of code insert.
So we create sections in elementor section builder and design a nice block, for example maybe a posts block section to show related posts
Then once we have the shortcode for the section we wish to insert, we add the shortcodes to this function:
Which adds these sections to every 4th paragraph.
If you wanted to use code, instead of shortcodes, for example Google adsense. THen you would use something similar to this ( this is untested )
So we create sections in elementor section builder and design a nice block, for example maybe a posts block section to show related posts
Then once we have the shortcode for the section we wish to insert, we add the shortcodes to this function:
PHP:
function insert_random_shortcode_every_fourth_fifteenth_paragraph($content) {
// Check if we are viewing a single post
if (is_singular('post')) {
// Define your shortcodes
$shortcodes = array('[elementor-template id="1562"]', '[elementor-template id="1562"]', '[elementor-template id="1562"]');
// Split the content into an array of paragraphs
$paragraphs = explode("</p>", $content);
$new_content = '';
$paragraph_counter = 0;
// Iterate over the paragraphs
foreach ($paragraphs as $paragraph) {
// Add paragraph to new content
$new_content .= $paragraph;
// Increment the counter
$paragraph_counter++;
// If this is the 4th paragraph or a multiple of 15 after that, add a random shortcode
if ($paragraph_counter == 4 || ($paragraph_counter > 4 && ($paragraph_counter - 4) % 15 == 0)) {
// Select a random shortcode
$random_shortcode = $shortcodes[array_rand($shortcodes)];
$new_content .= $random_shortcode;
}
// Add closing paragraph tag (it was removed in explode function)
$new_content .= '</p>';
}
return $new_content;
}
else {
// If not a single post, return the content unmodified
return $content;
}
}
add_filter('the_content', 'insert_random_shortcode_every_fourth_fifteenth_paragraph'); Which adds these sections to every 4th paragraph.
If you wanted to use code, instead of shortcodes, for example Google adsense. THen you would use something similar to this ( this is untested )
PHP:
function insert_adsense_every_fourth_fifteenth_paragraph($content) {
// Check if we are viewing a single post
if (is_singular('post')) {
// Define your AdSense ad codes
$adsense_codes = array(
'<div class="adsense-ad">YOUR ADSENSE CODE 1 HERE</div>',
'<div class="adsense-ad">YOUR ADSENSE CODE 1 HERE</div>'
);
// Split the content into an array of paragraphs
$paragraphs = explode("</p>", $content);
$new_content = '';
$paragraph_counter = 0;
// Iterate over the paragraphs
foreach ($paragraphs as $paragraph) {
// Add paragraph to new content
$new_content .= $paragraph;
// Increment the counter
$paragraph_counter++;
// If this is the 4th paragraph or a multiple of 15 after that, add a random AdSense code
if ($paragraph_counter == 4 || ($paragraph_counter > 4 && ($paragraph_counter - 4) % 15 == 0)) {
// Select a random AdSense code
$random_adsense_code = $adsense_codes[array_rand($adsense_codes)];
$new_content .= $random_adsense_code;
}
// Add closing paragraph tag (it was removed in explode function)
$new_content .= '</p>';
}
return $new_content;
} else {
// If not a single post, return the content unmodified
return $content;
}
}
add_filter('the_content', 'insert_adsense_every_fourth_fifteenth_paragraph');
