Created
February 21, 2024 17:23
-
-
Save sybrew/7a0811464ea1ab825b1ebae2266012bb to your computer and use it in GitHub Desktop.
Append brand name from custom pods field to TSF generated title.php
This file contains 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 | |
add_filter( | |
'the_seo_framework_title_from_generation', | |
function ( $title, $args ) { | |
// If Pods is deactivated, skip further processing. | |
if ( ! function_exists( 'pods_field' ) ) return $title; | |
if ( null === $args ) { | |
// Front end. | |
$tsfquery = tsf()->query(); | |
$post_id = $tsfquery->get_the_real_id(); | |
$is_product = $tsfquery->is_product(); | |
} elseif ( 'single' === The_SEO_Framework\get_query_type_from_args( $args ) ) { | |
$post_id = $args['id']; | |
$is_product = 'product' === get_post_type( $post_id ); | |
} | |
// If we're not modifying a product, bail. | |
if ( empty( $is_product ) ) return $title; | |
// Grabs the subtitle meta field from the product. | |
$subtitle = get_post_meta( $post_id, 'subtitle', true ); | |
// Extract the brandname. | |
preg_match( '/By :(.*?)/', $subtitle, $matches ); | |
// Append brandname (if any). TSF will remove extraneous spaces. | |
return "$title $matches[1]"; | |
}, | |
10, | |
2, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment