Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lee 1130 posts 3088 karma points
    Nov 04, 2010 @ 09:17
    Lee
    0

    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?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 04, 2010 @ 09:27
    Matt Brailsford
    0

    Hi Lee,

    Why don't you just create a macro and drop it in via the RTE insert macro button?

    Matt

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 04, 2010 @ 09:41
    Lee Kelleher
    1

    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.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 04, 2010 @ 09:45
    Matt Brailsford
    1

    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

  • Lee 1130 posts 3088 karma points
    Nov 04, 2010 @ 10:20
    Lee
    0

    @ 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.. 

     

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 04, 2010 @ 10:42
    Matt Brailsford
    0

    Something like this could work:

    </p>.*?(</p>)

    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 Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 04, 2010 @ 10:45
    Lee Kelleher
    1

    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.

    Cheers, Lee.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 04, 2010 @ 10:50
    Matt Brailsford
    1

    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

  • Lee 1130 posts 3088 karma points
    Nov 04, 2010 @ 10:51
    Lee
    1

    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

  • Lee 1130 posts 3088 karma points
    Nov 04, 2010 @ 10:54
    Lee
    0

    Ca'Ching...Oooooooohhhh and Matt flies in with a huge right hook and takes the bout!  

    Great find thanks :)

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Nov 04, 2010 @ 10:56
    Matt Brailsford
    0

    BOOYA! ;)

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 04, 2010 @ 10:57
    Lee Kelleher
    0

    Nice one Matt.  Bookmarked that one too! ;-)

Please Sign in or register to post replies

Write your reply to:

Draft