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.

I'm creating a plugin that will add a dropdown list box as an additional field on the comments page. The data in the dropdown is not being added to the MySQL database. Can someone take a look at my code to see what the problem might be? Here is the code from my extendcomment.php plugin:

<?php

// Add custom meta  fields to the default comment form
// Default comment form includes name, email address and website URL
// Default comment form elements are hidden when user is logged in

add_filter('comment_form_default_fields', 'custom_fields');
function custom_fields($fields) {

$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );

$fields[ 'author' ] = '<p class="comment-form-author">'.
    '<label for="author">' . __( 'Name' ) . '</label>'.
    ( $req ? '<span class="required">*</span>' : '' ).
    '<input id="author" name="author" type="text" value="'. esc_attr(                   $commenter['comment_author'] ) .
    '" size="30" tabindex="1"' . $aria_req . ' /></p>';

$fields[ 'email' ] = '<p class="comment-form-email">'.
    '<label for="email">' . __( 'Email' ) . '</label>'.
    ( $req ? '<span class="required">*</span>' : '' ).
    '<input id="email" name="email" type="text" value="'. esc_attr( $commenter['comment_author_email'] ) .
    '" size="30"  tabindex="2"' . $aria_req . ' /></p>';

$fields[ 'industry' ] = '<p class="comment-form-industry">'.
    '<label for="industry">' . __( 'Industry' ) . '</label>'.
    ( $req ? '<span class="required">*</span>' : '').
    '<input id="industry" name="industry" type="text" size="30"  tabindex="3"'. $aria_req .' /></p>';

$fields[ 'url' ] = '<p class="comment-form-url">'.
    '<label for="url">' . __( 'Website' ) . '</label>'.
    '<input id="url" name="url" type="text" value="'. esc_attr( $commenter['comment_author_url'] ) .
    '" size="30"  tabindex="4" /></p>';



return $fields;
}

// Add fields after default fields above the comment box, always visible

add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );

function additional_fields () {
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
echo '<p class="comment-form-industry">'.
    '<label for="industry">' . __( 'Industry' ) . '</label>' .
    '<select name = "industry" id="industry">' .
    '<option value="Commodities/Basic Materials">Commodities/Basic Materials</option>' .
    '<option value="Conglomerates">Conglomerates</option>'.
    '<option value="Consumer Goods">Consumer Goods</option>'.
    '<option value="Healthcare">Healthcare</option>'.
    '<option value="Industrial Goods">Industrial Goods</option>'.
    '<option value="Services">Services</option>'.
    '<option value="Technology">Technology</option>'.
    '<option value="Utilities">Utilities</option>'.
    '</select>'.
    '</p>';

add_action( 'comment_post', 'save_comment_meta_data');
function save_comment_meta_data( $comment_id ) {
    if ( ( isset( $_POST['industry'] ) ) && ( $_POST['industry'] != '') )
        $industry = wp_filter_nohtml_kses($_POST['industry']);
    add_comment_meta( $comment_id, 'industry', $industry );


}
// Add the filter to check if the comment meta data has been filled or not

add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ){
    if( !isset( $_POST['industry'] ) )
        wp_die( _( 'Error: You did not add your industry. Hit the back button and   resubmit your comment with your industry.'));

}
// Add an edit option to comment editing screen

add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
function extend_comment_add_meta_box() {
    add_meta_box( 'industry', __( 'Comment Metadata - Extend Comment' ),   'extend_comment_meta_box', 'comment', 'normal', 'high' );
}

function extend_comment_meta_box ( $comment ) {
    $industry = get_comment_meta( $comment->comment_ID, 'industry', true );
    $title = get_comment_meta( $comment->comment_ID, 'title', true);
    ?>
    <p>
        <label for="industry"><?php _e( 'Industry' ); ?></label>
        <input type="text" name="industry" value="<?php echo esc_attr( $industry );  ?>" class="widefat" />
    </p>
    <p>
        <label for="title"><?php _e( 'Comment Title' ); ?></label>
        <input type="text" name="title" value="<?php echo esc_attr( $title ); ?>"  class="widefat" />
    </p>

<?php
}

// Update comment meta data from comment edit screen

add_action( 'edit_comment', 'extend_comment_edit_metafields' );
function extend_comment_edit_metafields( $comment_id ) {
    if( ! isset( $_POST['industry'] ) || ! wp_verify_nonce( $_POST['industry'], 'industry' ) ) return;

    if ( ( isset( $_POST['industry'] ) ) && ( $_POST['industry'] != '') ) :
        $industry = wp_filter_nohtml_kses($_POST['industry']);
        update_comment_meta( $comment_id, 'industry', $industry );
    else :
        delete_comment_meta( $comment_id, 'industry');
    endif;

    if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') ):
        $title = wp_filter_nohtml_kses($_POST['title']);
        update_comment_meta( $comment_id, 'title', $title );
    else :
        delete_comment_meta( $comment_id, 'title');
    endif;


}



}
share|improve this question

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.