Support » Plugin: Rank Math - SEO Plugin for WordPress » Make %excerpt% pull from Product Description Instead of Short Description

  • Resolved wooq123

    (@wooq123)


    I tried switching over from All in One SEO, but I’ve encountered a problem: the excerpts that are auto generating for my products are using the short description (which in most cases has one line like “free shipping”) instead of generating an excerpt from the beginning of the product description like other SEO plugins. How do I make Rank Math generate the default excerpt from the actual product description rather than the short description?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Rank Math

    (@rankmath)

    Hello @wooq123

    Thank you for contacting the support.

    Assuming you are on the latest version of the plugin, in WooCommerce products, the short description is stored in the excerpt field.

    By default, we are adding excerpts to the SEO Description. You can use the below filter to change that behavior:
    https://rankmath.com/kb/filters-hooks-api-developer/#change-meta-description

    Hope that helps.

    wooq123

    (@wooq123)

    Will this only affect the auto generated excerpt for products that don’t have a custom excerpt? Also, when I apply this I don’t see any change on preview excerpt when editing a product.

    Plugin Author Rank Math

    (@rankmath)

    Hello @wooq123

    In WooCommerce’s product, the short description is stored in the excerpt field.

    By default, we are adding the excerpts to the SEO Description. You can use the below filter to change that:
    https://rankmath.com/kb/filters-hooks-api-developer/#change-meta-description

    Hope that helps. Thank you.

    wooq123

    (@wooq123)

    Adding that filter to my child theme’s functions.php file has not changed the behavior, and I’m still seeing my product short descriptions in the preview section when I edit products. Are there any plans to simply add a setting into the plugin that changes the source of the default excerpt? I imagine I’m not the only one who doesn’t one excerpts generated from a short description.

    Plugin Author Rank Math

    (@rankmath)

    Hello @wooq123

    Please use the following code in your themes’s functions.php file to show excerpt from the main content:

    add_action( 'rank_math/frontend/description', function( $description ) {
    
    	if ( ! is_singular() ) {
    		return $description;
    	}
    
    	 global $post;
    	 $desc = RankMath\Post::get_meta( 'description', $post->ID );
    
    	 if ( $desc ) {
    	 	return $desc;
    	 }
    
    	if ( empty( $post ) || empty( $post->post_content ) ) {
    		return $description;
    	}
    
    	$keywords     = RankMath\Post::get_meta( 'focus_keyword', $post->ID );
    	$post_content = RankMath\Paper\Paper::should_apply_shortcode() ? do_shortcode( $post->post_content ) : $post->post_content;
    	$post_content = \preg_replace( '/<!--[\s\S]*?-->/iu', '', $post_content );
    	$post_content = wpautop( $post_content );
    	$post_content = wp_kses( $post_content, [ 'p' => [] ] );
    
    	// 4. Paragraph with the focus keyword.
    	if ( ! empty( $keywords ) ) {
    		$regex = '/<p>(.*' . str_replace( [ ',', ' ', '/' ], [ '|', '.', '\/' ], $keywords ) . '.*)<\/p>/iu';
    		\preg_match_all( $regex, $post_content, $matches );
    		if ( isset( $matches[1], $matches[1][0] ) ) {
    			return $matches[1][0];
    		}
    	}
    
    	// 5. The First paragraph of the content.
    	\preg_match_all( '/<p>(.*)<\/p>/iu', $post_content, $matches );
    	return isset( $matches[1], $matches[1][0] ) ? wp_html_excerpt( $matches[1][0], 160 ) : '';
    });

    Hope that helps. Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Make %excerpt% pull from Product Description Instead of Short Description’ is closed to new replies.