Skip to content

Instantly share code, notes, and snippets.

@PechenkiUA
Created February 15, 2022 06:52
Show Gist options
  • Save PechenkiUA/039b99a0e944f9e43fddf9ff5e37102d to your computer and use it in GitHub Desktop.
Save PechenkiUA/039b99a0e944f9e43fddf9ff5e37102d to your computer and use it in GitHub Desktop.
Wordpress auto Description
<?php
function autoDescription() {
global $post;
if (!is_singular()) {return; }
elseif(!empty( $post->post_excerpt)) {
echo "<meta name='description' content='$post->post_excerpt' />";
}
else{
$meta = apply_filters('the_content', $post->post_content);
$meta = strip_tags($meta);
$meta = strip_shortcodes($meta );
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 400);
echo "<meta name='description' content='$meta' />";
}
}
add_action('wp_head', 'create_meta_desc');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment