• Resolved espressivo

    (@espressivo)


    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 (calls esc_url())

    Error:

    Uncaught TypeError: ltrim(): Argument #1 ($string) must be of type string, WP_Error given
    in wp-includes/formatting.php:4496

    Stack 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_cat post 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 calls get_term_link() and receives a WP_Error object back. That WP_Error is then passed unchecked into esc_url(), which calls ltrim(), 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 accesses

    These 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 against WP_Error and null term objects before passing the link to esc_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/items that drops crumbs whose URL value is a WP_Error is 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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Jaideep Asrani

    (@jaideeprm)

    Hello @espressivo,

    Thank you for contacting Rank Math support.

    We are not able to reproduce the issue on our end. The breadcrumbs work fine and don’t trigger any error even if the primary category is not set. Since the issue is specific to your setup, please check this thread to get in touch with our development team, so they can check this further: https://ww.wp.xz.cn/support/topic/attention-read-this-before-posting/

    Looking forward to helping you.

    Plugin Support Rakibuzzaman Siam

    (@rakibuzzamansiam1)

    Hello @espressivo,

    We have already replied to your email and will close this thread for now. Please reply to our email so we can continue investigating the issue further.

    If you would like, you can also reopen this thread or create a new one, and we will be happy to assist you.

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.