I guess you are talking about doing this dynamically, without touching the "bodyText" content itself (via TinyMCE)?
An XSLT extension would be the way to go.
Use something like the Html Agility Pack (if you don't mind the overhead of another assembly) to parse the HTML content of the "bodyText" and then insert the AdSense code wherever you want it.
You could also break your content up into say Intro and Main content properties, then when rendering, output the intro, then your adsence code, then your main content?
Nice suggestion by Lee, but I prefer the simpler options myself ie KISS ;)
@ Matt - I don't want it dependable upon the editors, I just wanted them to create their content and not have to think about inserting macros. However the split up approach might work, I have used it before but they just kept pasting the content in the top box :S.. Maybe its just education
@ Lee - Hmmm I was thinking this seems to be the way to do it, just need to figure out the regex to find the second paragraph :S
The first group of the first match will be the end of your second paragraph. Worth noting though that this is heavily dependant on the HTML content. What if your client just puts in a load of <br />s?
With the split approach, you could put a limit of the amount of text you can enter in the first box? (though they may just end up using the second ;)
Lee - you are lucky, 2 karma whores battling it out! ;-) LOL!
How's about this method?
public static string InjectAdSense(string content, int position)
{
var adSenseCode = "<script> ... </script>";
var output = new System.Text.StringBuilder();
var m = Regex.Match(content, "(<p>.*</p>)");
if (m.Success)
{
for (int i = 0; i < m.Groups.Count; i++)
{
if (i == position)
{
output.AppendLine(adSenseCode);
}
output.AppendLine(m.Groups[i].Value);
}
return output.ToString();
}
return content;
}
As Matt says, this heavily depends on your HTML content ... my code is based on it being all <p> tags.
Man you two kill me :) Great work... I just realised I'm going to be stuck in the middle of you two for training, do I need to bring my boxing gloves :P
Inserting Adsense Ad Into bodyText
I need to insert an adsense ad into page of content (bodyText) and only want to insert it after the second paragraph and before the third.
Do you think the best way to do this would be to create a custom XSLT extension? Or is there an onload event I am missing?
Hi Lee,
Why don't you just create a macro and drop it in via the RTE insert macro button?
Matt
Hey Lee,
I guess you are talking about doing this dynamically, without touching the "bodyText" content itself (via TinyMCE)?
An XSLT extension would be the way to go.
Use something like the Html Agility Pack (if you don't mind the overhead of another assembly) to parse the HTML content of the "bodyText" and then insert the AdSense code wherever you want it.
Otherwise try a RegEx to parse the HTML?
Cheers, Lee.
You could also break your content up into say Intro and Main content properties, then when rendering, output the intro, then your adsence code, then your main content?
Nice suggestion by Lee, but I prefer the simpler options myself ie KISS ;)
Matt
@ Matt - I don't want it dependable upon the editors, I just wanted them to create their content and not have to think about inserting macros. However the split up approach might work, I have used it before but they just kept pasting the content in the top box :S.. Maybe its just education
@ Lee - Hmmm I was thinking this seems to be the way to do it, just need to figure out the regex to find the second paragraph :S
Cheers lads..
Something like this could work:
The first group of the first match will be the end of your second paragraph. Worth noting though that this is heavily dependant on the HTML content. What if your client just puts in a load of <br />s?
With the split approach, you could put a limit of the amount of text you can enter in the first box? (though they may just end up using the second ;)
Matt
Lee - you are lucky, 2 karma whores battling it out! ;-) LOL!
How's about this method?
As Matt says, this heavily depends on your HTML content ... my code is based on it being all <p> tags.
Cheers, Lee.
Hmm, just a thought, but as adsence is just JS, couldn't you use JS to inject it?
http://www.impressivewebs.com/inject-custom-ad-blocks-between-paragraphs-in-wordpress/
Matt
Man you two kill me :) Great work... I just realised I'm going to be stuck in the middle of you two for training, do I need to bring my boxing gloves :P
Ca'Ching...Oooooooohhhh and Matt flies in with a huge right hook and takes the bout!
Great find thanks :)
BOOYA! ;)
Nice one Matt. Bookmarked that one too! ;-)
is working on a reply...