0

I've added a custom field in comment by using

function comment_label($fields){

    unset( $fields['url'] );

    $fields['rate'] = '<p class="comment_form_rate" id="rate_recipe">
                         <span class="star_rating">

    <input class="star star-5" id="star-5" type="radio"  name="rate" value="5"/>
    <label class="star star-5" for="star-5"></label>
    <input class="star star-4" id="star-4" type="radio" name="rate" value="4"/>
    <label class="star star-4" for="star-4"></label>
    <input class="star star-3" id="star-3" type="radio" name="rate" value="3"/>
    <label class="star star-3" for="star-3"></label>
    <input class="star star-2" id="star-2" type="radio" name="rate" value="2"/>
    <label class="star star-2" for="star-2"></label>
    <input class="star star-1" id="star-1" type="radio" name="rate" value="1"/>
    <label class="star star-1" for="star-1"></label>     </span>
                        </p>';

    return $fields;
}

which work as intended if you are going to post a comment with an unlogged status.

But as soon as you login this field goes away, leaving just with the textarea input of comment and the desired title_reply, even if you don't log as admin but as normal user. This is my comments.php file.

<?php

if ( post_password_required() ): ?>
     <p class="nopassword"><?php _e( 'Questo articolo è protetto da password. Inserisci la password per visualizzare i commenti.', 'wtd' ); ?></p>
<?php endif; ?>

<div id="comments" class="comments-area">

    <?php if ( have_comments() ) : ?>
        <h2 class="comments-title">
            <?php
                printf( _nx( 'Un commento su &ldquo;%2$s&rdquo;', '%1$s commenti su &ldquo;%2$s&rdquo;', get_comments_number(), 'comments title', 'wtd' ),
                    number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
            ?>
        </h2>

        <ol class="commentlist">
            <?php wp_list_comments( array( 'callback' => 'wtd_comment', 
                                              'style' => 'p', 
                                               'type' => 'comment',
                                               'per_page' => 8 ) 

                                ); ?>
        </ol><!-- .commentlist -->

        <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // navigazione dei commenti ?>
        <nav id="comment-nav-below" class="navigation" role="navigation">
            <h1 class="assistive-text section-heading"><?php _e( 'Paginazione commenti', 'wtd' ); ?></h1>
            <div class="nav-previous"><?php previous_comments_link( __( '&larr; Commenti Precedenti', 'wtd' ) ); ?></div>
            <div class="nav-next"><?php next_comments_link( __( 'Nuovi commenti &rarr;', 'wtd' ) ); ?></div>
        </nav>
        <?php endif; // check for comment navigation ?>

        <?php
        /* If there are no comments and comments are closed, let's leave a note.
         * But we only want the note on posts and pages that had comments in the first place.
         */
        if ( ! comments_open() && get_comments_number() ) : ?>
        <p class="nocomments"><?php _e( 'I commenti sono chiusi' , 'wtd' ); ?></p>
        <?php endif; ?>

    <?php endif; // have_comments() ?>

    <?php 

    //argomenti da stampare in front-end
        $argz = array(
            'class_form'           => 'form-control',
            'comment_notes_before' => __('il tuo feedback è importante per noi!','wtd'),
            'label_submit'         => __('Commenta','wtd'),
            'comment_fields'       => '',
            'title_reply'          => __('Valuta questa ricetta','wtd'),
        );

    comment_form($argz); ?>

</div><!-- #comments .comments-area -->

Am I missing something?

  • How are you using the comment_label() function? – birgire Dec 21 '15 at 13:16
  • no i'm not using comment_label(),can you provide an example?I'm not finding it on codex. I'm using just $argz = array( 'class_form' => 'form-control', 'comment_notes_before' => __('il tuo feedback è importante per noi!','wtd'), 'label_submit' => __('Commenta','wtd'), 'comment_fields' => '', 'title_reply' => __('Valuta questa ricetta','wtd'), ); comment_form($argz); ?> – Vale Steve Dec 21 '15 at 13:22
  • I'm just quoting your question where you said you successfully used the comment_label() function but now you say you don't use it - I'm confused ;-) – birgire Dec 21 '15 at 13:44
  • @birgire sorry,i've misunderstood.This thing is ma king me crazy and in searching for a solution really bad. – Vale Steve Dec 21 '15 at 13:51
1

I'm not sure I fully understand your question, but to add some HTML (just over or under the submit field) that is visible to both logged-in and logged-out users, you can try the following:

add_filter( 'comment_form_submit_field', function( $submit_field )
{
    //-----------------------------------
    // Adjust the prepend to your needs
    //-----------------------------------
    $prepend = '<p> Prepend some HTML </p>';

    //-----------------------------------
    // Adjust the append to your needs
    //-----------------------------------
    $append = '<p> Append some HTML </p>';

    return $prepend . $submit_field . $append;
} );

Other workarounds are possible, like the comment_form action.

There are some comment-form filters that are only available to logged-out users, like the

  • comment_form_before_fields
  • comment_form_field_{$name}
  • comment_form_after_fields

So most likely you're trying to hook into those filters - but you haven't mentioned what filters you're using.

  • 'comment_form_default_fields' it's the filter that i'm using,the strange thing that make me suppose that not depends by html showing is that work without being logged,looks more as you said wrong hook or some other is_logged() thing. – Vale Steve Dec 21 '15 at 14:43
  • The comment-fields loop only displays the comment text-area for logged-in users, so that's why your setup will not display the extra fields for logged-in users. Then we need to use different hooks as workaround. @ValeSteve – birgire Dec 21 '15 at 15:22
  • So i've to use some of the hooks you mentioned ,Or you have some other hints for a work around – Vale Steve Dec 21 '15 at 15:33
  • yes use this workaround, but other hooks are possible too, it just depends on the desired outcome. It's even possible to rearrange the fields, with this kind of approach. @ValeSteve – birgire Dec 21 '15 at 15:50
0

I Have managed it this way,using an action hook as you said because my filter is for non logged users only.

function comment_custom(){

    ?><p class="comment_form_rate" id="rate_recipe"><label>Vota la ricetta</label><br/>
                     <span class="star_rating">

<input class="star star-5" id="star-5" type="radio"  name="rate" value="5"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="rate" value="4"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="rate" value="3"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="rate" value="2"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="rate" value="1"/>
<label class="star star-1" for="star-1"></label>     </span>
                    </p>

}

    //Those filter makes my ""custom comment field"" both logged and unlogged  
    add_action('comment_form_after_fields','comment_custom');
    add_action('comment_form_logged_in_after','comment_custom');
0

You have probably done

$defaults['comment_notes_before'] = __( '' );  
$defaults['submit_button'] = __( '' );  
$defaults['comment_field'] = __( '' );  
$defaults['title_reply'] = __( 'Leave your Reply' );  
$defaults['label_submit'] = __( 'Submit Comment', 'custom' );  

For custom comment form

Now what happens is Wordpress will display default comment form for logged in users which you have initialized to blank thus nothing is display.

Solution Is that We need to change $default parameters of comment form since wordpress will display default form with only comment fields. We can do that by using condition

if (!is_user_logged_in()) {  
//custom form  
}  
else{  
//make changes in default comment form  
}  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.