Primary category
-
Subject: Fatal error in breadcrumbs when product is missing primary category meta
Hi Rank Math team,
Wanted to flag a reproducible fatal in the WooCommerce product breadcrumb output.
Plugin: Rank Math SEO
File:includes/frontend/class-breadcrumbs.php
Line: 147 (callsesc_url())Error:
Uncaught TypeError: ltrim(): Argument #1 ($string) must be of type string, WP_Error given in wp-includes/formatting.php:4496Stack trace (abbreviated):
#0 wp-includes/formatting.php(4496): ltrim() #1 seo-by-rank-math/includes/frontend/class-breadcrumbs.php(147): esc_url() #2 seo-by-rank-math/includes/frontend/class-shortcodes.php(73): get_breadcrumb()Root cause:
When a WooCommerce product is missing the
rank_math_primary_product_catpost meta (e.g. legacy products created before Rank Math was installed, products migrated from Yoast or another SEO plugin, or products whose primary category was never explicitly set), the breadcrumb builder callsget_term_link()and receives aWP_Errorobject back. ThatWP_Erroris then passed unchecked intoesc_url(), which callsltrim(), which throws the TypeError under PHP 8.x — fataling the entire product page.Related warnings appear in the same stack on affected products:
class-product.php:96 — Attempt to read property "parent" on null class-product.php:100 — Attempt to read property "term_id" on null / Undefined array key 0 class-product.php:105 — Attempt to read property "name" on null / Undefined array key 0 class-breadcrumbs.php:532/534 — similar null-property accessesThese all stem from the same condition: the primary category lookup returns nothing, and downstream code assumes a valid term object.
Impact:
On one site I looked at, ~280 products were missing the primary category meta. Every front-end visit to one of those product URLs returned a 500 to the user, search engine crawlers, and (in this case) the site’s backup/management service’s sync bot — which is how the issue was originally discovered.
Suggested fix:
At
class-breadcrumbs.php:147, guard againstWP_Errorand null term objects before passing the link toesc_url(). Either fall back to the parent category, the shop page, or simply omit the broken crumb — anything is better than a hard fatal on a public product page.For reference, a small mu-plugin filter on
rank_math/frontend/breadcrumb/itemsthat drops crumbs whose URL value is aWP_Erroris enough to neutralize the fatal on the user side, but it would be much better to handle this defensively inside the plugin so the warnings/notices also go away.Thanks for the great plugin!
You must be logged in to reply to this topic.