Support » Plugin: The SEO Framework – Fast, Automated, Effortless. » Custom Field – Meta Description

  • Resolved maraki

    (@neodjandre)


    Hello,

    I have been using this function to make the metafield description take the value of a custom field. However, this no longer works. Any help please?

    add_filter( 'the_seo_framework_custom_field_description', function( $description, $args ) {
    
    	if ( $description )
    		return $description;
    
    	if ( function_exists( 'get_field' ) ) {
    		// Pass ID for SEO bar compatibility.
    		$excerpt = get_field( 'fl_description', $args['id'], false ) ?: '';
    
    		if ( $excerpt ) {
    
    			$description = the_seo_framework()->trim_excerpt( $excerpt, mb_strlen( $excerpt ), 155 );
    		}
    	}
    
    	return $description;
    }, 10, 2 );
Viewing 5 replies - 1 through 5 (of 5 total)
  • 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..

    Plugin Author Sybre Waaijer

    (@cybr)

    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.
    Plugin Author Sybre Waaijer

    (@cybr)

    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Field – Meta Description’ is closed to new replies.