Support » Plugin: The SEO Framework » Use custom field to generate description

  • Resolved rustyd0g

    (@rustyd0g)


    I have a custom post type called “products”. All of the fields for this CPT are custom fields created with ACF. I have an ACF field “product_description” that holds the product description. Is it possible to configure this plugin to use the value of that custom field to generate the description?

    Thanks for your help!

Viewing 12 replies - 1 through 12 (of 12 total)
  • I’ve taken a different approach in the meantime, I’ve added a filter to set the post_content for my CPT equal to the value of my custom field product_description when the post is created / updated. This works fine but it seems like a roundabout way of doing things.

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi rustyd0g,

    That roundabout does seem to be a new source for bugs.
    Currently, it’s not possible to directly inject an excerpt.

    However, it’s possible to inject a custom description. See example below (untested!):

    add_filter( 'the_seo_framework_description_output', 'my_description_excerpt', 10, 2 );
    add_filter( 'the_seo_framework_ogdescription_output', 'my_description_excerpt', 10, 2 );
    add_filter( 'the_seo_framework_twitterdescription_output', 'my_description_excerpt', 10, 2 );
    /**
     * Filters the description from The SEO Framework.
     *
     * @since xxx
     * @requires The SEO Framework 2.7.0+.
     *
     * @param string $description The current description.
     * @param int $id The post/page/taxonomy/term ID.
     * @return string The description.
     */
    function my_description_excerpt( $description = '', $id = 0 ) {
    
    	if ( is_singular( 'products' ) ) {
    		//* Products post type.
    		$custom_desc = get_post_meta( $id, 'product_description', true );
    		if ( $custom_desc ) {
    			//* Custom desc has been found.
    
    			// Trim to 155 chars, if it exceeds.
    			$custom_desc = the_seo_framework()->trim_excerpt( $custom_desc, mb_strlen( $custom_desc ), 155 );
    
    			/**
    			 * The SEO Framework 2.7.1+ allows complete removal of html tags, as
    			 * well as optional shortcode execution on excerpts. (requires another filter)
    			 *
    			 * It also runs s_description.
    			 * Which removes multi-line and multi-space characters.
    			 */
    			if ( method_exists( the_seo_framework_class(), 's_excerpt' ) ) {
    				$custom_desc = the_seo_framework()->s_excerpt( $custom_desc );
    			} else {
    				$custom_desc = the_seo_framework()->s_description( $custom_desc );
    			}
    
    			// Prettify, escape and override output.
    			$description = the_seo_framework()->escape_description( $custom_desc );
    		}
    	}
    
    	return $description;
    }

    It’s quite advanced, but it allows for so many possibilities :).
    It pretty much does what you’re doing already, but then through the right filter and a bit faster as it’s not testing multiple post meta.

    I hope this helps! Best of luck!

    rustyd0g

    (@rustyd0g)

    Thanks @cybr. That’s very useful.

    maraki

    (@neodjandre)

    Hi,

    I am in the same situation but I want to use this for all of my custom post types and pages. Could you please let me know what code I can use?

    many thanks,
    Maria 🙂

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Maria,

    I’m unsure which field you wish to populate with your description.

    Could you share more information on “which” post type and “which” field name?

    Cheers!

    maraki

    (@neodjandre)

    I have 10 different post types and the field is the same: “fl_description”.

    Could I use a universal code which is applicable to all posts, pages and custom post types?

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Maria,

    I think this should work as you intend to:

    add_filter( 'the_seo_framework_description_output', 'my_description_excerpt', 10, 2 );
    add_filter( 'the_seo_framework_ogdescription_output', 'my_description_excerpt', 10, 2 );
    add_filter( 'the_seo_framework_twitterdescription_output', 'my_description_excerpt', 10, 2 );
    /**
     * Filters the description from The SEO Framework.
     *
     * @since xxx
     * @requires The SEO Framework 2.7.0+.
     *
     * @param string $description The current description.
     * @param int $id The post/page/taxonomy/term ID.
     * @return string The description.
     */
    function my_description_excerpt( $description = '', $id = 0 ) {
    
    	$custom_desc = get_post_meta( $id, 'fl_description', true );
    	if ( $custom_desc ) {
    		//* Custom desc has been found.
    
    		// Trim to 155 chars, if it exceeds.
    		$custom_desc = the_seo_framework()->trim_excerpt( $custom_desc, mb_strlen( $custom_desc ), 155 );
    
    		/**
    		 * The SEO Framework 2.7.1+ allows complete removal of html tags, as
    		 * well as optional shortcode execution on excerpts. (requires another filter)
    		 *
    		 * It also runs s_description.
    		 * Which removes multi-line and multi-space characters.
    		 */
    		if ( method_exists( the_seo_framework_class(), 's_excerpt' ) ) {
    			$custom_desc = the_seo_framework()->s_excerpt( $custom_desc );
    		} else {
    			$custom_desc = the_seo_framework()->s_description( $custom_desc );
    		}
    
    		// Prettify, escape and override output.
    		$description = the_seo_framework()->escape_description( $custom_desc );
    	}
    
    	return $description;
    }
    maraki

    (@neodjandre)

    I am getting this error in the source code:

    Notice: the_seo_framework()->s_description() is not accessible. In /var/www/html/wp-content/themes/x-child/functions.php on line 431.<br>

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Maria,

    Oops! That was my mistake.
    I should make all those functions public… (this is then also an issue for the snippet I gave you @rustyd0g).

    This must work then, but it won’t cut any “multiline/double spaces” until version 2.7.1 is installed:

    add_filter( 'the_seo_framework_description_output', 'my_description_excerpt', 10, 2 );
    add_filter( 'the_seo_framework_ogdescription_output', 'my_description_excerpt', 10, 2 );
    add_filter( 'the_seo_framework_twitterdescription_output', 'my_description_excerpt', 10, 2 );
    /**
     * Filters the description from The SEO Framework.
     *
     * @since xxx
     * @requires The SEO Framework 2.7.0+.
     *
     * @param string $description The current description.
     * @param int $id The post/page/taxonomy/term ID.
     * @return string The description.
     */
    function my_description_excerpt( $description = '', $id = 0 ) {
    
    	$custom_desc = get_post_meta( $id, 'fl_description', true );
    	if ( $custom_desc ) {
    		//* Custom desc has been found.
    
    		// Trim to 155 chars, if it exceeds.
    		$custom_desc = the_seo_framework()->trim_excerpt( $custom_desc, mb_strlen( $custom_desc ), 155 );
    
    		/**
    		 * The SEO Framework 2.7.1+ allows complete removal of html tags, as
    		 * well as optional shortcode execution on excerpts. (requires another filter)
    		 *
    		 * It also runs s_description.
    		 * Which removes multi-line and multi-space characters.
    		 */
    		if ( method_exists( the_seo_framework_class(), 's_excerpt' ) ) {
    			$custom_desc = the_seo_framework()->s_excerpt( $custom_desc );
    		}
    
    		// Prettify, escape and override output.
    		$description = the_seo_framework()->escape_description( $custom_desc );
    	}
    
    	return $description;
    }

    Cheers!

    maraki

    (@neodjandre)

    Ok this works but 2 things.

    1. My meta description appears like:

    <meta name="description" content="Chef Yoshinori Ishii is on a mission to spread his <em>Ike Jime revolution</em> across the world." />

    Is google going to translate those special characters correctly or is that gibberish text ?

    2. If I have a custom meta description in the SEO box of the post, this gets overwritten. I can live with that 🙂

    many thanks again x Maria

    • This reply was modified 4 years ago by maraki.
    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Maria,

    1. The <em> tags will either be omitted by Google, or they’ll be included as is.
    Google will not ever include external scripts or code into their search results.

    To fix this issue, you’d have to add the following right below //* Custom desc has been found.:

    $custom_desc = wp_strip_all_tags( $custom_desc, true );
    

    If the description contains special characters in the code like &quot;&#19;, it will always be rendered as .

    2. That’s right. The filters used take priority over the one from the SEO settings. To fix this, you’d have to change this line:
    From: if ( $custom_desc ) {
    To: if ( $custom_desc && is_singular() && empty( the_seo_framework()->get_custom_field( '_genesis_description', $id ) ) ) {

    Cheers!

    maraki

    (@neodjandre)

    ok thanks works great now !

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Use custom field to generate description’ is closed to new replies.