Kategoriler
WordPress

Yorumlarda ekstra bilgi istemek

WordPress yorumlarda standar olarak bulunan isim, email, website ve yorum içeriği kısımlarına ekstra alanlar eklemek veya düzenlemek mümkün.

WordPress sitesinde yorumculardan varsayılan olarak isim, email, web sitesi ve yorum içeriği istenir. Bu alanlarda değişiklik yapmak veya ekstra istenecek bir bilgi için (örneğin -telefon numarası-) bir alan eklenebilir.

Öncelikle eğer kodlarla uğraşmak istemiyorsanız aşağıdaki eklenti işinizi görecektir:

Eğer ben deneyimli kullanıcıyım ve elle kendim yapmak isterim, boşuna sitemde eklenti ağırlığı oluşmasın diyorsanız bu linkte olduğu gibi yorum kısmına ekstra alanlar eklemeniz mümkün.

// Display the phone number field on the front end.
add_filter( 'comment_form_default_fields', 'wpse248957_add_phonenumber_field' );
function wpse248957_add_phonenumber_field( $fields ){
    $fields['phonenumber'] = 
        '<p class="comment-form-phonenumber">' .
            '<label for="phone-number">Phone Number <span class="required">*</span></label>' .
            '<input id="phone-number" name="phone-number" type="tel" ' . 
                'pattern="[\+]\d{2}[\(]\d{3}[\)]\d{3}[\-]\d{4}" ' . 
                'title="Phone Number (Format: +63(123)456-7980)" aria-required="true" required="required"/>' .
        '</p>';
    return $fields;
}
// Add the filter to check whether the comment meta data has been filled
add_filter( 'preprocess_comment', 'wpse248957_verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ){
  if ( ! isset( $_POST['phone-number'] ) ){
        wp_die( __( 'Error: Please enter your phone number. Hit the Back button ' .
            'on your Web browser and resubmit your comment with a phone number.', 'text_domain' )
        );
   }

  return $commentdata;
}

// Save the comment meta data when saving the comment.
add_action( 'comment_post', 'wpse248957_save_comment_meta_data' );
function wpse248957_save_comment_meta_data( $comment_id ){
  if ( ( isset( $_POST['phone-number'] ) ) && ( $_POST['phone-number'] != '') ){
        $phone_number = wp_filter_nohtml_kses( $_POST['phone-number'] );
        add_comment_meta( $comment_id, 'phone-number', $phone_number );
   }
}
// Register meta box on comment edit screen
add_action( 'add_meta_boxes_comment', 'wpse248957_comment_edit_add_meta_box' );
function wpse248957_comment_edit_add_meta_box(){
    add_meta_box( 'title', __( 'Phone Number', 'text-domain' ), 'wpse248957_comment_meta_box', 'comment', 'normal', 'high' );
}

// Callback function for displaying the comment meta box.
function wpse248957_comment_meta_box( $comment ){
    $phone_number = get_comment_meta( $comment->comment_ID, 'phone-number', true );
    wp_nonce_field( 'wpse248957_comment_fields_update', 'wpse248957_comment_fields_update', false );
    ?>
    <p>
        <label for="phone-number"><?php _e( 'Phone', 'text-domain' ); ?></label>
        <input type="text" name="phone-number" value="<?php echo esc_attr( $phone_number ); ?>" class="widefat"/>
    </p><?php
}
// Update comment meta data from comment editing screen 
add_action( 'edit_comment', 'wpse248957_comment_edit_meta_fields' );
function wpse248957_comment_edit_meta_fields( $comment_id ){
    if ( ! isset( $_POST['wpse248957_comment_fields_update'] ) || 
             ! wp_verify_nonce( $_POST['wpse248957_comment_fields_update'], 'wpse248957_comment_fields_update' )
    ){
        return;
   }

  if ( ( isset( $_POST['phone-number'] ) ) && ( $_POST['phone-number'] != '' ) ){
        $phone_number = wp_filter_nohtml_kses( $_POST['phone-number'] );
        update_comment_meta( $comment_id, 'phone-number', $phone_number );
 }else{
        delete_comment_meta( $comment_id, 'phone-number');
 }
}
// Add phone number custom column to comments listing in admin
add_filter( 'manage_edit-comments_columns', 'wpse248957_manage_comment_columns' );
function wpse248957_manage_comment_columns( $columns ){
    return array_merge( $columns, 
        [ 'phone-number' => __( 'Phone Number', 'text_domain' ) ]
    );
}

// Display phone number custom column in comments listing in admin
add_action( 'manage_comments_custom_column', 'wpse248957_manage_comments_columns', 10, 2 );
function wpse248957_manage_comments_columns( $column, $post_id ){
    switch ( $column ){
        case 'phone-number':
            $phone_number = get_comment_meta( $post_id, 'phone-number', true );
            echo esc_html( $phone_number );
            break;
   }
}
$phone_number = get_comment_meta( $comment->comment_ID, 'phone-number', true );
echo esc_html( $phone_number );