Skip to content

Instantly share code, notes, and snippets.

@sybrew
Created February 16, 2026 07:50
Show Gist options
  • Select an option

  • Save sybrew/6fac62935cc66f2f027ae4b251c58bc0 to your computer and use it in GitHub Desktop.

Select an option

Save sybrew/6fac62935cc66f2f027ae4b251c58bc0 to your computer and use it in GitHub Desktop.
This snippet shows how to pull an Advanced Custom Fields (ACF) field value into the auto-generated meta description.
<?php
// Do not include the PHP opening tag if PHP is already open.
/**
* ACF Description Snippet for The SEO Framework
*
* This snippet shows how to pull an Advanced Custom Fields (ACF) field value
* into the auto-generated meta description. Two approaches are demonstrated:
*
* 1. With HTML -- When your ACF field stores rich/HTML content, use
* `tsf_acf_meta_desc_with_html()`. It strips tags intelligently via
* TSF's `extract_content()` so that block-level elements become proper
* sentence breaks instead of glued-together words.
*
* 2. Without HTML -- When your ACF field already stores plain text, use
* `tsf_acf_meta_desc_without_html()`. It returns the value as-is,
* falling back to the original excerpt when the field is empty.
*
* How to use:
* 1. Copy ONE of the two examples below into your theme's functions.php
* or a custom plugin file.
* 2. Replace 'my_custom_field_name' with the ACF field name you want.
* 3. (Optional) Adjust the post-type or query-type guard to narrow
* which pages are affected.
*
* @package The_SEO_Framework\Snippets
*/
// -- Example 1: ACF field that contains HTML ---------------------------------
add_filter( 'the_seo_framework_description_excerpt', 'tsf_acf_meta_desc_with_html', 10, 3 );
/**
* Replaces the auto-generated meta description with an ACF field value
* that may contain HTML markup.
*
* The HTML is converted to plain text via TSF's extract_content() helper,
* which respects block-level tags (e.g. <p>, <div>) as word boundaries so
* the resulting sentence reads naturally.
*
* Falls back to the original excerpt when the ACF field is empty or when
* the current request is not a singular post/page view.
*
* @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 The replacement description, or the original excerpt.
*/
function tsf_acf_meta_desc_with_html( $excerpt, $args, $type ) {
// Bail early when ACF is not active on this site.
if ( ! function_exists( 'get_field' ) )
return $excerpt;
// Resolve the post ID from the supplied args, or from the current query.
if ( isset( $args['id'] ) ) {
$post_id = (int) $args['id'];
} else {
// Only proceed on singular (post/page) views.
if ( 'single' !== \The_SEO_Framework\get_query_type_from_args( $args ) )
return $excerpt;
$post_id = tsf()->query()->get_the_real_id();
}
// Grab the raw HTML value from ACF. Change the field name to match yours.
$html = get_field( 'my_custom_field_name', $post_id );
if ( ! $html )
return $excerpt;
// Strip tags while preserving natural word boundaries between blocks.
$text = tsf()->format()->html()->extract_content( $html );
return $text ?: $excerpt;
}
// -- Example 2: ACF field that contains plain text ---------------------------
add_filter( 'the_seo_framework_description_excerpt', 'tsf_acf_meta_desc_without_html', 10, 3 );
/**
* Replaces the auto-generated meta description with an ACF plain-text
* field value.
*
* Use this variant when your ACF field already stores plain text (e.g. a
* "Text" or "Text Area" field type). No HTML processing is performed;
* the value is returned directly.
*
* Falls back to the original excerpt when the ACF field is empty or when
* the current request is not a singular post/page view.
*
* @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 The replacement description, or the original excerpt.
*/
function tsf_acf_meta_desc_without_html( $excerpt, $args, $type ) {
// Bail early when ACF is not active on this site.
if ( ! function_exists( 'get_field' ) )
return $excerpt;
// Resolve the post ID from the supplied args, or from the current query.
if ( isset( $args['id'] ) ) {
$post_id = (int) $args['id'];
} else {
// Only proceed on singular (post/page) views.
if ( 'single' !== \The_SEO_Framework\get_query_type_from_args( $args ) )
return $excerpt;
$post_id = tsf()->query()->get_the_real_id();
}
// Return the plain-text ACF value, falling back to the original excerpt.
return get_field( 'my_custom_field_name', $post_id ) ?: $excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment