Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shirokoweb/975b38f774f75ebee0ed8bbe6692acff to your computer and use it in GitHub Desktop.

Select an option

Save shirokoweb/975b38f774f75ebee0ed8bbe6692acff to your computer and use it in GitHub Desktop.
<?php
// Security check to prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* SEOPress Breadcrumb "Fix"
* Ensures home page is always included in breadcrumb JSON-LD schema
* NOT tested with nested (parent) pages
*/
add_filter('seopress_pro_breadcrumbs_json', 'fix_seopress_breadcrumb_home', 10, 1);
function fix_seopress_breadcrumb_home($breadcrumbs) {
// 1. Safety checks
if (!isset($breadcrumbs['itemListElement']) || !is_array($breadcrumbs['itemListElement'])) {
return $breadcrumbs;
}
// 2. Get Home URL (normalized)
$home_url = user_trailingslashit(home_url());
// 3. Check if the first item is already Home
// We check valid array AND if the first item's URL matches Home
if (!empty($breadcrumbs['itemListElement'])) {
$first_item = $breadcrumbs['itemListElement'][0];
// If the first item IS the home page, do nothing and return
// SEOPress uses nested structure: item.url
if (isset($first_item['item']['url']) && $first_item['item']['url'] === $home_url) {
return $breadcrumbs;
}
// Also check the old format just in case
if (isset($first_item['item']) && is_string($first_item['item']) && $first_item['item'] === $home_url) {
return $breadcrumbs;
}
}
// 4. Retrieve Home Label safely
// SEOPress stores breadcrumb settings in 'seopress_pro_option_name'
$options = get_option('seopress_pro_option_name');
$home_label = 'Accueil'; // Default fallback
if (isset($options['seopress_breadcrumbs_i18n_home']) && !empty($options['seopress_breadcrumbs_i18n_home'])) {
$home_label = $options['seopress_breadcrumbs_i18n_home'];
}
// 5. Create the new Home Item (matching SEOPress structure)
$home_item = [
'@type' => 'ListItem',
'position' => 1,
'item' => [
'@type' => 'WebPage',
'id' => $home_url . '#webpage',
'url' => $home_url,
'name' => $home_label
]
];
// 6. Shift positions of existing items
foreach ($breadcrumbs['itemListElement'] as &$item) {
if (isset($item['position'])) {
$item['position']++;
}
}
unset($item); // Break the reference
// 7. Add Home to the start
array_unshift($breadcrumbs['itemListElement'], $home_item);
return $breadcrumbs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment