0

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!

1 Answer 1

0

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' );
14
  • This is great! Forgive my ignorance- Should I include this code in my single-artist.php or in my functions, or in my comments.php?
    – DJZEEGLER
    Commented May 25, 2022 at 15:27
  • After some research, I think I need to create a function, and add these into it. How do I write these actions into a function? Thank you
    – DJZEEGLER
    Commented May 25, 2022 at 15:35
  • @RCOTWUnite Edited my answer and added the code. You might want to conditionally check for the post type of the comment to display that field in your post type only.
    – Abhik
    Commented May 26, 2022 at 6:45
  • If I could upvote again, I would. I know I'm not supposed to ask side questions, but this one's important. I've not seen sanitize_text_field yet, seems like I should use that on ALL of my $_POST where users give input. This protects me from injection attacks? Thanks
    – DJZEEGLER
    Commented May 26, 2022 at 15:33
  • OK and I'm going to try putting this in the comments.php and see what happens. Let me know if this should go in functions instead. Thanks!
    – DJZEEGLER
    Commented May 26, 2022 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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