ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Adding text before a certain <h3> tag in every entry?

August 17, 2010 6:42pm

Subscribe [2]
  • #1 / Aug 17, 2010 6:42pm

    Ruairidh

    6 posts

    I have a global variable, let’s call it {adsense}. I have a lot of articles/entries and would like to put that {adsense} tag in every one. The only problem is I would like to place the tag before the first subheading of each article. Each subheading is wrapped around

    </h3> tags. Example:
    This is the first paragraph
    This is the second paragraph
    {adsense}
    <h3>This is the first subheading</h3>
    <p>This is another paragraph</p><h3>This is another heading</h3>

    I found Hop Studio’s Hop Injection plugin here: http://www.hopstudios.com/software/hop_inject/
    This does what I am looking for but it doesn’t add the tag into the entry itself. If I were to remove the plugin then it would remove the tag from every entry too.

    It doesn’t edit or “write” to the entry.

    How would I go about doing that? I was thinking Find and Replace but I’m not sure what kind of query I would use..

  • #2 / Aug 18, 2010 6:40pm

    Green Egg Media

    111 posts

    If you add it to the entry itself then you can’t use the global var, since you can’t call the variable inside of the entry. This means that the actual contents of the global var would need to be injected into each entry. If you needed to change the contents of the global var down the road, it won’t auto-update in existing entries, meaning you would need to manually modify every single previously existing entry with the new content.

    In reality, you need some type of Add-On to accomplish what you’re trying to do without creating an unnecessary amount of manual labor. If Hop Injection does what you need it to do, I suggest you use that.

    Lance
    Green Egg Media

  • #3 / Aug 18, 2010 6:54pm

    edit: ignore

  • #4 / Aug 18, 2010 7:02pm

    Ruairidh

    6 posts

    Thanks for your input Lance. That makes sense but I probably should have been more specific with the global variable bit. Basically all we need in each entry is text that says {adsense} but we will use LG Replace to change that specific text into what we want to display.

    In the articles template, it basically says if an entry has the text “{adsense}” inside it, then replace it with this adsense code. So it’s not a global variable so to speak but more of a “trigger”. If we want to make a global change, we would do that in the template which contains the LG Replace info.

    Long story short, I should have said we need to enter a string of text into each and every entry, before the first <h3>.

  • #5 / Aug 18, 2010 8:45pm

    Green Egg Media

    111 posts

    It almost sounds like you would benefit from a custom field type that would do this for you, but again, that would be an add-on, so if that field type were removed so would the added content. Since that’s the case, rather than write a new field type that gives you the same problem you would have with Hop Injection, you would be better off just using the existing plugin.

    Another option would be to write an extension that could intercept all entries submitted in the CP and actually insert the text block into your content before writing to the DB. As far as I know, nothing like this exists right now, and even then, it would only work on new or modified entries, not entries just hanging out in the DB. Unless the extension, on install, could write to the DB.

    A third, and maybe easier option, would be to write a PHP loop that just grabs the relevant fields from the database and does a preg_replace() on the <h3> tag. That would work for all existing entries, but doesn’t really help for future entries, though you can just add the text block manually for future entries.

    CAUTION: Running PHP against the EE database can have serious consequences if something goes wrong. BACKUP THE DB before you do anything to it!

  • #6 / Aug 25, 2010 4:20pm

    Ruairidh

    6 posts

    A third, and maybe easier option, would be to write a PHP loop that just grabs the relevant fields from the database and does a preg_replace() on the <h3> tag. That would work for all existing entries, but doesn’t really help for future entries, though you can just add the text block manually for future entries.

    CAUTION: Running PHP against the EE database can have serious consequences if something goes wrong. BACKUP THE DB before you do anything to it!

    Hi there, thanks for the detailed reply. I think your suggestion above is eggzactly (sorry) what I’m looking for. We just need to do a mass replace on all the current entries and after that we will be adding text blocks manually.

    If I understand correctly, I need to use the preg_replace() function in my template, specify the correct weblog and custom field, save the template and then that should do it? Afterwards I could just remove the script from the template and carry on as normal?

    Now I just need to figure out the correct syntax for replacing only the first h3!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases