Created
January 29, 2021 08:20
-
-
Save sybrew/3b5f329b6098d5713a55fd6e31751899 to your computer and use it in GitHub Desktop.
Add WooCommerce SKU to auto-generated titles for The SEO Framemework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 – $title"; | |
} | |
} | |
return $title; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment