WordPress.org

Ready to get started?Download WordPress

Forums

RSS: Replace Specific Text in Content with Variable? (9 posts)

  1. zigx
    Member
    Posted 7 years ago #

    Hello,

    I'm trying to figure out how i can place a variable in the content of my posts that is replaces on the fly when accessed via the RSS feed.

    So for example, if the post body were:

    "I am #####'s post body."

    Then via an RSS url such as site.com/feed/bobby/ the content of the post body would print out like:

    "I am bobby's post body."

    (##### is replaced with the value "bobby")

    Any input would be appreciated. thank you.

  2. zigx
    Member
    Posted 7 years ago #

    if anyone has ANY related info on this i would love to read it. i have searched the forums but couldn't find anything on point with what im looking to do.

    thank you.

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

    What is "bobby" in this case? Replacing text is easy, the real question is what are you replacing it with? Unless you know where to get "bobby", there's no way to tell you how to do it.

  4. zigx
    Member
    Posted 7 years ago #

    otto thank u for responding!

    basically "bobby" is a string that the feed will use to replace a "place holder" string within the post's content.

    So it could look like site.com/feed/?name=whatever OR site.com/feed/whatever/

    And when someone uses the feed, content will be generated and on the fly anywhere within the content "#####" is found, it is replaced with "whatever" .

  5. mutube
    Member
    Posted 7 years ago #

    zigx: I'm not sure how much PHP/etc. you understand but the two things you're looking for are $_GET[''] vars and the str_replace() function.

    Basically, before the feed is sent (find this in the source), include the line:

    $rss = str_replace("%name%", $_GET['name'], $rss);

    Where $rss is the variable currently holding the rss feed data. Anywhere that %name% appears it will be replaced with the ?name=xxxx content on the request URL.

    More information on str_replace()

    Hope that helps.

  6. zigx
    Member
    Posted 7 years ago #

    mutube that is exactly what i was thinking needed to be done... BUT, :) , line 45 of wp-rss2.php says:

    <content:encoded><![CDATA[<?php the_content() ?>]]></content:encoded>

    "the_content()" is a function... how do i apply the replace on a function like that???

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

    WordPress has the concept of "filters". A filter works like this:

    function replace_name_tag_rss($content)
    {
    if (is_feed())
    {
    $content = str_replace("%name%", $_GET['name'], $content);
    }
    return $content;
    }
    add_filter('the_content','replace_name_tag_rss');
  8. zigx
    Member
    Posted 7 years ago #

    wow man thank you so much.

    I will read up on filters and try to figure out where i actually need to store that function.

    I really appreciate you taking the time to actually code that up.

  9. juda
    Member
    Posted 7 years ago #

    did a plug in or solution come out of this? i need a way to replace text with a file or include another post.

Topic Closed

This topic has been closed to new replies.

About this Topic