WordPress की थीम मे Custom Meta Description Box कैसे लगाए

आप अपने वेबसाईट का meta description दिखाना चाहते हैं अपनी वेबसाईट मे तो आप दिए हुए कोड का उपयोग करके अपनी वेबसाईट मे meta description को जोड़ सकते है।

क्या करें और क्या है meta description

  • meta description के लिए आपको सबसे पहले एक बॉक्स चाहिए जिसमे आप अपने कुछ चुनिंदा वाक्य लिख सकते है ।
  • आप meta description मे जो वाक्य लिखेंगे वो सर्च इंजन के रिजल्ट मे दिखाया जाएगा ।
  • meta description SEO के हिसाब से बहुत जरूरी होता है ।

कैसे करें 

आप अपनी WordPress के कस्टम थीम मे या कोई भी थीम मे नीचे दिए कोड को functions.php मे पेस्ट कर दें ।

// Add meta box
function custom_meta_box() {
add_meta_box(
'custom-meta-box',
__('Meta Description', 'textdomain'),
'custom_meta_box_callback',
'post', // Change this to 'page' if you want the meta box on pages
'side',
'high'
);
}
add_action('add_meta_boxes', 'custom_meta_box');

// Meta box callback function
function custom_meta_box_callback($post) {
$meta_description = get_post_meta($post->ID, '_custom_meta_description', true);
?>
<p>
<label for="custom-meta-description"><?php _e('Meta Description:', 'textdomain'); ?></label>
<br>
<textarea id="custom-meta-description" name="custom-meta-description" rows="6" style="width:100%"><?php echo esc_textarea($meta_description); ?></textarea>
<br>
<span id="char-count" class="description"><?php printf(__('Characters entered: %d / 160', 'textdomain'), strlen($meta_description)); ?></span>
</p>
<script>
jQuery(document).ready(function($) {
var maxChars = 160;
var $metaDescription = $('#custom-meta-description');
var $charCount = $('#char-count');

$metaDescription.on('input', function() {
var enteredChars = $(this).val().length;
$charCount.text('Characters entered: ' + enteredChars + ' / ' + maxChars);

if (enteredChars > maxChars) {
$charCount.css('color', 'red');
} else {
$charCount.css('color', 'inherit');
}
});

// Enforce character limit
$metaDescription.on('input', function() {
if ($(this).val().length > maxChars) {
$(this).val($(this).val().substring(0, maxChars));
}
});
});
</script>
<?php
}

// Save meta description
function save_custom_meta_description($post_id) {
if (isset($_POST['custom-meta-description'])) {
$meta_description = sanitize_textarea_field($_POST['custom-meta-description']);
update_post_meta($post_id, '_custom_meta_description', $meta_description);
}
}
add_action('save_post', 'save_custom_meta_description');

अब आपको SAVE कर देना है ।

इसके बाद अब नीचे दिए कोड को अपने header.php मे <head> मे paste कर दें ।

<?php $meta_description = get_post_meta($post->ID, '_custom_meta_description', true);
if (empty($meta_description)) {
$trimmed_description = wp_trim_words($post->post_content, 40);
$meta_description = mb_substr($trimmed_description, 0, 160);
}
echo '<meta name="description" content="' . $meta_description . '" />' . "\n"; ?>

अब आप SAVE कर दें ।

इन सभी कोड को आप बताए  हुए जगह मे paste किए होंगे तो आपको writing वाले पेज के दायें तरफ मेटा बॉक्स दिखाई देगा जहा पे आप 160 अक्षरों तक के वाक्य को लिख सकते है।