• Need a hand. I’m not a noob, but I’m puzzled by the instructions/tutorials I’ve found for customizing a WP comment form.

    Quite simply, I want to add an input to the comment form. Seems like the following is needed:

    1. Add the input (and label) to the form.
    2. Store the data in the database, requiring a new field.
    3. Retrieve the data for display on posts.
    4. Add the data to the Admin for viewing/editing.

    Could somebody lay out, in simple terms, how this is done? Again: I’ve read EVERYTHING I can find on this, but the info is either outdated or I don’t understand it.

    Thanks for your help!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Again: I’ve read EVERYTHING I can find on this, but the info is either outdated or I don’t understand it.

    In cases like this, it is highly useful for you to link to something you’ve read so that we have a starting point. If you link to something that’s outdated, we can often suggest a line of code or two that will bring it up to date. If something confuses you, tell us where it started to confuse you, so we don’t waste our time and yours going over stuff you already understand.

    Help us to help you. You get better responses that way.

    Thread Starter GeneralHavok

    (@generalhavok)

    Thread Starter GeneralHavok

    (@generalhavok)

    I’m still looking for solutions, but can’t find any. Has ANYONE had success adding to, or changing, the WP comment form?

    Thread Starter GeneralHavok

    (@generalhavok)

    I find it weird that so many use WP, yet I find no useful information about customizing the comment form anywhere. Yes, some info is available…but it’s not very clear. I co-wrote a CMS in PHP, yet I don’t understand the tutorials out there.

    SURELY someone has taken the time to make is clear?

    Moderator bcworkz

    (@bcworkz)

    Sorry I missed your initial posts, I’ve been away. The mods frown on thread bumps, but it may have paid off this time.

    I don’t know if I can make it clear, but I have an added field in my comment form, I can briefly explain what I’ve done. To add the field, hook the filter ‘comment_form_default_fields’. Your filter function is passed an associative array of form field html. Simply add your field’s html to the array and return the entire array. If you’re not happy with the order of the fields, copy the values into a new array in the order you like and return the new array.

    Next you need to get the form POST data into the DB. I used the ‘wp_insert_comment’ action, though other comment hooks probably could work as well. Get your field value from $_POST[‘your_field_name’] and save it somewhere. I used the comment meta table.

    I can’t tell you how to reflect your saved value in the comment output because my field was an operational setting which does not effect comment output. If you can’t figure this part out, let me know and I’ll see if I can suss out an approach.

    For reflecting the setting in the admin panels, I added a column to the comment table. There is a page in the Codex for adding a column to the posts table. All tables are based on the same root class, so the technique translates to any table. First you register your column by hooking the ‘manage_edit-comments_columns’ filter and add your column name as a new key/value in the array your are passed. Then return the array.

    Once registered, to display the actual value in the column, hook the ‘manage_comments_custom_column’ action. Your action function is passed the column name and comment ID. If the column name belongs to you, that is your cue to echo out the column table cell html content for the particular comment.

    How or if you administer this value depends on what it’s purpose is. Some sort of AJAX technique is well suited for this. AJAX in WP is a large tutorial in itself. I will warn you that the usual AJAX techniques need to be modified for WP. If you are not enqueueing your script and hooking an ajax action, you are doing it wrong.

    I fear all this is even more confusing than the info you’ve already reviewed. If you get stuck somewhere and have a specific question, post a code snippet of what you have so far and I’ll try to help, but I can’t write up a full tutorial. This will at least get you pointed in the right direction and provides some key information. Good luck!

    Thread Starter GeneralHavok

    (@generalhavok)

    bcworkz:

    Thank you VERY much for taking the time to help me out. I’ve read your post dozens of times, and have made a little progress. I appreciate it!

    I’ve been able to add fields to the form by hooking the filter. I think I have that process down, now.

    As you said, the next step is saving the data to the database. I feel like a total n00b, but I’m stuck there. Do you know of any examples where people have used wp_insert_comment to do this? I know how to add fields to the database, but am unsure about how WP saves the data.

    Thanks!

    Thread Starter GeneralHavok

    (@generalhavok)

    BTW: because your comment was so thorough, I’ve been able to do a LOT of studying. I didn’t really understand hooks before, so you helped by pointing me in the right direction. You’re awesome.

    Moderator bcworkz

    (@bcworkz)

    I’m pleased my post was helpful. You probably realize now that you will get nowhere in customizing WP if you do not understand hooks.

    I can offer an example of what I did.

    function bcw_handle_comment($id, $comment) {
       //$comment is the entire comment object, often very useful data there
       if (array_key_exists('reply', $_POST)) { //'reply' is checkbox field name
          update_comment_meta( $id, 'bcw_email_reply', 'Yes' );
       }
    }
    add_action('wp_insert_comment', 'bcw_handle_comment', 10, 2);

    A simple checkbox was added to the comment form so commenters could choose to be emailed if anyone replies to their comment. The form only sends field data when the box is checked, so it’s mere existence in $_POST is adequate information about what state to store. The data is really a true/false thing, but I chose to store the actual content displayed in the comments table instead of true/false.

    One can then use get_comment_meta() to retrieve the stored value. By using comment meta, you don’t need to be concerned with how the data gets stored, just how to store and retrieve it. In any case, the data is stored in the commentmeta table along with the key you assigned when you first added the data and the comment ID. You can also store arrays this way, the data is serialized and unserialized behind the scenes, you don’t need to be concerned about it.

    Of course, you can store data anywhere you want, even in a completely different database. I believe in using the provided WP structure unless one has good valid reasons to deviate, thus I use commentmeta.

    Thread Starter GeneralHavok

    (@generalhavok)

    bcworkz:

    What are the 10 and 2 in your example?

    Moderator bcworkz

    (@bcworkz)

    They are semi-optional parameters. The 10 is the priority used by the action handler to decide in which order to call the possibly several callbacks added to any given action. 10 is the default if the parameter is not provided. Lower priority values are executed before higher.

    I only provided a priority so I could pass the 2, the number of parameters I want passed to my callback. If not provided, only the first parameter is passed. One needs to look at the initiating do_actions() call in source code to determine what parameters are available to be passed.

    This sort of information is easily found in the Codex BTW ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Add input to Comment form’ is closed to new replies.