Skip to content

Instantly share code, notes, and snippets.

@sybrew
Last active June 8, 2021 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sybrew/5fac0d3db62136b70d40c8e997dfea09 to your computer and use it in GitHub Desktop.
Save sybrew/5fac0d3db62136b70d40c8e997dfea09 to your computer and use it in GitHub Desktop.
Replace excerpt TSF
<?php
// Don't include this tag if PHP has already started.
// For: https://wordpress.org/support/topic/auto-generated-meta-description-from-content-not-excerpt/
// I do not recommend using this snippet.
add_filter( 'the_seo_framework_fetched_description_excerpt', function( $excerpt, $depr, $args ) {
// There's no excerpt found, might as well stop trying to fetch it again.
if ( ! strlen( $excerpt ) ) return $excerpt;
$post_id = 0;
if ( null === $args ) {
$tsf = the_seo_framework();
if ( ! $tsf->is_real_front_page() && $tsf->is_singular() ) {
$post_id = $tsf->get_the_real_ID();
}
} else {
$post_id = empty( $args['taxonomy'] ) ? $args['id'] : 0;
}
if ( $post_id ) {
$post = get_post( $post_id );
// TSF already tried getting the stuff below. Return that, instead of reparsing.
if ( empty( $post->post_excerpt ) ) return $excerpt;
// TSF doesn't support some page builders, for that'll double your response times, and Google can replace descriptions anyway.
$_excerpt = $tsf->uses_non_html_page_builder( $post->ID ) ? '' : $post->post_content;
if ( $_excerpt ) {
$_excerpt = $tsf->strip_newline_urls( $_excerpt );
$_excerpt = $tsf->strip_paragraph_urls( $_excerpt );
// Overwrite:
$excerpt = $_excerpt;
}
}
return $excerpt;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment