WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Help with add_filter for wp_filter_comment() (5 posts)

  1. Joe Hoyle
    Human Made
    Posted 6 years ago #

    Hi Guys, I am currently having problems trying to influence the wp_filter_comment() function. I want my plugin to intercept the post comment function to change some values.

    I currently have this in my plugin file:

    function mod_filter_comment($commentdata) {
    $commentdata['comment_content'] = 'HIA';
    return $commentdata;
    }
    
    add_filter('wp_filter_comment', 'mod_filter_comment');

    When a new comment is submitted it is passed through the wp_filter_comment() function, but it seems no matter what I put into mod_filter_comment() it doesn't change the final comment output.

    The wp_filter_comment() is called from wp_new_comment() in wp-includes/comment.php - line 391.

    Any help would be very much appreciated.

    Thanks

  2. Adam Brown
    Member
    Posted 6 years ago #

    Perhaps try this filter instead: comment_save_pre

    http://codex.wordpress.org/Plugin_API/Filter_Reference#Database_Writes_2

  3. dev_n
    Member
    Posted 6 years ago #

    You forgot the global variable.

    function mod_filter_comment($str) {
    global $commentdata;
    
    // do something with the comment_content here ($str)
    
    $commentdata['comment_content'] = $str; // then make it stick...
    }
    
    add_filter('comment_save_pre', 'mod_filter_comment');
  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    ... No, dev_n, that code is not correct in just about any way.

    You might want to read up on WP filters a bit more:
    http://codex.wordpress.org/Plugin_API/Filter_Reference

    His original problem was that there was no filter named "wp_filter_comment". That's a function name, not a filter.

    Also, this thread is 3 months old. I assume he's figured it out by now.

  5. dev_n
    Member
    Posted 6 years ago #

    Yes, my mistake. That's what I get for trying to think too fast. Here's something that should work for him:

    function mod_filter_comment($comment) {
    $comment = 'HIA';
    return $comment;
    }
    add_filter('pre_comment_content', 'mod_filter_comment');

    Also I noticed the age, but I know people look around these dunes a lot looking for answers - so replied regardless...

    --Thanks Otto

Topic Closed

This topic has been closed to new replies.

About this Topic