WordPress website मे लगाए rating option बिना plugins के ।
आप अपने वॉर्डप्रेस वेबसाईट मे यूजर का rating देने वाले ऑप्शन को लगा सकते है । आपकी वेबसाईट मे कोई भी User एक बार से ज्यादा रेटिंग नही दे सकता है । rating option से आपके website को rank होने की आशंका थोड़ी बढ़ सकती है ।
कैसे करें
सबसे पहले आप अपनी wrodpress वेबसाईट के functions.php मे नीचे दिए हुए कोड जो paste कर दें ।
add_action('wp_ajax_save_post_rating', 'save_post_rating');
add_action('wp_ajax_nopriv_save_post_rating', 'save_post_rating');
function save_post_rating() {
if ( empty($_POST['post_id']) || empty($_POST['rating']) ) {
wp_send_json_error('Invalid request');
}
$post_id = intval($_POST['post_id']);
$rating = intval($_POST['rating']);
if ($rating < 1 || $rating > 10) {
wp_send_json_error('Invalid rating');
}
// ek user ek hi baar
if (isset($_COOKIE['rated_' . $post_id])) {
wp_send_json_error('already_voted');
}
$ratings = get_post_meta($post_id, '_ratings', true);
if (!is_array($ratings)) {
$ratings = [];
}
$ratings[] = $rating;
update_post_meta($post_id, '_ratings', $ratings);
setcookie('rated_' . $post_id, 1, time() + YEAR_IN_SECONDS, '/');
$avg = round(array_sum($ratings) / count($ratings), 1);
$count = count($ratings);
wp_send_json_success([
'avg' => $avg,
'count' => $count
]);
}
// SHORTCODE
function post_rating_shortcode() {
global $post;
$ratings = get_post_meta($post->ID, '_ratings', true);
if (!is_array($ratings)) $ratings = [];
$count = count($ratings);
$avg = $count ? round(array_sum($ratings) / $count, 1) : 0;
ob_start(); ?>
<div class="post-rating"
data-post="<?php echo esc_attr($post->ID); ?>"
data-avg="<?php echo esc_attr($avg); ?>">
<div class="stars">
<?php for ($i = 1; $i <= 10; $i++) : ?>
<span class="star" data-value="<?php echo $i; ?>">★</span>
<?php endfor; ?>
</div>
<div class="rating-info">
⭐ <?php echo esc_html($avg); ?>/10 (<?php echo esc_html($count); ?> votes)
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode('post_rating', 'post_rating_shortcode');
function add_rating_schema() {
if (!is_single()) return;
global $post;
$ratings = get_post_meta($post->ID, '_ratings', true);
if (!is_array($ratings) || count($ratings) < 1) return;
$count = count($ratings);
$avg = round(array_sum($ratings) / $count, 1);
?>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "MusicRecording",
"name": "<?php echo esc_js(get_the_title()); ?>",
"url": "<?php echo esc_url(get_permalink()); ?>",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "<?php echo $avg; ?>",
"bestRating": "10",
"worstRating": "1",
"ratingCount": "<?php echo $count; ?>"
}
}
</script>
<?php
}
add_action('wp_head', 'add_rating_schema');
अब footer.php मे नीचे दिए हुए कोड को पेस्ट कर दें ।
<script>
document.addEventListener('DOMContentLoaded', function () {
const ajaxUrl = '/wp-admin/admin-ajax.php';
document.querySelectorAll('.post-rating').forEach(box => {
const stars = Array.from(box.querySelectorAll('.star'));
const info = box.querySelector('.rating-info');
let avg = Math.round(parseFloat(box.dataset.avg));
// page load par fill
stars.forEach(star => {
if (parseInt(star.dataset.value) <= avg) {
star.classList.add('filled');
}
});
stars.forEach((star, index) => {
// LEFT → RIGHT hover
star.addEventListener('mouseenter', () => {
stars.forEach((s, i) => {
s.classList.toggle('hovered', i <= index);
});
});
star.addEventListener('mouseleave', () => {
stars.forEach(s => s.classList.remove('hovered'));
});
// CLICK
star.addEventListener('click', () => {
const rating = star.dataset.value;
const postId = box.dataset.post;
fetch(ajaxUrl, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: `action=save_post_rating&post_id=${postId}&rating=${rating}`
})
.then(res => res.json())
.then(data => {
if (!data.success) {
if (data.data === 'already_voted') {
alert('❌ Aap pehle hi vote de chuke ho');
} else {
alert('⚠️ Kuch error aaya, dobara try karein');
}
return;
}
// SUCCESS
alert('✅ Thanks! Aapka vote save ho gaya');
// update UI
info.textContent =
`⭐ ${data.data.avg}/10 (${data.data.count} votes)`;
stars.forEach(s => {
if (parseInt(s.dataset.value) <= Math.round(data.data.avg)) {
s.classList.add('filled');
} else {
s.classList.remove('filled');
}
s.classList.remove('hovered');
s.style.pointerEvents = 'none';
});
});
});
});
});
});
</script>
<style>
.post-rating {
margin: 15px 0;
}
.star {
font-size: 24px;
cursor: pointer;
color: #ccc;
margin-right: 2px;
}
.star.filled,
.star.hovered {
color: #f5b301;
}
.rating-info {
margin-top: 6px;
font-size: 14px;
color: #555;
}
</style>
अब आपको rating वाले ऑप्शन को जहा भी दिखाना हो वह पे shortcode को पेस्ट कर दें ।
इस तरह से आप अपनी वेबसाईट मे rating देने वाले ऑप्शन को लगा सकते है और बिना किसी plugins के ऐसा आप कर रकते हैं ।
