I am using the following code to place some ad code inside my content .
<?php
$content = apply_filters('the_content', $post->post_content);
$content = explode (' ', $content);
$halfway_mark = ceil(count($content) / 2);
$first_half_content = implode(' ', array_slice($content, 0, $halfway_mark));
$second_half_content = implode(' ', array_slice($content, $halfway_mark));
echo $first_half_content.'...';
echo ' YOUR ADS CODE';
echo $second_half_content;
?>
How can I modify this so that I can place 2 ads out at the same time in between text only paragraphs (both the <p>...</p>
enclosing the ad should not have images or embedded videos).
I want to avoid jQuery.
Example of my Usecase…
- I want to insert 2 advert blocks in this article.
- I want the 1st ad block to be after the 1st paragraph. But any image in the 1st paragraph should be removed.
- For the 2nd ad it should be placed possible in the second half of the article in between text-only paragraph such that an ad code sandwitched between a good amount of text and is never very near to an embedded image or video etc.
- There should be at least 2 paragraphs between 2 ads
Here is my approach to the question. Sorry for posting a bit late ( and missing the bounty 🙁 ), but it has been a hectic week, so I did everything in bits and pieces.
QUICK RUNDOWN
I have not removed the attachments in the first paragraph. I don’t really see why content must be vandalized for the sake of an ad
I have done quite a lot of checks in my code to try and make sure that ads are only inserted in between text only paragraphs. Text only paragraphs are taken to be paragraphs that does not contain img, li and ul tags
I have documented each block of code properly, so you can easily work through each section. I have also added
todo
doc blocks which you need to attend to, if necessaryI have used
wptexturize
to apply p tags tothe_content
. Each double line break constitutes a paragraphModify and use this code as you see fit. I have tested it, so it is bug free on my side.
Here is the code. Hope this works for you as expected. *PS! All of this goes into your functions.php. No need for any other code or mods to your template files
EDIT
From your comments:
You should use the following code to debug
<pre><?php var_dump($NAME_OF_VARIABLE); ?></pre>
. For your first issue, I will use?><pre><?php var_dump($paragraphs); ?></pre><?php
just after this line$paragraphs = explode( $closing_p, wpautop($content) );
to exactly see how the content is split up. This will give you an idea if your content is being split up correctly.Also use
?><pre><?php var_dump($p); ?></pre><?php
just after this line$p = array();
to inspect what value is given to a specific paragraph. Remember, paragraphs with img, li and ul tags should have a0
, also paragraphs with less than 10 words. The rest should have a 1EDIT -> Issue with stripped paragraphs is resolved. Thanks for pointing that flaw out to me. Never realized this. I have updated the code, so just simply copy and paste it
EDIT 2
Please note, you cannot test your code with the online tool you are using. This is not a true reflection of of what is output by
the_content()
. The output that you see using your online tool is filtered and marked up before being send to the screen asthe_content()
. If you inspect your output with a browser like google chrome, you will see that p tags are correctly applied.I have also changed the a tag with image tag in the code
with the sample code you have given me this is the best i can do, there were far too many images in there, you would need a genius to figure out the logic required for your requirements, but try it out it might not be too far off. You’ll need php 5.5 for this.
a couple of points to note:
1. it identifies paragraphs as being wrapped in p elements, not as in visual paragraphs.
2. if p elements exist inside other elements, it will also recognise them as paragraphs. The first ad is a example of this. Avoid using p inside blockquotes, lists, etc, its not necessary, use spans, divs for text instead.
3. I have commented a line calling a function in __construct, uncomment this to insert the 2nd image. This actually works well but your content has a lot of p elements with sentances split over a number of p’s, this is unlikely to be a factor in actual content.
4. It searches for images in para 1 + para 2 and removes them.
then where you want to output your content
This is one solution. It’s not fully programmatic, but I’ve done it before and it will work. Basically, use a “shortcode”. The problem with doing it fully programatically is that there’s no nice way of finding if the flow of an article with inline images, videos, etc. would cause the ad placements to look really bad. Instead, set up your CMS with a shortcode so that editors can place ads in an optimal place in the article.
E.g.
Then, using PHP’s
str_replace
you can simply swap out the ad_block shortcode with your HTML for your ads.E.g.
I did some rework and this is what I finally got.
Feel free to test and give me some feedback.
Usage example:
You can comment the placeSecondAd() out or replace it with your working function.
Here is a smart way:
Here is a complete function:
Ok this is an idea to move you in (possibly) a direction closer to the fully programmatic way you’re asking…
Use a HTML DOM parser like http://simplehtmldom.sourceforge.net/ to parse each article. Using this, you should theoretically be able to pick out all the paragraph tags, and then insert the ad blocks in between the right ones based on your math.
Does that get a bit closer to what you’re trying to do?
Use