4

I have simple fuction that adds additional comment form field called "Subject":

add_action( 'comment_form_logged_in_before', 'additional_fields' );
add_action( 'comment_form_before_fields', 'additional_fields' ); 

function additional_fields () {
    echo '<p class="comment-form-subject">'.
    '<label for="subject">' . __( 'Your subject' ) . '</label>'.
    '<input id="subject" name="subject" type="text" size="30"  tabindex="5" /></p>';
}

It works fine, but just now it adds "Subject" field to every comment form.

I need to add this field only for first level/depth of comments.

My goal is:
When user will reply directly to post, he will get "Subject" field in reply form. When user will reply to comment, he will not see "Subject" field in reply form.

  • Select the correct answer when you see it, so I can award bounty. – MrJustin Dec 30 '13 at 14:55
6
+50

The Reply links

I assume your Reply links look like this:

<a class="comment-reply-link" href="/2013/12/29/hello-world/?replytocom=32#respond" 
 onclick="return addComment.moveForm('comment-32', '32', 'respond', '1')">Reply</a>

and the Cancel Reply link:

<a rel="nofollow" id="cancel-comment-reply-link" 
href="/2013/12/29/hello-world/#respond" style="">Cancel reply</a>

The Javascript method addComment.moveForm is defined in /wp-includes/js/comment-reply.min.js.

Demo plugin:

To hide the extra Subject field for comments replies, you can try to check for the replytocom GET parameter for the non-Javascript case and hook into the click event for the Javascript case.

Here is a demo Comment Subject plugin: /wp-content/plugins/comment-subject/comment-subject.php:

<?php
/**
 * Plugin Name: Comment Subject
 */

/**
 * Add extra Subject comment field when there is no replytocom in the url 
 */
function additional_fields () {

    $url_query = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );

    if( FALSE === stripos( $url_query, 'replytocom' ) )
    { 
        echo '<p class="comment-form-subject">'.
        '<label for="subject">' . __( 'Your subject' ) . '</label>'.
        '<input id="subject" name="subject" type="text" size="30"  tabindex="5" /></p>';
    }
}

add_action( 'comment_form_logged_in_before', 'additional_fields' );
add_action( 'comment_form_before_fields', 'additional_fields' ); 

/**
 * Add jQuery and load the custom "script.js"
 */    
function custom_scripts() 
{
    wp_enqueue_script( 'jquery' );

    wp_enqueue_script(  'my-comment-subject', 
                            plugins_url( 'js/script.js' , __FILE__ ), 
                            array( 'jquery' ), 
                            '1.0.0', 
                            TRUE 
                     );

}

add_action( 'wp_enqueue_scripts', 'custom_scripts' );

where you add the file /wp-content/plugins/comment-subject/js/script.js containing:

jQuery(document).on( 'click', 'a.comment-reply-link', function( event ) {
    jQuery('p.comment-form-subject').hide();
});

jQuery(document).on( 'click', 'a#cancel-comment-reply-link', function( event ) {
    jQuery('p.comment-form-subject').show();
});
  • Due to the original poster, User3136701, not replying in any way or sort. Your accuracy of code, and highest upvote, I have selected your answer as the bounty winner. – MrJustin Jan 2 '14 at 23:50
  • @MrJustin thanks. I hope you can use it and good luck with your project. – birgire Jan 3 '14 at 1:37
  • 1
    Actually it wasn't for me. The user 'USER3136701' is the one who asked the question. I just placed a bounty on it to be nice, hopefully he can use it :) I use a custom Walker for my comments, so I don't actually use a list at all, I use HTML5 articles and sections. – MrJustin Jan 4 '14 at 9:27
  • ok, thanks for sharing. I hope @USER3136701 appreciates your kindly awarded bounty, but I guess we will never know ;-) – birgire Jan 4 '14 at 11:00
2

Let me throw a simple CSS-only solution into the hat:

/* Hide the subject for nested comments (level 2 and below) */
.comment .comment p.comment-form-subject {
    display: none;
}

This solution assumes you are using comment_class.

1

How about using a conditional to check if there are comments? If not, display a different comment form.

$num_comments = get_comments_number( $post_id );
if($num_comments > 0) {
    // Your function to edit comment fields if there ARE comments. Or nothing if the default fields are fine.
}
else
{
//We have comments, so run your function, as above.
add_action( 'comment_form_logged_in_before', 'additional_fields' );
add_action( 'comment_form_before_fields', 'additional_fields' ); 

function additional_fields () {
    echo '<p class="comment-form-subject">'.
    '<label for="subject">' . __( 'Your subject' ) . '</label>'.
    '<input id="subject" name="subject" type="text" size="30"  tabindex="5" /></p>';
}
} //End else
1

Use this snippet inside your single.php, paste it in while loop before comments open function, this will get you solution.

$args = array( 'post_id' => $post->ID,'count' => true );
 $comments = get_comments($args);

 if( $comments == 0  ) :
    add_action( 'comment_form_logged_in_before', 'additional_fields' );
    add_action( 'comment_form_before_fields', 'additional_fields' );
 endif;

 function additional_fields () {
    echo '<p class="comment-form-subject">'.
    '<label for="subject">' . __( 'Your subject' ) . '</label>'.
    '<input id="subject" name="subject" type="text" size="30"  tabindex="5" /></p>';
 }

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.