Why are two functions over-riding each other?

I have function in my plugin that is:

add_filter('comment_text', 'commentimage_comment_text');
function commentimage_comment_text($comment="") { 
  $options = get_option('commentimage');
  $id = get_comment_ID();

  $images = $options['images'];
  if (!isset($images) || !is_numeric($images)) $images = 1;
    $url = get_option('siteurl');
    for ($i=0; $i<$images; $i++) {
      if (file_exists(ABSPATH . 'wp-content/comment-image/' . $id . ($i==0?'':('-'.$i)) . '-tn.jpg')) {
         $comment .= '<p><a href="' . $url . '/wp-content/comment-image/' . commentimage_find_original($id, $i) . '"><img src="' . $url . '/wp-content/comment-image/' .    $id . ($i==0?'':('-'.$i)) . '-tn.jpg"/></a></p>';
      }
    }
  return $comment;
}

Now it is showing a successful image uploaded by my viewers. I want my viewers to have the option of rating them, so for that I write in my plugin just below the above line the following code:

 add_filter('comment_text', 'commentimage_comment_text2');
 function commentimage_comment_text2()
 {
   $rtt= "<br>Rating";
   return $rtt;
 }

Then my pictures are gone and it shows text rating – I will change the text rating to my desired needs once I get the answer on how to overcome over writing.

1 Answer
1

It looks like you are overwriting the comment text with your commentimage_comment_text2 filter, try this to append the ratings text:

add_filter( 'comment_text', 'commentimage_comment_text2' );
function commentimage_comment_text2( $comment ){
    $rtt = "<br>Rating";

    return $comment.$rtt;
}

ps: you forgot the $comment input parameter.

Here is a poor man’s skematic picture of the filter flow in WordPress 😉

poor man's skematic image of the filter flow in WordPress

The filter icon was taken from here.

In your case the filter’s name is comment_text and your two callbacks are commentimage_comment_text and commentimage_comment_text2 with priority 10 (the default).

You can view all the callbacks for this filter with this code snippet:

add_action('wp_footer',function(){
        global $wp_filter;
        printf('<pre>%s</pre>',print_r( $wp_filter['comment_text'],true));
});

On my install I get the following output (in my theme’s footer part)
of all these callbacks with the corresponding assigned pirorities.

I added your two callbacks commentimage_comment_text and commentimage_comment_text2 so you can see them too:

Array
(
    [9] => Array
        (
            [make_clickable] => Array
                (
                    [function] => make_clickable
                    [accepted_args] => 1
                )
        )
    [10] => Array
        (
            [wptexturize] => Array
                (
                    [function] => wptexturize
                    [accepted_args] => 1
                )
            [convert_chars] => Array
                (
                    [function] => convert_chars
                    [accepted_args] => 1
                )
            [commentimage_comment_text] => Array
                (
                    [function] => commentimage_comment_text
                    [accepted_args] => 1
                )
            [commentimage_comment_text2] => Array
                (
                    [function] => commentimage_comment_text2
                    [accepted_args] => 1
                )
        )
    [20] => Array
        (
            [convert_smilies] => Array
                (
                    [function] => convert_smilies
                    [accepted_args] => 1
                )
        )
    [25] => Array
        (
            [force_balance_tags] => Array
                (
                    [function] => force_balance_tags
                    [accepted_args] => 1
                )
        )
    [30] => Array
        (
            [wpautop] => Array
                (
                    [function] => wpautop
                    [accepted_args] => 1
                )
        )
    [31] => Array
        (
            [capital_P_dangit] => Array
                (
                    [function] => capital_P_dangit
                    [accepted_args] => 1
                )
        )
)

Leave a Comment