Automatic Meta Description
Prints a meta description from the excerpt or opening words of posts, from term descriptions on archives and from the tagline on the homepage. Skipped when an SEO plugin is active.
Opens WPIDE on
Code
add_action( 'wp_head', function () {
if ( defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ) || defined( 'AIOSEO_VERSION' ) || defined( 'THE_SEO_FRAMEWORK_VERSION' ) || defined( 'SEOPRESS_VERSION' ) ) {
return;
}
$description = '';
if ( is_singular() ) {
$post = get_queried_object();
$description = has_excerpt( $post ) ? $post->post_excerpt : wp_trim_words( excerpt_remove_blocks( strip_shortcodes( $post->post_content ) ), 35, '' );
} elseif ( is_category() || is_tag() || is_tax() ) {
$description = term_description();
} elseif ( is_front_page() || is_home() ) {
$description = get_bloginfo( 'description' );
}
$description = trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( (string) $description ) ) );
if ( '' !== $description ) {
printf( "<meta name=\"description\" content=\"%s\" />\n", esc_attr( wp_html_excerpt( $description, 160, '' ) ) );
}
}, 1 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.