WordPress.org

Ready to get started?Download WordPress

Forums

custom comment fields inside custom post type (1 post)

  1. dynamyc
    Member
    Posted 10 months ago #

    I have a custom post type and I want to create 2 new fields and remove the default ones (author,url,email) without affecting the default comment fields inside the blog posts.

    Also I want to make it visible just for logged in users, with a message like this for non-logged in users:

    You must be logged in to post a comment

    I've noticed that I can do this from the dashboard setting but the problem is that this will be applied to all the comments and I want to apply this just on a specific CPT.

    So, here is my approach:

    function debate_comment_fields( $fields ) {
    
     if( is_singular( 'debate' ) ) {
    
         $fields['url'] = '';
        $fields['author'] = '';
        $fields['email'] = '';
    
        $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;
    
    }
    }
    add_filter('comment_form_default_fields','debate_comment_fields');

    This code is removing the default fields also on the blog posts and I don't want this.
    After I submit a comment inside my CPT I get this:
    ERROR: please fill the required fields (name, email).

    And also how can I add a message if the user is not logged in like this:

    You must be logged in to post a comment

    just on my CPT.

Reply

You must log in to post.

About this Topic