Support » Plugin: Yoast SEO » wp_seo_get_bc_ancestors hook not working

  • Resolved buzut

    (@buzut)


    Hi!

    I’m trying to customise the breadcrumb path and saw that the wp_seo_get_bc_ancestors but it doesn’t seem to work.

    
    // functions.php
    
    function breadcrumb_ancestors($arr) {
        // this does nothing
        var_dump($arr);
    }
    
    add_filter('wp_seo_get_bc_ancestors', 'breadcrumb_ancestors', 10, 1);
    

    Is there something I might have missed?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I think that this filter triggers only on child pages (or other hierarchical CPT). Other issue is that your code doesn’t have return (which is required for filter callbacks).

    I think that you already posted similar issue and I’m not sure which was original issue, but it seems that you should use more general filter – wpseo_breadcrumb_links.

    Thread Starter buzut

    (@buzut)

    Thank you for your prompt answer.
    Sure, I had previously asked weather the feature was available in Yoast SEO.
    Then the ticket was closed 😢

    I was trying wpseo_breadcrumb_links on WooCommerce product pages because it’s actually a child (at least I think so): Home > All products > product.

    But can’t have it work. It seems like it’s never called.
    Sure I know filters must return something, but a var_dump is always printed nonetheless.

    I’ve have wpseo_breadcrumb_links a try as you suggest, but I don’t get its return value:

    
    array(3) {
      [0]=>
      array(3) {
        ["text"]=>
        string(7) "Accueil"
        ["url"]=>
        string(17) "http://localhost/"
        ["allow_html"]=>
        bool(true)
      }
      [1]=>
      array(1) {
        ["ptarchive"]=>
        string(7) "product"
      }
      [2]=>
      array(1) {
        ["id"]=>
        int(68)
      }
    }
    

    Here if [1] had the same value as [0] I’d be able to filter it properly and replace text and link to end up with what I need. But unless there is something I’m missing, I don’t really know how I can use this filter to achieve the task at hand : which is modifying the page’s immediate anscestor in the breadcrumb.

    Thank you for your patience 🙈

    “All products” is post type archive which is shop page (for products). I think that’s always bellow home page. You can use unset to remove one element from the array (I can send PHP example if you want to remove shop page from path).

    If you want to include product categories then you should enable it in breadcrumbs settings (SEO -> Search Appearance -> Breadcrumbs -> Taxonomy to show in breadcrumbs for content types ).

    I’m still trying to figure what you exactly want. Maybe I can write PHP example if you send more details.

    Thread Starter buzut

    (@buzut)

    No wayyyy, that’s what I was looking for from the beginning!
    Now I just have to remove “all products” from the ancestors – that I can do with wpseo_breadcrumb_links – and I’m good to go.

    One last question. As a product may be in several categories, is there a way to choose the one that displays in the breadcrumb by default?

    I’d guess that you want something like this when I’ve re-read your both posts… Anyway, it was much easier than we expected.

    It’s possible that you can remove post type archive in the settings without PHP code.

    There is feature “primary term”, but I think that you should install Yoast WooCommerce SEO plugin for this feature on products. I hope that someone from Yoast support will send more details.

    Thread Starter buzut

    (@buzut)

    Thank you for your answer, yes finally it wasn’t this complicated, just needed to know what setting to use 🙂

    I couldn’t find the setting that would remove “all products” from the breadcrumb though.

    Sure, I’d be willing to buy the Yoast WC SEO if it’s more flexible and allows me to chose what “path” I want for each product.

    In the meantime, I’ve been able to remove the “all products” with the filter you had given me.

    
    // Remove all products from breadcrumbs
    function breadcrumb_ancestors($links) {
        $breadcrumb = [];
    
        foreach ($links as $val) {
            if (array_key_exists('ptarchive', $val)) continue;
            array_push($breadcrumb, $val);
        }
        
        return $breadcrumb;
    }
    add_filter('wpseo_breadcrumb_links', 'breadcrumb_ancestors', 10, 1);
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp_seo_get_bc_ancestors hook not working’ is closed to new replies.