Utility Schema Markup for WordPress
Functions.php
// Add custom meta boxes for Rating value and Total Ratings
function add_custom_meta_boxes() {
add_meta_box(
'rating_value_meta_box',
'Rating Value',
'rating_value_meta_box_callback',
'post',
'side', // Change 'normal' to 'side' to display on the right side
'high'
);
add_meta_box(
'total_ratings_meta_box',
'Total Ratings',
'total_ratings_meta_box_callback',
'post',
'side', // Change 'normal' to 'side' to display on the right side
'high'
);
}
add_action('add_meta_boxes', 'add_custom_meta_boxes');
// Callback function to display Rating Value meta box
function rating_value_meta_box_callback($post) {
$rating_value = get_post_meta($post->ID, 'rating_value', true);
echo '<label for="rating_value">Rating Value:</label>';
echo '<input type="text" id="rating_value" name="rating_value" value="' . esc_attr($rating_value) . '" size="5" />';
}
// Callback function to display Total Ratings meta box
function total_ratings_meta_box_callback($post) {
$total_ratings = get_post_meta($post->ID, 'total_ratings', true);
echo '<label for="total_ratings">Total Ratings:</label>';
echo '<input type="text" id="total_ratings" name="total_ratings" value="' . esc_attr($total_ratings) . '" size="5" />';
}
// Save custom meta data
function save_custom_meta_data($post_id) {
if (array_key_exists('rating_value', $_POST)) {
update_post_meta(
$post_id,
'rating_value',
sanitize_text_field($_POST['rating_value'])
);
}
if (array_key_exists('total_ratings', $_POST)) {
update_post_meta(
$post_id,
'total_ratings',
sanitize_text_field($_POST['total_ratings'])
);
}
}
add_action('save_post', 'save_custom_meta_data');
Single.php
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "SoftwareApplication",
"name": "<?php echo get_the_title(get_the_ID()); ?>",
"description": "<?php echo get_post_meta(get_the_ID(), 'rank_math_description', true); ?>",
"operatingSystem": "Web",
"applicationCategory": "Utility",
"offers": {
"@type": "Offer",
"price": "0",
"priceCurrency": "USD"
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "<?php echo get_post_meta(get_the_ID(), 'rating_value', TRUE); ?>",
"bestRating": "5",
"reviewCount": "<?php echo get_post_meta(get_the_ID(), 'total_ratings', TRUE); ?>"
},
"author": {
"@type": "Organization",
"name": "Mechanical Site"
},
"image": "<?php $post_thumbnail_url = get_the_post_thumbnail_url(); echo $post_thumbnail_url; ?>",
"url": "<?php $post_url = get_permalink(); echo $post_url; ?>",
"potentialAction": {
"@type": "EntryPoint",
"target": {
"@type": "EntryPoint",
"urlTemplate": "<?php $post_url = get_permalink(); echo $post_url; ?>"
},
"actionPlatform": [
"http://schema.org/DesktopWebPlatform",
"http://schema.org/MobileWebPlatform"
],
"inLanguage": "en"
}
}
</script>