Skip to content

Instantly share code, notes, and snippets.

@sybrew
Last active May 21, 2025 19:34
Show Gist options
  • Save sybrew/0d469e4a5fdcc0313a51ac2e7a1e3c84 to your computer and use it in GitHub Desktop.
Save sybrew/0d469e4a5fdcc0313a51ac2e7a1e3c84 to your computer and use it in GitHub Desktop.
Use ACF fields for meta descriptions via excerpt extraction in The SEO Framework plugin, supporting both admin and front-end contexts.
<?php
// Do not include the PHP opening tag if PHP is already open.
// Per email Wojciech.
add_filter( 'the_seo_framework_description_excerpt', 'my_tsf_acf_custom_excerpt', 10, 3 );
/**
* Use ACF HTML fields as meta description for specific CPTs.
*
* @param string $excerpt The auto‐generated excerpt.
* @param array|null $args Query args ( [ 'id','tax','pta','uid'] ) or null.
* @param string $type Description type: 'search','opengraph','twitter'.
* @return string
*/
function tsf_acf_meta_desc( $excerpt, $args, $type ) {
// Bail if ACF isn’t active.
if ( ! function_exists( 'get_field' ) )
return $excerpt;
// Determine post ID.
if ( isset( $args['id'] ) ) {
$post_id = (int) $args['id'];
} else {
// Only proceed on single views.
if ( 'single' !== \The_SEO_Framework\get_query_type_from_args( $args ) )
return $excerpt;
$post_id = tsf()->query()->get_the_real_id();
}
// Map CPT → ACF field.
$map = [
'member' => 'tm_desc',
'practice' => 'i_fulldesc',
];
$cpt = get_post_type( $post_id );
if ( ! isset( $map[ $cpt ] ) )
return $excerpt;
$html = get_field( $map[ $cpt ], $post_id );
if ( ! $html )
return $excerpt;
// Extract HTML content considering its tags.
$text = tsf()->format()->html()->extract_content( $html );
return $text ?: $excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment