Skip to content

Instantly share code, notes, and snippets.

@sunriseweb
Last active October 28, 2019 12:01
Show Gist options
  • Save sunriseweb/9dbc059bad64b2fcb6486683d5e7e2e5 to your computer and use it in GitHub Desktop.
Save sunriseweb/9dbc059bad64b2fcb6486683d5e7e2e5 to your computer and use it in GitHub Desktop.
Divi auto-generate Yoast WordPress SEO meta descriptions
<?php
/* Fixes Divi page builder problem not allowing Yoast to auto-generate meta descriptions
* Updated August 22, 2016
*/
add_filter( 'wpseo_metadesc', 'divi_auto_gen_meta_description' ); //see https://yoast.com/wordpress/plugins/seo/api/
function divi_auto_gen_meta_description($metadesc) {
if( trim($metadesc) == '' ) {
global $post;
$rendered_content = apply_filters('the_content', $post->post_content);
$newmetadesc = strip_tags( $rendered_content );
$newmetadesc = preg_replace( "/\r|\n/", " ", $newmetadesc );
$newmetadesc = trim($newmetadesc);
$newmetadesc = str_replace( " ", " ", $newmetadesc ); //replace tabs with spaces
$newmetadesc = preg_replace('/\s+/', ' ', $newmetadesc); //replace multiple spaces with a single space
return trim(substr($newmetadesc, 0, 250));
} else {
return $metadesc;
}
}
@uhlhosting
Copy link

Does this only works with Divi?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment