• Resolved Joe G.

    (@webgmclassics)


    I am using WooCommerce, I would like to prefix my product meta title (and description) with a custom generated string based on product categories.

    I was looking at this filter:

    add_filter( ‘the_seo_framework_title_from_generation’, function( $title, $args ) {
    if ( is_post_type_archive( ‘my_post_type_name’ ) ) {
    $title = ‘My custom title’;
    }
    return $title;
    }, 10, 2 )

    What arguments are passed? Is the post_ID included? If so, I can use this filter to generate what I need.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello Joe!

    Yes, there is ?? You found the correct filter. I think this will get you started–the $args parameter should speak for itself in this example:

    add_filter( 'the_seo_framework_title_from_generation', function( $title, $args ) {
    
    	$is_product = false;
    
    	if ( null === $args ) {
    		// in the loop, use WP query.
    		$is_product = the_seo_framework()->is_product();
    	} else {
    		// out the loop, use args.
    		if ( ! $args['taxonomy'] ) {
    			$is_product = the_seo_framework()->is_product( $args['id'] );
    		}
    	}
    
    	if ( $is_product && function_exists( 'wc_get_product_cat_ids' ) ) {
    		$categories = [];
    
    		foreach ( wc_get_product_cat_ids( $args['id'] ) as $cat_id ) {
    			$term = get_term( $cat_id );
    
    			if ( ! empty( $term->name ) )
    				$categories[] = $term->name;
    		}
    
    		// Do something nice with the category list here.
    		$title = implode( ', ', $categories ) . ': ' . $title;
    	}
    
    	return $title;
    }, 10, 2 );

    It won’t work too nicely with our Javascript, where the input-example-placeholder will reset as you change the page title, but it’d do its job otherwise perfectly fine ??

    Thread Starter Joe G.

    (@webgmclassics)

    Thank you! I figured it out a while ago, but the loop detection helps!

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Joe,

    I found that I made a mistake in the example–I assumed $args['id'] was set in the foreach-loop. Although it still works as intended, you might get PHP warnings. This updated code addresses those:

    add_filter( 'the_seo_framework_title_from_generation', function( $title, $args ) {
    
    	$is_product = false;
    
    	if ( null === $args ) {
    		// in the loop, use WP query.
    		$is_product = the_seo_framework()->is_product();
    	} else {
    		// out the loop, use args.
    		if ( ! $args['taxonomy'] ) {
    			$is_product = the_seo_framework()->is_product( $args['id'] );
    		}
    	}
    
    	if ( $is_product && function_exists( 'wc_get_product_cat_ids' ) ) {
    		$id         = isset( $args['id'] ) ? $args['id'] : the_seo_framework()->get_the_real_ID();
    		$categories = [];
    
    		foreach ( wc_get_product_cat_ids( $id ) as $cat_id ) {
    			$term = get_term( $cat_id );
    
    			if ( ! empty( $term->name ) )
    				$categories[] = $term->name;
    		}
    
    		// Do something nice with the category list here.
    		$title = implode( ', ', $categories ) . ': ' . $title;
    	}
    
    	return $title;
    }, 10, 2 );

    Cheers ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Is there a filter for product title prefix?’ is closed to new replies.