Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

First, this is what I want to achieve:

I want to add a comments hook on a CPT which is asking users to be registered and logged in to be able to post a comment. And after the user is logged in to display two custom fields before the comment field.

My discussion settings are not set to *Users must be registered and logged in to comment * because on my default blog posts I don't want to force users to register in order to comment.

Here is what I've did till now :

function debate_comment_fields( $fields ) {

 if( is_singular( 'debate' ) ) {

  // these are the custom fields which I want to display before the comment field 
    $fields['first'] = '<p class="comment-form-first"><label for="first">' . 
                     __( 'HTML5' ) . 
                     '</label>' . 
                     '<input id="first" name="category" type="radio" value="'.get_post_meta($post->ID, 'agree', true).'" /></p>';


    $fields['second'] = '<p class="comment-form-second"><label for="second">' . 
                       __( 'FLASH' ) . 
                       '</label>' .
                      '<input id="second" name="category" type="radio" value="'.get_post_meta($post->ID, 'disagree', true).'" /></p>';

    return $fields;


}

else {

         return $fields;
}


} 
add_filter('comment_form_default_fields','debate_comment_fields');  

I've looked through the CODEX, and I found this comment_form_must_log_in_after but this is not helping me because I need first to add a hook which is asking user to log in.

I've looked also into mytheme/comments.php here is a snippet:

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

    <?php if ( have_comments() ) : ?>
        <h3 class="comments-title">

            <?php
                printf( _n('%d comment', '%d comments', get_comments_number(), 'outbox' ),
                    number_format_i18n( get_comments_number() ) );
            ?>
        </h3>

        <ol class="commentlist">
            <?php
                wp_list_comments( array( 'callback' => 'outbox_comment' ) );
            ?>
        </ol><!-- .commentlist -->

        <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through? If so, show navigation ?>
        <nav role="navigation" id="comment-nav-below" class="site-navigation comment-navigation clearfix">
            <div class="nav-previous"><i class="icon-left-open-1"></i>&nbsp;<?php echo get_previous_comments_link( __( 'Older Comments', 'outbox' ) ); ?></div>
            <div class="nav-next"><?php echo get_next_comments_link( __( 'Newer Comments', 'outbox' ) ); ?>&nbsp;<i class="icon-right-open-1"></i></div>
        </nav><!-- #comment-nav-below .site-navigation .comment-navigation -->
        <?php endif; ?>

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

    <?php
        // If comments are closed and there are comments, let's leave a little note, shall we?
        if ( ! comments_open() && '0' != get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
    ?>
        <p class="nocomments"><?php _e( 'Comments are closed.', 'outbox' ); ?></p>
    <?php endif; ?>

    <?php comment_form(); ?>

This is the 'must_log_in' :

        'must_log_in'          => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',

Any idea on how can I achieve this?

Thank you!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

One alternative is simply not to display the comment field if the current user isn't logged in, using is_user_logged_in().

For example, inside your comments template:

<?php comment_form( $args ); ?>

Just wrap that in a conditional:

<?php
// Don't output the comment form if CPT and user isn't logged in
if ( 'debate' != get_post_type() || is_user_logged_in() ) {
    comment_form( $args );
}
?>

Edit

Just put it into the template directly:

<?php
// If CPT and not logged in, display a message:
if ( 'debate' == get_post_type() && ! is_user_logged_in() ) {
    echo '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>';
}
?>
share|improve this answer
    
I'm looking trough the comments-template.php and I can't find anything similar with <?php comment_form( $args ); ?>, I've updated also my code with a snippet from the comments-template. I'm a little bit confused... –  agis Nov 13 '13 at 15:13
    
Well then, how is your Theme outputting the comments form? –  Chip Bennett Nov 13 '13 at 15:16
    
I've edited my question with the snippet containing the output –  agis Nov 13 '13 at 15:34
2  
The very last line of your comments.php: <?php comment_form(); ?> –  Chip Bennett Nov 13 '13 at 15:39
    
this is working, but I would like the to display default message from this 'must_log_in' $args. I've posted the code in my question –  agis Nov 13 '13 at 15:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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