Skip to content

Instantly share code, notes, and snippets.

@sybrew
Created January 29, 2021 08:20
Show Gist options
  • Save sybrew/3b5f329b6098d5713a55fd6e31751899 to your computer and use it in GitHub Desktop.
Save sybrew/3b5f329b6098d5713a55fd6e31751899 to your computer and use it in GitHub Desktop.
Add WooCommerce SKU to auto-generated titles for The SEO Framemework
<?php
// Don't include the PHP tag if PHP is already opened in the file.
add_filter( 'the_seo_framework_title_from_generation', function( $title, $args ) {
if ( ! function_exists( 'wc' ) ) return $title;
if ( null === $args ) {
$product = the_seo_framework()->is_product() ? wc_get_product() : '';
} else {
$product = the_seo_framework()->is_product( $args['id'] ) ? wc_get_product( $args['id'] ) : '';
}
if ( is_a( $product, '\WC_Product' ) && method_exists( $product, 'get_sku' ) ) {
$sku = $product->get_sku();
if ( strlen( $sku ) ) {
// TSF will sanitize.
$title = "$sku &ndash; $title";
}
}
return $title;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment