Skip to content

Instantly share code, notes, and snippets.

@sybrew
Created December 3, 2024 20:22
Show Gist options
  • Save sybrew/ccd21358d0e1ff96dd26066b53e08de3 to your computer and use it in GitHub Desktop.
Save sybrew/ccd21358d0e1ff96dd26066b53e08de3 to your computer and use it in GitHub Desktop.
Filters The SEO Framework's excerpt and replaces it with "excerpt_description" created via Advanced Custom Fields.
<?php
// Don't include the PHP opening tag if PHP is already open.
add_filter( 'the_seo_framework_get_excerpt', 'my_tsf_acf_excerpt', 10, 2 );
/**
* @param string $excerpt The obtained excerpt.
* @param array|null $args The query arguments. Contains 'id', 'tax', 'pta', and 'uid'.
* Is null when the query is auto-determined.
* @return string The overwritten description.
*/
function my_tsf_acf_excerpt( $excerpt, $args ) {
// If ACF isn't activated, don't do anything.
if ( ! function_exists( 'get_field' ) ) return $excerpt;
if ( isset( $args ) ) {
// Admin area.
if ( 'single' === The_SEO_Framework\get_query_type_from_args( $args ) ) {
$post_id = $args['id'];
}
} else {
// On the front-end.
$tsfquery = tsf()->query();
if ( $tsfquery->is_singular() ) {
$post_id = $tsfquery->get_the_real_id();
}
}
if ( ! empty( $post_id ) )
$excerpt = get_field( 'excerpt_description', $post_id ) ?: $excerpt;
return $excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment