• Resolved netzgestaltung

    (@netzgestaltung)


    Hi there

    i do have the purpose to set the $star_pos “cmstr_pos” different on different views

    i looked at /wp-post-comment-rating/inc/function.php on line 138 to the function “wpcr_comment_tut_add_title_to_text” which is injected by the filter “comment_text”

    idealicy i would suggest to add a call to apply_filters at the end of the function like so:

    
    ///// show rating stars with visitors comment /////
    add_filter('comment_text','wpcr_comment_tut_add_title_to_text',99,2);
    function wpcr_comment_tut_add_title_to_text($text,$comment){
      // ... your stuff
      // instead of "return $text;" at the end:
      return apply_filters('wpcr_comment_tut_add_title_to_text', $text, $comment);
    }

    in my case it will be called in this way:

    
    echo apply_filters('comment_text', $comment->comment_content, $comment, $args);
    

    and

    
    add_filter('wpcr_comment_tut_add_title_to_text', 'myTheme_comment_rating_comment_text', 10, 2);
    function myTheme_comment_rating_comment_text($text, $comment){
      // my own logic goes here
      return $text;
    }
    

    for now i add it into the plugin file, but i wish it would be included in an update ??

    maybe this is also a suggestion for other functions

    • This topic was modified 4 years, 8 months ago by netzgestaltung.
    • This topic was modified 4 years, 8 months ago by netzgestaltung.
    • This topic was modified 4 years, 8 months ago by netzgestaltung.
    • This topic was modified 4 years, 8 months ago by netzgestaltung. Reason: a preview would be a very nice function!
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter netzgestaltung

    (@netzgestaltung)

    Here is the result of my themes function, changing the $star_pos depending on a page id or on the setting

    
    function myTheme_comment_rating_comment_text($text, $comment){
      $profile_post_ID = 1234; // on profile page or descendants
      $wpcr_options = get_option('wpcr_settings'); // no need for $wpdb
      $enable_rating = apply_filters('wpcr_options_checkbox1', $wpcr_options['checkbox1'], $comment); // "Enable rating" possible overwrite on per post_type basis
      $rating_text = ''; // added to $comment_text
      $star_pos = 'above'; // default stars position 
    
      if ( $enable_rating ) {
        $rating = get_comment_meta($comment->comment_ID, 'rating', true);
    
        if ( $rating ) {
          $post = get_post();
          $parents = get_post_ancestors($post);
          $parents[] = $post->ID;
          $rating_inner_text = '(' . $rating . '/5)';
          $rating_text = '<div class="cmstr-out"><span class="wpcr_author_stars" data-rating="' . $rating . '" ></span></div><span class="tval">' . $rating_inner_text . '</span>';
          $comment_text = get_comment_text($comment);
    
          if ( !in_array($profile_post_ID, $parents) && isset($wpcr_options['cmstr_pos']) && $wpcr_options['cmstr_pos'] === 0 ) {
            $star_pos = 'below';
          }
    
          if ( $star_pos === 'above' ) {
            $text = $rating_text . $comment_text;
          } else {
            $text = $comment_text . $rating_text;
          }
        }
      }
      return $text;
      // return apply_filters('myTheme_comment_rating_comment_text', $text, $comment);
    }
    

    kind regards
    tom

    Plugin Author shoaib88

    (@shoaib88)

    Hi,
    Could you provide me a complete code? I will update this code snippet in next version.

    What about $profile_post_ID = 1234;?

    Thanks

    Thread Starter netzgestaltung

    (@netzgestaltung)

    hi there,

    the previous one was the filter with my template logic – its different on the profile page.

    this is the function.php full code for function “wpcr_comment_tut_add_title_to_text” i would suggest:

    
    function wpcr_comment_tut_add_title_to_text($text, $comment){
      $wpcr_options = get_option('wpcr_settings'); // no need for $wpdb
      $enable_rating = apply_filters('wpcr_options_checkbox1', $wpcr_options['checkbox1'], $comment); // "Enable rating" possible overwrite on per post_type basis
      $rating_text = ''; // added to $comment_text
      $star_pos = 'above'; // default stars position
    
      if ( $enable_rating ) { // "Enable rating"
        $rating = get_comment_meta($comment->comment_ID, 'rating', true);
    
        if ( $rating ) {
          $post = get_post();
          $parents = get_post_ancestors($post);
          $parents[] = $post->ID;
          $rating_inner_text = '(' . $rating . '/5)';
          $rating_text = '<div class="cmstr-out"><span class="wpcr_author_stars" data-rating="' . $rating . '" ></span></div><span class="tval">' . $rating_inner_text . '</span>';
          $comment_text = get_comment_text($comment);
    
          if ( isset($wpcr_options['cmstr_pos']) && $wpcr_options['cmstr_pos'] === 0 ) {
            $star_pos = 'below';
          }
    
          if ( $star_pos === 'above' ) {
            $text = $rating_text . $comment_text;
          } else {
            $text = $comment_text . $rating_text;
          }
        }
      }
      return apply_filters('wpcr_comment_tut_add_title_to_text', $text, $comment);
    }
    

    if you had a github repository i would send you pull requests

    Plugin Author shoaib88

    (@shoaib88)

    Hi,
    I’ll update this in next version. It will really helpful for me.

    Thanks

    Thread Starter netzgestaltung

    (@netzgestaltung)

    Hi there,

    i think i need to test the code a bit more to get it working in all circumstances.

    i’ll update you here with a new post then.

    Thread Starter netzgestaltung

    (@netzgestaltung)

    The problem was that when using get_comment_text the usual comment_text filter was not used at all and some filters where missing:

    
    add_filter( 'comment_text', 'wptexturize'            );
    add_filter( 'comment_text', 'convert_chars'          );
    add_filter( 'comment_text', 'make_clickable',      9 );
    add_filter( 'comment_text', 'force_balance_tags', 25 ); 
    add_filter( 'comment_text', 'convert_smilies',    20 );
    add_filter( 'comment_text', 'wpautop',            30 );
    

    Reference: https://stackoverflow.com/a/2609197/1165121

    also using comment_text() did not work because its then filtered infinite.
    removing the rating filter temporary did also not work in my themes funcion.

    This is what now works for me:

    i combined all values regarding to rating in an array an use it as parameter for the filter apply_filters('wpcr_comment_tut_add_title_to_text', $comment_text, $rating, $comment).

    /wp-post-comment-rating/inc/function.php line 136

    
    ///// show rating stars with visitors comment /////
    add_filter('comment_text','wpcr_comment_tut_add_title_to_text',99,2);
    function wpcr_comment_tut_add_title_to_text($comment_text, $comment){
      $wpcr_options = get_option('wpcr_settings'); // no need for $wpdb
      $rating = array( // all rating values combined for better filtering afterwards
        'enable' => apply_filters('wpcr_options_checkbox1', $wpcr_options['checkbox1'], $comment), // "Enable rating" possible overwrite on per post_type basis
        'text' => '', // added to $comment_text
        'star_pos' => 'above', // default stars position
        'filtered_comment_content' => $comment_text, // Was allready filtered
      );
    
      if ( $rating['enable'] ) { // "Enable rating"
        $rating['rating'] = get_comment_meta($comment->comment_ID, 'rating', true);
    
        if ( $rating['rating'] ) {
          $rating_inner_text = '(' . $rating['rating'] . '/5)';
          $rating['text'] = '<div class="cmstr-out"><span class="wpcr_author_stars" data-rating="' . $rating['rating'] . '" ></span></div><span class="tval">' . $rating_inner_text . '</span>';
    
          if ( isset($wpcr_options['cmstr_pos']) && $wpcr_options['cmstr_pos'] === 0 ) {
            $rating['star_pos'] = 'below';
          }
    
          if ( $rating['star_pos'] === 'above' ) {
            $comment_text = $rating['text'] . $rating['filtered_comment_content'];
          } else {
            $comment_text = $rating['filtered_comment_content'] . $rating['text'];
          }
        }
      }
      return apply_filters('wpcr_comment_tut_add_title_to_text', $comment_text, $rating, $comment);
    }
    

    This is now how my theme works (with getting all the rating values, its much smaller):

    
    add_filter('wpcr_comment_tut_add_title_to_text', 'myTheme_comment_rating_comment_text', 10, 3);
    
    function sandbox_comment_rating_comment_text($comment_text, $rating, $comment){
      $profile_post_ID = 1234; // on profile page or descendants
      $star_pos = isset($rating['star_pos']) ? $rating['star_pos'] : 'above'; // default stars position
      $enable_rating = isset($rating['enable']) ? $rating['enable'] : false;
    
      if ( $enable_rating ) { // "Enable rating"
        if ( isset($rating['rating']) ) {
          $post = get_post();
          $parents = get_post_ancestors($post);
          $parents[] = $post->ID;
    
          if ( in_array($profile_post_ID, $parents) ) {
            $star_pos = 'above';
          }
    
          if ( $star_pos === 'above' ) {
            $comment_text = $rating['text'] . $rating['filtered_comment_content'];
          } else {
            $comment_text = $rating['filtered_comment_content'] . $rating['text'];
          }
        }
      }
      return $comment_text;
    }
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘add filter to functions’ is closed to new replies.