Skip to:
Content
Pages
Categories
Search
Top
Bottom

Replace word in Post

  • @ruilouis

    Member

    Hello all,

    I tried to realize a plugin that would allow me to replace some words by others in the post. I created the DB contained the words to replace and the word to put instead, i created the function and all is working fine, BUT, I don’t find how to use it on BBPRESS.

    My function is something like

    echo replace_words($TextToBeTransform,$WordToReplace)

    , and the function use the DB to see what is be put instead of the $WordToReplace. As I can see, the post are printed out from the post.php of the template we do use.

    I got this:

    <?php

    $tset="A trial phrase.";

    $WordToBeReplaced="trial";

    ?>

    <div class="threadpost">

    <div class="post">

    <?php echo replace_words( $test,$WordToBeReplaced); post_text()?></div>

    <div class="poststuff">

    <?php printf( __('Posted %s ago'), bb_get_post_time() ); ?>

    ">#

    <?php post_ip_link(); ?> <?php post_edit_link(); ?>

    <?php post_delete_link(); ?></div>

    </div>

    It print me exactly as I want the “A trial phrase” replace trial by “new” (from the DB).

    The problem is that when I want to apply this to the text of the post, post_text(), it won’t work at all. I see that this post_text() is actually applying filters and others to the text, which my cause my function to fail (no error, it just don’t replace the words).

    Could someone tell me where should I apply my own filter replace_words() for it to be taken into account?

    Thank you very much.

Viewing 3 replies - 1 through 3 (of 3 total)
  • @nightgunner5

    Member

    I’m guessing get_post_text() would be your best option. post_text() doesn’t return a value, it just pastes the text onto the page.

    @livibetter

    Member

    You also need http://tw2.php.net/str_replace

    I think it’s better to write a plugin (using a filter) for this task, instead of modifying template. (ya… this would be a good example to be “How to write a plugin” on my podcast show)

    @livibetter

    Member

    I just finished uploading talkPress 002: A Simple bbPress Plugin – Bad Words Filter. You can watch this here.

    The code mentioned in video is:

    <?php
    /*
    Plugin Name: Bad Words Filter
    Plugin URI: http://www.livibetter.com/
    Description: Removing bad words from post content
    Author: Yu-Jie Lin
    Author URI: http://www.livibetter.com/
    Version: 0.1
    */

    function BadWordsFilter($post_text) {
    // $post_text = str_ireplace('bad', '*****', $post_text);

    $bad_words = array('bad', 'topic', 'cialis');
    foreach($bad_words as $bad)
    // $post_text = str_ireplace($bad, '*****', $post_text);
    $post_text = preg_replace("/\b$bad\b/i", '*****', $post_text);

    return $post_text;
    }

    add_filter('post_text', 'BadWordsFilter');
    ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Skip to toolbar