Here's the situation:

I have a custom field named {article} that contains and outputs formatted articles. I would like to be able to modify that {article} variable before it outputs in the template. To be specific, I would like to insert an Adsense ad after the first paragraph of each article.

Is this possible in any way shape or form?

Any feedback and/or help is appreciated...

  • It might help to post an explicit example of what {article} outputs and where you'd like your ad to go. Answers could be more specific. – Matt Stein Jan 19 '13 at 15:53

Hop Studios Hop Inject was created primarily for that purpose. It can be used for other times when you need to inject things within content. I haven't used it, our clients don't run ads. But have heard good things about, including it's ease of use.

For example, with Low Replace, you'd have to insert code/text that would be replaced.

  • 1
    Thanks, I just bought it and implemented it, works great, does exactly what I need – Jan Paul Jan 20 '13 at 11:32
  • That's great! Glad it worked. Don't forget to accept an answer, so that a solution is noted. – nonprofit_tech Jan 20 '13 at 12:11

One way you could do this is to use Low Replace, using a regular expression to insert the ad after the first closing </p> tag, or a comment added to the markup <!-- ad-here -->. You can do this right from the template without any need to get into PHP, assuming there's a consistent way to identify the insertion point.

Edit: You also have the option of enabling PHP for the template (probably on output) and doing the same thing without using an add-on.


My answer was terrible! But I'm not going to delete it. Might help someone inserting without scripts.

You can use jQuery with [.insertBefore()] .insertAfter():

<div class="article">

$('AD CODE HERE').insertAfter('.article p:first-child');

Change single quotes to double quotes in your AD CODE or change single quotes in my JS example to double quotes and have all your AD CODE use single quotes.

  • +1 except that the question was to put the ad 'after' the first pargraph :-) – GDmac Jan 20 '13 at 8:15
  • 1
    I tried this already (I used insertAfter()), but it doesn't work... To quote: "All of jQuery's insertion methods use a domManip function internally to clean/process elements before and after they are inserted into the DOM. One of the things the domManip function does is pull out any script elements about to be inserted and run them through an "evalScript routine" rather than inject them with the rest of the DOM fragment. It inserts the scripts separately, evaluates them, and then removes them from the DOM." – Jan Paul Jan 20 '13 at 11:36
  • Very good to know. Didn't think to check docs for script removal. Hop Inject is the way to go for sure – Anna_MediaGirl Jan 20 '13 at 17:23

For future folks looking for the same thing. String Divide does this exact thing easily and it's free.

  • Thanks for posting! It has an very useful option that I'm missing in Hop Inject, which is "insert after x characters". I'm now inserting ads "after x number of <p>", but since paragraph length can very a lot, the text between ads is sometimes too little or too much. Inserting after character count would solve this. Butt... String Divide seems to be missing the ability to repeat/loop inserts, to insert after every 500 characters, so once at 500, once at 1000 and so on. Or am I missing something? – Jan Paul Apr 30 '13 at 7:40
  • Hi Jan, you're not missing anything. String Divide, at this point, does not repeat/loop as you're needing. I'll take this as a feature request though. :) – Stephen Callender Apr 30 '13 at 10:22
  • Ok, I will modify that feature request a bit then since my description of it was not really right. What I meant to say was.. what I need is to be able to insert multiple html snippets in the same EE variable at different places. So once after 100 words, once after 400, and so on.. And, those snippets inserted would be different (not repeats). In addition to taking into account character count they should also insert only after a paragraph. So something like this condition: "Insert after the first paragraph --> that occurs after 500 words (or characters)" – Jan Paul May 2 '13 at 1:59
  • @JanPaul We've recently updated String Divide. It doesn't have exactly what you were needing, but it definitely has a lot more going on, including looping and two insert options. – Stephen Callender Jul 16 '13 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.