Last active
February 17, 2024 15:19
-
-
Save sybrew/0562fdbc98d7fdc134c2c9ff5bad863b to your computer and use it in GitHub Desktop.
Gets a custom term field from ACF to override TSF's generated description.
This file contains 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 | |
// Don't include the PHP opening tag if PHP is already open. | |
add_filter( 'the_seo_framework_generated_description', 'my_tsf_generated_description', 10, 2 ); | |
/** | |
* @param string $desc The generated description. | |
* @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_generated_description( $desc, $args ) { | |
if ( ! function_exists( 'get_field' ) ) return $desc; | |
if ( isset( $args ) ) { | |
// Custom query, such as for the SEO Bar. | |
if ( 'term' === The_SEO_Framework\get_query_type_from_args( $args ) ) { | |
$term = get_term( $args['id'], $args['tax'] ); | |
} | |
} else { | |
// On the front-end. | |
if ( tsf()->query()->is_editable_term() ) { | |
$term = get_queried_object(); | |
} | |
} | |
if ( ! empty( $term ) && ! is_wp_error( $term ) ) { | |
// Fall back to generated description if no description is set via the term's custom field. | |
$desc = get_field( 'description', $term ) ?: $desc; | |
} | |
return $desc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment