Hi,
I'm new here and I don't know whether should I ask my question here or in newbies section, so, let it be here.
I found this code in google search, for inserting ads in the middle of the blog post.
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 2000 characters
$enable_length = 2000;
// insert after the first </p> after 1500 characters
$after_character = 1500;
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 Ads 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');
This code works well with image and text post.
The problem I'm encounter is, when I do a post which contain <table></table>, like comparison chart I commonly use <table> code. The ads will be inserted into the table and make the table very weird, because of a 768x90 ads present inside the table column.
I tried to modified the code to
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 2000 characters
$enable_length = 1800;
// insert after the first </p> after 2200 characters
$after_character = 1550;
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 Ads Here
';
array_splice($after_content, 1, 0, $text);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
}
elseif (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</table>', $after_content);
$text = '
Your Ads Here
';
array_splice($after_content, 1, 0, $text);
$after_content = implode('</table>', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
add_filter('the_content', 'inject_ad_text_after_n_chars');
And this time, the ads works well with blog post contain <table>, but in the images and text post, the ads will present on the bottom of post, so it mixed with the bottom ads.
What I want to do is:
1. In images and text post, it will works as original, insert the ads just after the first </p> after certain character.
2. In post with <table>, it will insert the ads before the table if the character meet the 1. situation, else insert the ads into bottom of the table, but not inside the <table> body.
I'm appreciate your helps.
Thank you.