WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Replacing a string within the_content() with another? (10 posts)

  1. mistergin
    Member
    Posted 7 years ago #

    Hello!

    To insert ads in particular locations on my articles, I place a particular string within the location of the article where I want the ad to appear.

    For example, I may place the string ###replaceme### somewhere inside of an article.

    What I want to do is use the php replace tag (str_replace?) to replace that string, with a string of my ads.

    Because the_content() is a function call, I can't seem to replace it effectively though. Any tips?

    Also, the block that is replacing the string may look something like:

    [code]
    <div>
    Title For Something<br>
    Body about that something, etc.
    </div>
    [/code]

    Is there any way to wrap that code into a variable? Cold Fusion allows you to do it with CFSAVECONTENT, but in php, I wasn't aware of a way to do it. Worst case scenario, I could force it all on a single line, but being able to save the variable and keep the formatting is always nice :)

    Thanks!

  2. Chris_K
    Member
    Posted 7 years ago #

    Why not just build a simple plugin?

    Writing_a_Plugin

  3. TimoK
    Member
    Posted 7 years ago #

    You want to create a filter and hook it in at the "the_content" hook. See http://codex.wordpress.org/Plugin_API#Create_a_Filter_Function and http://codex.wordpress.org/Plugin_API/Hooks

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    function replace_content($content)
    {
    $content = str_replace('###replaceme###', 'something_else',$content);
    return $content;
    }
    add_filter('the_content','replace_content');

    Stick that in a plugin or in your theme's functions.php file and voila.

    Instead of using str_replace, you can also use preg_replace or any sort of PHP code you like to modify the $content string.

  5. davidchait
    Member
    Posted 7 years ago #

    Alternate, you could add an extra processor function to my CG-Inlines plugin. It uses inline-functions of the form:

    <!--handlername:parameter-->

    ... or something like that. So would be easy to add a function like 'showad', with the parameter being a string-based index into an array of advertising html snippets, or of a jpg to show, whatever.

    The advantage of people adding more inline functions to CG-Inline is that there's one fewer filter, str_replace, and/or regex to run. ;)

    -d

  6. mistergin
    Member
    Posted 7 years ago #

    You guys are awesome - thank you. :)

  7. juda
    Member
    Posted 7 years ago #

    did any replace plugin come out of this? i need something similar. i need a way to include a post in other posts.

  8. johnstonphilip
    Member
    Posted 7 years ago #

    I am trying to do this same thing - replace a string in 'the_content'

    I am using it in the RSS file but I want the RSS to output all the HTML (including tags and stuff). So the way I am using it is like this:

    <![CDATA[<?php the_content('', 0, '') ?>]]>

    What I want to do is take that output and store it in a variable and do a string replace after all the tags.

    (I am pulling the content into Flash - but wordpress adds a space after the tags which translate as a full line break in Flash - so I want to replace the space with nothing so it's gone)

    Has anyone come up with a way to store the output of 'the_content' in a variable yet?

  9. Jenst
    Member
    Posted 6 years ago #

    Hi there! I've been irritated over this problem for some time now and now come up with a fix. It's presented on my homepage:
    http://www.jenst.se/2007/08/02/wordpress-more-tag-fix-works-with-pages If you come up with a better way of doing it, please let me know.

    /JT

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.