Skip to content

Instantly share code, notes, and snippets.

@sybrew
Last active April 12, 2024 19:03
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/299ad19597f974c89b1564316297c1ed to your computer and use it in GitHub Desktop.
Save sybrew/299ad19597f974c89b1564316297c1ed to your computer and use it in GitHub Desktop.
Gets a custom post and term fields from ACF to override TSF's generated description.
<?php
// Don't include the PHP opening tag if PHP is already open.
// Previous: https://gist.github.com/sybrew/0562fdbc98d7fdc134c2c9ff5bad863b.
// For: https://wordpress.org/support/topic/meta-og-description-for-a-custom-taxonomy-term/
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 ACF isn't activated, don't do anything.
if ( ! function_exists( 'get_field' ) ) return $desc;
if ( isset( $args ) ) {
// Admin area.
switch ( The_SEO_Framework\get_query_type_from_args( $args ) ) {
case 'term':
$term = get_term( $args['id'], $args['tax'] );
break;
case 'single':
$post_id = $args['id'];
}
} else {
// On the front-end.
$tsfquery = tsf()->query();
if ( $tsfquery->is_editable_term() ) {
$term = get_queried_object();
} elseif ( $tsfquery->is_singular() ) {
$post_id = $tsfquery->get_the_real_id();
}
}
if ( ! empty( $term ) ) {
$desc = wp_strip_all_tags( get_field( 'description', $term ) ) ?: $desc;
} elseif ( ! empty( $post_id ) ) {
$desc = wp_strip_all_tags( get_field( 'synopsis', $post_id ) ) ?: $desc;
}
return $desc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment