Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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