In what ways can PHP knowledge be leveraged to customize and optimize meta-descriptions for different pages in a WordPress website?
To customize and optimize meta-descriptions for different pages in a WordPress website using PHP, you can leverage PHP knowledge to dynamically generate meta-descriptions based on the content of each page. This can help improve SEO by ensuring that each page has a unique and relevant meta-description.
<?php
function custom_meta_description() {
if (is_single()) {
$post = get_post();
$meta_description = strip_tags($post->post_content);
$meta_description = substr($meta_description, 0, 160); // Limit meta-description to 160 characters
echo '<meta name="description" content="' . $meta_description . '">';
} else {
echo '<meta name="description" content="Default meta description">';
}
}
add_action('wp_head', 'custom_meta_description');
?>