How to create a classified section in place of comments_template

I am running a site where users can find great information about musical artists and their releases. Each release is a wordpress post. Each artist is a wordpress post. In my single-artist.php and my single-release.php, i use

comments_template();

EDIT: here is my comments.php


comment_form();

if (have_comments()) : 

    $wantto= get_comment_meta( $comment_id, 'wantto', true );
    echo 'ol class=post-comments';
            wp_list_comments(array(
                'style'       = 'ol',
                'short_ping'  = true,
            ));
            echo $wantto;
          
    echo '/ol';
endif;

It just shows a text box. Here's an example of a page with a comment form.

https://staging3.recordcollectorsoftheworldunite.com/artist/021-2/

What I'd like to do is create a new field, a dropdown menu, that tags their post. It will have 3 options:

  • WANT TO BUY
  • WANT TO SELL
  • INFO.

I'd also like to add some fields, depending on which option they choose- if WANT TO BUY, or WANT TO SELL, I'd like to add a price field. If WANT TO SELL, I'd like to add a condition of the item field.

I'd like the value they choose to be able to be queried as well, so I can make a page that queries Latest items for sale, etc.

This way users can interact with each other around the site. If you want to buy Madonna ticket stubs, dropdown the menu to WANT TO BUY and tell us what you're looking for. In the same way, you can do that for selling items, and sharing info about the page.

I'm not married to comments_template(), it's just what I'm using now. Is it best to modify the template for comment form, or use another avenue to make this work?

I'd prefer to do all this without a store/market plugin. They seem overcomplicated for what i'm looking for. However, if that's the best way to go, let me know and I'll look into that.

Thanks all!

Topic comments-template comment-form Wordpress

Category Web


It will be a three step process.

  1. You can use comment_form_logged_in_after (for logged in users) and/or comment_form_after_fields (for non-logged in users) actions to add your custom fields.

  2. Save the values as comment meta using update_comment_meta function hooked into comment_post action.

  3. Get the values using get_comment_meta.

EDIT: 25-05-2022

Try this..

/*
 * This will add the fields to the comment form
*/
function wpse406058_custom_comment_fields() {

    echo '<p class="comment-form-wantto">';
    echo '<label for="wantto">I Want To</label>';
    echo '<select id="wantto" name="wantto" class="myclass">';
        echo '<option value="WANT TO BUY">WANT TO BUY</option>';
        echo '<option value="WANT TO SELL">WANT TO SELL</option>';
        echo '<option value="INFO">INFO</option>';
    echo '</select>';

}
add_action( 'comment_form_logged_in_after', 'wpse406058_custom_comment_fields' );
add_action( 'comment_form_after_fields', 'wpse406058_custom_comment_fields' );

/*
 * This will field value as comment meta
*/
function wpse406058_save_custom_field($comment_id) {

    if ( isset($_POST['wantto']) && !empty($_POST['wantto']) ) {
        $wantto = sanitize_text_field($_POST['wantto']);
        update_comment_meta( $comment_id, 'wantto', $wantto );
    }
    
}
add_action( 'comment_post', 'wpse406058_save_custom_field' );

Now you can get the value using get_comment_meta( $comment_id, 'wantto', true );

One option to display the value would be filtering the comment text.

function wpse406058_display_comment_meta( $comment_text ) {
    
    $wantto = get_comment_meta( get_comment_ID(), 'wantto', true );
    
    if ( isset($wantto) && !empty($wantto) ) {
    
        $wanttotext = '<p class="wantosec">' . esc_html($wantto) . '</p>';
        
        $comment_text = $wanttotext . $comment_text;
    }
    
    return $comment_text;

}
add_filter( 'comment_text', 'wpse406058_display_comment_meta' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.