Merhaba aşağıdaki kodda nasıl bir hata olabilir? Kod sqle veri göndermiyor. Bu kodla amacımız yorum bölümüne 3 seçenek eklemek bu 3 seçenekten birini zorunlu tutturmak. Ve en çok seçilen seçeneği istenilen alanda göstermek;
// Yorum Seçenekleri Ekleme
function add_comment_options() {
global $post;
$meta_key = 'comment_options_post_' . $post->ID;
echo '<div class="comment-options"><p class="comment-form-comment">';
echo '<span class="option"><input type="radio" name="comment_options" id="olumlu" value="olumlu" required><label for="olumlu">Güvenli</span>';
echo '<span class="option"><input type="radio" name="comment_options" id="belirsiz" value="belirsiz" required><label for="belirsiz">Belirsiz</span>';
echo '<span class="option"><input type="radio" name="comment_options" id="olumsuz" value="olumsuz" required><label for="olumsuz">Güvensiz</span>';
echo '</p></div>';
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
var submitButton = document.querySelector("#submit");
submitButton.addEventListener("click", function(e) {
if (!document.querySelector('input[name="comment_options"]:checked')) {
alert("Lütfen bir seçenek belirleyin.");
e.preventDefault();
}
});
});
</script>
<?php
}
add_action( 'comment_form_before', 'add_comment_options' );
// Yorum Seçenekleri Kaydetme
function save_comment_options( $comment_id ) {
if ( isset( $_POST['comment_options'] ) ) {
$comment_options = sanitize_text_field($_POST['comment_options']);
add_comment_meta( $comment_id, 'comment_options', $comment_options );
}
}
add_action( 'comment_post', 'save_comment_options' );
// En Çok Seçilen Seçeneği Hesaplama
function get_most_voted_option() {
global $wpdb, $post;
$most_voted_option = $wpdb->get_var( $wpdb->prepare(
"SELECT meta_value FROM $wpdb->commentmeta WHERE meta_key = 'comment_options' AND comment_id IN (SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d) GROUP BY meta_value ORDER BY COUNT(*) DESC LIMIT 1",
$post->ID
) );
return $most_voted_option;
}
// Sonucu Gösterme
function display_most_voted_option() {
$most_voted_option = get_most_voted_option();
if ( $most_voted_option ) {
echo '<div class="most-voted-option">';
switch ($most_voted_option) {
case 'olumlu':
echo '<p class="option">Güvenli</p>';
break;
case 'olumsuz':
echo '<p class="option">Güvensiz</p>';
break;
case 'belirsiz':
echo '<p class="option">Belirsiz</p>';
break;
default:
echo '<p class="option">Hata: En çok seçilen seçenek belirsiz</p>';
break;
}
echo '</div>';
}
}