Created
February 15, 2022 06:52
-
-
Save PechenkiUA/039b99a0e944f9e43fddf9ff5e37102d to your computer and use it in GitHub Desktop.
Wordpress auto Description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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