Skip to content

Instantly share code, notes, and snippets.

@asllanmaciel
Created August 31, 2022 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asllanmaciel/fa7235c7cd71fdb05a41d1204fde2558 to your computer and use it in GitHub Desktop.
Save asllanmaciel/fa7235c7cd71fdb05a41d1204fde2558 to your computer and use it in GitHub Desktop.
<?php
function wp24h_add_meta_tags() {
// homepage
if (is_home()) {
echo '<meta name="description" content="' . get_bloginfo( "description" ) . '" />' . "\n";
}
// single post page
if ( is_singular() ) {
global $post;
$meta_description = strip_tags($post->post_content);
$meta_description = strip_shortcodes($post->post_content);
$meta_description = str_replace(array("\n", "\r", "\t"), ' ', $meta_description);
$meta_description = substr($meta_description, 0, 160);
echo '<meta name="description" content="' . $meta_description . '" />' . "\n";
}
// category page
if ( is_category() ) {
$meta_description = strip_tags(category_description());
echo '<meta name="description" content="' . $meta_description . '" />' . "\n";
}
}
add_action( 'wp_head', 'wp24h_add_meta_tags');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment