Comment Rating plugins not working with custom comment form template WordPress

HippySunshine

Senior Member
I have inherited a WP site that now requires a star rating to be added to user comments. I have found a plugin that I like ‘Star Rating’, simple and does what it says, and easy to override styles.
This works perfectly with the default comment_form() from WordPress, but does not work on the custom comment form template that is in the theme. I’ve tried multiple plugins and neither work due to this custom comment form.

Can anyone point out what is missing and why it wont show?

I've asked for help over on the WP fprum but no such luck :(
Here is the comments.php that the theme is using:


PHP:
<?php
// Do not access this file directly
if ( !defined('ABSPATH') ) exit;

// If passworded, must have given password
if ( post_password_required() ) return;

// If comments are closed and there are no comments already, do not show comments at all
if ( !comments_open() && !get_comments_number() ) return;
?>

<div class="comments">
    <h5>Comments (<?php comments_number( '0', '1', '%' ); ?>)</h5>
    <div class="comments-section">
        <?php if ( 'open' == $post->comment_status ) : ?>

            <?php if ( get_option( 'comment_registration' ) && !$user_ID ) : ?>

                <p class="unstyled">You must <a href="<?php echo get_option( 'siteurl' ); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>">log in</a> to post a comment.</p>

            <?php else : ?>

                 <form action="<?php echo get_option( 'siteurl' ); ?>/wp-comments-post.php" method="post" id="comment_form">
                    <?php if ( $user_ID ) { ?>
                        <p class="unstyled">Logged in as <a href="<?php echo get_option( 'siteurl' ); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>.
                            <a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="<?php _e( 'Log out of this account' ) ?>">Logout &raquo;</a></p>
                    <?php } ?>
                    <?php if ( !$user_ID ) { ?>
                        <input class="u-full-width" type="text" name="author" id="author" value="<?php echo $comment_author; ?>" tabindex="1" placeholder="NAME" />
                        <input class="u-full-width" type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" tabindex="2" placeholder="EMAIL" />
                        <input class="u-full-width" type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" tabindex="3" placeholder="WEBSITE" />
                    <?php } ?>
                    <textarea class="u-full-width main" name="comment" id="comment" rows="8" tabindex="4" placeholder=""></textarea>
                    <?php comment_id_fields(); ?><input name="submit" type="submit" id="submit" tabindex="5" value="Post Comment..." />
                    <?php do_action( 'comment_form', $post->ID ); ?>
                </form>

            <?php endif; // If registration required and not logged in ?>

        <?php endif; ?>
    </div>
    <?php if ( have_comments() ) { ?>
        <ul class="comment-block" id="comment-block">
            <?php
            // Callback defined in /_includes/functions/theme-functions.php
            wp_list_comments( 'callback=aa_list_comments' );
            ?>
        </ul>
    <?php } ?>
</div>

Thank you
 
It won't show because the plugin probably hooks into comment_form(). You need to look at the plugin code and look for the hook. You can then edit this to point to your own function.

I'll download the plugin later and have a look for you
 
It won't show because the plugin probably hooks into comment_form(). You need to look at the plugin code and look for the hook. You can then edit this to point to your own function.

I'll download the plugin later and have a look for you

I tried to see where the hook was and how it was working, but I was frustrated at that point and failed :(
 
This is what I can see in the plugin file:


PHP:
        /**
         * Initialize hooks.
         *
         * @since 1.0.0
         */
        public function init_hooks() {

            add_action( 'comment_form_logged_in_before', array( $this, 'comment_form_fields' ) );
            add_action( 'comment_form_top', array( $this, 'comment_form_fields' ) );
            add_filter( 'preprocess_comment', array( $this, 'verify_comment_rating' ) );
            add_action( 'comment_post', array( $this, 'save_comment_rating' ) );
            add_filter( 'comment_text', array( $this, 'modify_comment' ) );
            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_plugin_files' ) );

            $avg_rating_display = get_option( ' avg_rating_display', 'show' );

            if ( 'show' == $avg_rating_display ) {
                add_filter( "comments_template", array( $this, 'rating_average' ) );
            }

        }

        /**
         * Add fields after default fields above the comment box, always visible
         */
        public function comment_form_fields() {

            if ( ! self::status() ) {
                return;
            }

            $require_rating = get_option( 'require_rating', 'no' );
            ?>
            <div class="stars-comment-rating">
                <div class="rating-box">
                    <select id="rate-it" class="require-<?php echo esc_attr( $require_rating ); ?>" name="rating">
                        <?php
                        $selected_for = 5;
                        for ( $i = 1; $i <= 5; $i ++ ) {
                            $selected = ( $i == $selected_for ) ? "selected" : "";
                            echo '<option value="' . $i . '" ' . $selected . '>' . $i . '</option>';
                        }
                        ?>
                    </select>
                </div>
            </div>
            <?php
        }
 
This might be more helpful... does this mean anything to you?

PHP:
/**
         * Initialize hooks.
         *
         * @since 1.0.0
         */
        public function init_hooks() {

            add_action( 'comment_form_logged_in_before', array( $this, 'comment_form_fields' ) );
            add_action( 'comment_form_top', array( $this, 'comment_form_fields' ) );
            add_filter( 'preprocess_comment', array( $this, 'verify_comment_rating' ) );
            add_action( 'comment_post', array( $this, 'save_comment_rating' ) );
            add_filter( 'comment_text', array( $this, 'modify_comment' ) );
            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_plugin_files' ) );

            $avg_rating_display  = get_option( ' avg_rating_display', 'show' );
            $google_search_stars = get_option( ' google_search_stars', 'show' );

            if ( 'show' == $avg_rating_display ) {
                add_filter( "comments_template", array( $this, 'rating_average_markup' ) );
            }
            if ( 'show' == $google_search_stars ) {
                add_action( 'wp_head', array( $this, 'add_reviews_schema' ) );
            }
        }

        public function add_reviews_schema() {

            if ( ! self::status() ) {
                return;
            }

            $schema_name  = ucfirst( get_post_type() );
            $schema_title = get_the_title();
            $rating_stat  = $this->rating_stat();
            $review_type  = get_option( 'google_search_stars_type' );

            if ( ! empty( $review_type ) ) {
                $schema_name = esc_attr( $review_type );
            }

            echo '<script type="application/ld+json">
{
  "@context": "https://schema.org/",
  "@type": "' . $schema_name . '",
  "name": "' . $schema_title . '",
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "' . $rating_stat['avg'] . '",
    "bestRating": "5",
    "ratingCount": "' . $rating_stat['count'] . '"
  }
}</script>';
        }
 
There are ways to add ratings to custom comment forms - but you need to add the code yourself.

Extract the relevant parts from this and add the to to custom comment form:

 
Back
Top