sybrew / the-seo-framework

The SEO Framework WordPress plugin.
https://theseoframework.com/
GNU General Public License v3.0
420 stars 46 forks source link

Hide Breadcrumbs for specific taxonomies #267

Closed SimeonGriggs closed 6 years ago

SimeonGriggs commented 6 years ago

I know the Breadcrumbs Schema can be disabled entirely with...

add_filter( 'the_seo_framework_json_breadcrumb_output', '__return_false' );

...but is it possible to filter this to just specific taxonomies?

sybrew commented 6 years ago

Hi @SimeonGriggs

This is possible using a function. See has_category(), which is used to adjust the outcome.

Adjust $terms below to alter the behavior.


add_filter( 'the_seo_framework_json_breadcrumb_output', function( $output = true ) {
    // (int|string|array) (Required) Category ID, name or slug, or array of said.
    $terms = array();

    if ( has_category( $terms ) ) {
        $output = false;
    }

    return $output;
} );
SimeonGriggs commented 6 years ago

Cool thanks. Is it possible though to just filter out a specific category from the breadcrumbs? This code removes all categories, and I can't see a filter that would allow this.

sybrew commented 6 years ago

Hi @SimeonGriggs

Do you mean that this: Home > Category > Sub Category > Page

Becomes this? Home > Sub Category > Page

You can adjust this behavior using the primary category selection tool. This is done via the radio button found next to each selected category: image

If this isn't what you meant, could you give me an example of "what I have... and what I want"?

sybrew commented 6 years ago

[deleted duplicated comment]

Are the selected product categories in this structure?

Main category (wanted, and selected)
   | - Sub category (unwanted, but selected)

Or in this structure?

Main category (unwanted, but selected)
   | - Sub category (wanted, and selected)

If it's the former then you should use the primary term option. If it's the latter then you should indeed use a filter. Although I don't understand why you'd wish to obscure an assigned category.

In any case, this is the filter you'll need: https://github.com/sybrew/the-seo-framework/blob/8ad4ef87ffff1e37b1d022e669aefd42188b0884/inc/classes/generate-ldjson.class.php#L470-L478

The first parameter contains all terms. You should do a foreach loop on the sequential terms until one matches the term IDs (or anything else) you'd like to exclude.

Here's an untested draft:


add_filter( 'the_seo_framework_ld_json_breadcrumb_terms', function( $terms, $post_id, $taxonomy ) {

    if ( 'product_cat' === $taxonomy ) {
        foreach ( $terms as $i => $term ) {
            if ( isset( $term->ID ) && 5 == $term->ID  ) {
                unset( $terms[ $i ] );
            }
        }
    }

    return $terms;
}, 10, 3 );
SimeonGriggs commented 6 years ago

Thanks for the reply. WooCommerce Product Categories aren't nested into parent/child relationships, it's a list of checkboxes. So there's no option to set a Primary Term.

The use case here is we're using one of these Categories to set some specific pricing on the products, but it's not a Category we want publicly available. And Google is picking up the unwanted Category as the Breadcrumb in its index, so I'd like to remove it entirely.

The code you've posted isn't working atm but I'll keep looking at it...

sybrew commented 6 years ago

Hi Simeon,

I do see those primary term selection radio boxes next to the category selections on product pages: image

When they're not listed as parent/child categories, just set the one you wish to have highlighted in Google Search as primary and it should work.

I'm hasting through the 3.0.4 release because of an incorrect assessment, so I have little time to inspect any custom code 😅. If you are unable to get it working by next week I'll take a closer look.

SimeonGriggs commented 6 years ago

Ooooh just realised I wasn't on the latest version of the plugin. Sorry. Okay so I'm seeing those radio buttons now.

...would be nice to globally disable a Taxonomy from ever being used as a Breadcrumb but I understand I'm an edgy edge case.

sybrew commented 6 years ago

Hi Simeon,

I'm glad you've found them 😄 Do those buttons resolve the initial issue?

For exclusion of taxonomies, the the_seo_framework_ld_json_breadcrumb_terms filter would then be helpful. When only the excluded taxonomies are assigned to the post/CPT, the breadcrumbs won't render.

But usage thereof is indeed "edgy edge" 😉.

SimeonGriggs commented 6 years ago

Yeah the Primary Category worked, thanks. And I'll look more into that filter to make it permanent.

sybrew commented 6 years ago

I'm glad it works as intended 😄 Have a great Monday!