Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce + Yoast SEO WordPress

  • SOLVED

Hey everyone, I'm looking for a way to customize my title tag in WooCommerce using the Yoast SEO and the variables available within the plugin.

The structure I desire is as follows:

{productcode} | {manufacturer} + {model} + {color} + {widget}

or

ABC1234 | Tyco 7345 Blue Toolbox

I have all the data loaded up in WooCommerce as either a product name, product attribute, product tag or product category. However, I just can't seem to figure out what the proper format to set this in the Yoast plugin. It's almost like the plugin ignores the WooCommerce attributes.

The only other alternative I can think of is to manually set the title tag, but I'd rather not do this as I have several hundred products to address.

Any suggestions are appreciated,
Kyle

Answers (1)

2012-11-03

Bryan Headrick answers:

I'm not sure how you'd do this using the yoast rewrite titles interface, but I use hooks to accomplish this:

Now, theres a separate hook for the header title (the thing that goes in the title bar) and the page title (the thing in the <h1> tags)

function st_addbrand($title,$id=0) {
if(function_exists('is_product')){
if(is_product()){
$terms = wp_get_post_terms($id,'pa_brand');
foreach($terms as $term){
$theterm = $term->name;}
return $theterm . ' ' . $title;}

else return $title;
}
else return $title;
}
function st_addbrandtotitle($title) {
$title = str_replace(get_bloginfo('name'),'',$title);
if(function_exists('is_product')){if((is_product()||get_query_var( 'pa_brand' )) && !is_tax('pa_brand')){
$terms = wp_get_post_terms(get_the_id(),'pa_brand');
$i = 0;
foreach($terms as $term){
$i++;
$theterm = $term->name;}

if($i>1) $theterm = '';
return $theterm . ' ' . $title ;}
elseif(is_tax( 'product_cat' )& is_product_category()){

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if($term->parent>0){
$parent_cat = get_term($term->parent, get_query_var('taxonomy') );
$parent = ' ' . $parent_cat->name;
}else $parent = '';
$title=str_replace('-','',$title);
$title = $title . $parent;
return $title;

}

else{
$title=str_replace('-','',$title);
return $title;}
}

else {
$title=str_replace('-','',$title);
return $title;}
}

add_filter('the_title', 'st_addbrand',10,2);
add_filter('wp_title', 'st_addbrandtotitle');


This should give you a good starting point.


Bryan Headrick comments:

ok, I checked out the "help" tab, and it looks like this is actually doable using the yoast interface

The thing I think you're missing is that woocommerce attributes are custom taxonomies, but an attribute "manufacturer" actually has a taxonomy name of pa_manufacturer (you might have figured that out from my previous post)


Kyle Hungate comments:

Thanks Bryan! the use of %%ct_pa_model-number%% etc. etc. did the trick, really appreciate the help.