WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to change text string to another string? (10 posts)

  1. marcinpl87
    Member
    Posted 5 years ago #

    Hello
    This is my plugin code:

    function my( $content ) {
    $contenttt = str_replace("abc", "AAAAAAAAA", $content);
    return $contenttt.'BBBBBBBBB';
    }
    add_filter('the_content', 'my');

    But this script only add 'BBBBBBBBB' to my post.
    What I must to write in this plugin when I will replace string "abc" in all posts??

  2. Roger Theriault
    Member
    Posted 5 years ago #

    Make sure "abc" is in the_content? Technically the code looks OK to me.

  3. marcinpl87
    Member
    Posted 5 years ago #

    I’m sure.
    Now I write in plugin this code:

    function my( $content ) {
    echo 'BBBBBB';
    }
    add_filter('the_content', 'my');

    But when I activate plugin and write post: 'text text text text text', I see 'text text text text textBBBBBB'
    What I must to write in this plugin when I will see only 'BBBBBB' ?

  4. Roger Theriault
    Member
    Posted 5 years ago #

    Don't echo. It's a filter. Stuff goes in the top and comes out (modified, or the same) the bottom. No side effects (well, you can do them but it's ill-advised). End with a return statement.

    Replace echo with return "foo"; and you should only see 'foo'

  5. Glenn Ansley
    Member
    Posted 5 years ago #

    This is my plugin code:

    function my( $content ) {
    $contenttt = str_replace("abc", "AAAAAAAAA", $content);
    return $contenttt.'BBBBBBBBB';
    }
    add_filter('the_content', 'my');

    But this script only add 'BBBBBBBBB' to my post.
    What I must to write in this plugin when I will replace string "abc" in all posts??

    I don't know if this was your problem, but you defined the variable $contenttt and then returned $content . 'BBBBBBBBB'.

    In this case, $content would have been returned empty.

  6. marcinpl87
    Member
    Posted 5 years ago #

    ok. now I have:

    function my( $content ) {
    return "foo";
    }
    add_filter('the_content', 'my');

    But this does not solve the problem :/

    Look here: http://www.uniaeuropejska.net/
    There is (in posts) something like:
    text text text
    link
    "foo"
    But i will only "foo"

    please help.

  7. Roger Theriault
    Member
    Posted 5 years ago #

    I'm not quite sure what you're trying to do... so please explain. And if you can, post "the loop" from the template that is generating that page.

    Your filter above should replace the entire contents of the_content with the word "foo" anytime the_content() is used. It won't affect anything else. and if your template is taking the data from the database without using the WordPress template tags, you'll need to change the template.

  8. marcinpl87
    Member
    Posted 5 years ago #

    I want to replace the content of the post from database with another text string.

    In this string i will change something in orginal content of the post from database.

    Unfortunately, str_replace() doesn't work, echo "foo" and return "foo" as well.

  9. Roger Theriault
    Member
    Posted 5 years ago #

    If you're using the filter you defined, and that's where the "foo" comes from, then something else other than the_content is generating the rest of the text. Perhaps a later filter?

    You'll need to post a bit more info than that.

  10. marcinpl87
    Member
    Posted 5 years ago #

    You were right
    Another plugin generate the_content and when I repair this it's working.

    thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic