php – Closing Paragraph Tag Missing in Wordoress

Question

The closing P tag

is stripped out of my blog posts whenever I use this code in my Functions.php file to insert an after a number of paragraphs. It leaves the

unclosed. What could be wrong?


// Article Ads After X Paragraphs
/////////////////////////////////////
$mvp_post_inad = get_option('mvp_post_inad');
if (!empty($mvp_post_inad)) {
function mvp_post_ad_insert( $text ) {
if ( is_single() ) {
$mvp_post_inad = get_option('mvp_post_inad');
if ($mvp_post_inad) {
$ads_text="<div class="mvp-post-ad-wrap"><span class="mvp-ad-label">" . esc_html__( 'Advertisement', 'zox-news' ) . '</span><div class="mvp-post-ad">'.do_shortcode(html_entity_decode($mvp_post_inad)).'</div></div>';
}
$split_by = "</p>";
$mvp_post_freq = get_option('mvp_post_freq');
$insert_after = $mvp_post_freq; //number of paragraphs

// make array of paragraphs
$paragraphs = explode( $split_by, wptexturize($text));
if ( count( $paragraphs ) > $insert_after ) {
$new_text="";
$i = 1;
foreach( $paragraphs as $paragraph ) {
if( preg_match( '~<(?:img|blockquote|ul|li)[ >]~', $paragraph )) {
$new_text .= $paragraph;
} else {
$new_text .= $paragraph . ( $i % $insert_after == 0 ? $ads_text : '' );
$i++;
}
}
return $new_text;
}
}
return $text;
}
add_filter('the_content', 'mvp_post_ad_insert');
}

0
Komolafe Olumide 1 month 2022-05-28T16:53:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse