Thread Starter
maraki
(@neodjandre)
I think I have figured it out:
function my_seo_framework_description($description, $args) {
if (empty($description)) {
$description = get_field( 'fl_description', $args['id'], false ) ?: '';
$description = strip_shortcodes ( $description);
$description = wp_trim_words($description,25);
}
return $description;
}
add_filter('the_seo_framework_custom_field_description', 'my_seo_framework_description', 10, 2);
add_filter('the_seo_framework_generated_description', 'my_seo_framework_description', 10, 2);
add_filter('the_seo_framework_fetched_description_excerpt', 'my_seo_framework_description', 10, 2);
Please let me know if you see any issues with this code..
Hello!
I recommend using a higher-level filter which allows TSF to generate a proper description for you without much processing power required.
The snippet below is compatible with the TSF v4.1.4 and the upcoming v4.2.0. It requires PHP 7.0 or higher.
add_filter( 'the_seo_framework_fetched_description_excerpt', function( $excerpt, $depr, $args ) {
if ( null === $args ) {
// In the loop.
$get_field = is_singular();
} else {
// Out the loop; only $args['id'] may be filled...
$get_field = $args['id'] && empty( $args['taxonomy'] ) && empty( $args['pta'] );
}
$field = '';
if ( $get_field ) {
$field = function_exists( 'get_field' ) ? get_field( 'fl_description', $args['id'] ?? null ) : '';
}
return $field ?: $excerpt;
}, 10, 3 );
Thread Starter
maraki
(@neodjandre)
ok this seems to work but I am always getting these characters (<p>
) at the beginning of the description
<meta name=”description” content=”<p>Lorem Ipsum text blah blah” />
-
This reply was modified 2 years, 4 months ago by maraki.
Hello!
I didn’t know you added HTML to the field. Here’s the filter adjusted to accommodate for HTML fields:
add_filter( 'the_seo_framework_fetched_description_excerpt', function( $excerpt, $depr, $args ) {
if ( null === $args ) {
// In the loop.
$get_field = is_singular();
} else {
// Out the loop; only $args['id'] may be filled...
$get_field = $args['id'] && empty( $args['taxonomy'] ) && empty( $args['pta'] );
}
$field = '';
if ( $get_field ) {
$field = function_exists( 'get_field' ) ? get_field( 'fl_description', $args['id'] ?? null ) : '';
$field = $field ? the_seo_framework()->s_excerpt_raw( $field ) : '';
}
return $field ?: $excerpt;
}, 10, 3 );
Thread Starter
maraki
(@neodjandre)
ok great 🙂 thank you, I will use this from now on.