Skip to content

Instantly share code, notes, and snippets.

@cre8tivediva
Created May 31, 2024 18:06
Show Gist options
  • Save cre8tivediva/2a4d979a83109b4e03ab81edbaf7ca2a to your computer and use it in GitHub Desktop.
Save cre8tivediva/2a4d979a83109b4e03ab81edbaf7ca2a to your computer and use it in GitHub Desktop.
Inject Genesis SEO title and meta description into WooCommerce Shop page
// By default, when using Genesis' SEO settings (not a plugin like Yoast or Rankmath), the title and meta description will not show up and will not display the new title name on the browser tab. Code provided by ChatGPT, tested and works.
// Inject Genesis SEO title and meta description into WooCommerce Shop page
add_action('wp_head', 'custom_shop_page_meta', 1);
function custom_shop_page_meta() {
if (is_shop()) {
$shop_page_id = wc_get_page_id('shop');
// Retrieve Genesis title and meta description
$shop_title = get_post_meta($shop_page_id, '_genesis_title', true);
$meta_description = get_post_meta($shop_page_id, '_genesis_description', true);
// Inject custom meta description if available
if ($meta_description) {
echo '<meta name="description" content="' . esc_attr($meta_description) . '">';
}
// Inject custom title if available
if ($shop_title) {
echo '<title>' . esc_html($shop_title) . '</title>';
}
}
}
// Filter to override WooCommerce page title
add_filter('document_title_parts', 'custom_shop_page_title', 10);
function custom_shop_page_title($title) {
if (is_shop()) {
$shop_page_id = wc_get_page_id('shop');
$shop_title = get_post_meta($shop_page_id, '_genesis_title', true);
if ($shop_title) {
$title['title'] = esc_html($shop_title);
}
}
return $title;
}
// Ensure Genesis SEO settings are applied
add_action('template_redirect', 'apply_genesis_seo_to_shop_page');
function apply_genesis_seo_to_shop_page() {
if (is_shop()) {
remove_action('woocommerce_shop_page', 'woocommerce_shop_page', 10);
remove_action('wp_head', 'wc_page_enhanced_seo', 1);
add_action('woocommerce_shop_page', 'custom_shop_page_meta');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment