Skip to content

Instantly share code, notes, and snippets.

@sybrew
Last active May 5, 2024 09:44
Show Gist options
  • Save sybrew/1b10e9c6074a4835d1b9d6f068b05af7 to your computer and use it in GitHub Desktop.
Save sybrew/1b10e9c6074a4835d1b9d6f068b05af7 to your computer and use it in GitHub Desktop.
<?php
// Don't include the PHP opening tag if PHP is already open.
// Previous: https://gist.github.com/sybrew/299ad19597f974c89b1564316297c1ed
// For: https://wordpress.org/support/topic/fetch-meta-description-on-custom-field-from-meta-box/
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 Meta Box isn't activated, don't do anything.
if ( ! function_exists( 'rwmb_get_value' ) ) return $desc;
if ( isset( $args ) ) {
// Admin area.
switch ( The_SEO_Framework\get_query_type_from_args( $args ) ) {
case 'single':
$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 ) )
$desc = wp_strip_all_tags( rwmb_get_value( 'short_description', [], $post_id ) ) ?: $desc;
return $desc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment