Last active
October 28, 2019 12:01
-
-
Save sunriseweb/9dbc059bad64b2fcb6486683d5e7e2e5 to your computer and use it in GitHub Desktop.
Divi auto-generate Yoast WordPress SEO meta descriptions
This file contains hidden or 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 | |
/* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this only works with Divi?