Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yoast-provided filter for disabling Yoast breadcrumb schema output not working properly #16711

Closed
2 of 9 tasks
wpttb opened this issue Feb 26, 2021 · 4 comments
Closed
2 of 9 tasks
Labels
Yoast: SEO Features Yoast Feature

Comments

@wpttb
Copy link

wpttb commented Feb 26, 2021

  • I've read and understood the contribution guidelines.
  • I've searched for any related issues and avoided creating a duplicate issue.

Please give us a description of what happened.

Since Yoast 15.8, breadcrumb schema output is included on all pages, even if you’re not using the Yoast breadcrumb function – and we’re not.

There’s an example from Yoast here of how to exclude just the breadcrumb schema output:

https://developer.yoast.com/features/schema/api/#disable-schema

The filter provided is:

// functions.php
add_filter( 'wpseo_schema_graph_pieces', 'remove_breadcrumbs_from_schema', 11, 2 );

/**
 * Removes the breadcrumb graph pieces from the schema collector.
 *
 * @param array  $pieces  The current graph pieces.
 * @param string $context The current context.
 *
 * @return array The remaining graph pieces.
 */
function remove_breadcrumbs_from_schema( $pieces, $context ) {
    return \array_filter( $pieces, function( $piece ) {
        return ! $piece instanceof \Yoast\WP\SEO\Generators\Schema\Breadcrumb;
    } );
}

When implemented in functions.php this removes some of the breadcrumb schema, but there is still a trace left e.g.

,"breadcrumb":{"@id":"https://www.example.com/folder/page/#breadcrumb"},

This exists in the Yoast generated schema, it’s not being created by anything else, and as it’s incorrect, it also triggers an error in Google’s Structured Data Testing Tool.

Please describe what you expected to happen and why.

I expected this to exclude the breadcrumb graph pieces from showing up in the schema output as that filter's comments state "Removes the breadcrumb graph pieces from the schema collector".

How can we reproduce this behavior?

  1. Add the filter to functions.php.
  2. Load a page
  3. View the Yoast schema graph output, search for breadcrumb - it will be towards the end.

Technical info

  • If relevant, which editor is affected (or editors):
  • Classic Editor
  • Gutenberg
  • Classic Editor plugin
  • Which browser is affected (or browsers):
  • Chrome
  • Firefox
  • Safari
  • Other

Used versions

  • WordPress version: 5.6.2
  • Yoast SEO version: 15.9
  • Gutenberg plugin version:
  • Classic Editor plugin version:
  • Relevant plugins in case of a bug:
  • Tested with theme: Twenty Twenty-One
@MariekeB-Yoast
Copy link

I can confirm this bug. After applying the filter, it outputs Breadcrumbs schema leftovers, it seems at every page (which isn't unexpected).

@MariekeB-Yoast MariekeB-Yoast added Yoast: Built-in tools Yoast Feature Yoast: SEO Features Yoast Feature and removed Yoast: Built-in tools Yoast Feature labels Feb 26, 2021
@Djennez
Copy link
Member

Djennez commented Feb 26, 2021

The filter seems to work correctly: it removes the breadcrumb piece from the schema output.

What it does not do is remove the reference to the breadcrumb. That is what you're seeing. This can be removed by, for example, this snippet:

add_filter( 'wpseo_schema_webpage', 'remove_breadcrumb_schema_ref', 10, 1);

function remove_breadcrumb_schema_ref( $piece ){
	unset( $piece['breadcrumb'] );
	return $piece;
}

This might need some tweaking with different sites, but that's the nature of filters; Don't blindly copy-paste.

Closing this issue as not applicable.

@pindiespace
Copy link

pindiespace commented May 22, 2021

I have a similar problem trying to conditionally remove graph pieces. Is there an alternative to checking if plugins are loaded?

====================================
Currently, I'm loading my custom schema classes like this:

if ( is_plugin_active( YOAST_PLUGIN ) ) {

    add_action( 'plugins_loaded', function () {

                // This works...you can add an Event schema
               //  ...and you can check the global $post type to do conditional tests**

                if ( ! class_exists( 'WPSEO_Schema_Event' ) ) {
                    require_once PLYO_SCHEMA_EXTENDER_PATH . '/classes/WPSEO_Schema_Event.php';
                }
                $pieces[] = new WPSEO_Schema_Event( $context, 'Event' );

              // But you CANNOT remove graph pieces inside the function - this fails!

              add_filter( 'wpseo_schema_graph_pieces', 'remove_breadcrumbs_from_schema', 11, 2 );
             add_filter( 'wpseo_schema_webpage', 'remove_breadcrumb_schema_ref', 10, 1);

    } );  // is_plugin_active end

   // BUT...if you try to remove pieces out here, it WORKS
   // ...but global $post is NOT available yet, so yo can't do conditional checks on the post**

   add_filter( 'wpseo_schema_graph_pieces', 'remove_breadcrumbs_from_schema', 11, 2 );
   add_filter( 'wpseo_schema_webpage', 'remove_breadcrumb_schema_ref', 10, 1);

   /**
    * Remove Yoast Pieces from the graph
    * 
    * @param array                 $pieces graph pieces to output.
    * @param \WPSEO_Schema_Context $context Yoast object with context variables.
    */
   function remove_breadcrumbs_from_schema ( $pieces, $context ) {
       return \array_filter( $pieces, function( $piece ) {
           return ! $piece instanceof \Yoast\WP\SEO\Generators\Schema\Breadcrumb;
       } );
   }

   function remove_breadcrumb_schema_ref ( $pieces ) {
       unset( $pieces['breadcrumb'] );
       return $pieces;
   }

So, you can't remove pieces after plugins are loaded and the plugins_loaded event has fired?

@mdotk
Copy link

mdotk commented Jul 12, 2022

The filter seems to work correctly: it removes the breadcrumb piece from the schema output.

What it does not do is remove the reference to the breadcrumb. That is what you're seeing. This can be removed by, for example, this snippet:

add_filter( 'wpseo_schema_webpage', 'remove_breadcrumb_schema_ref', 10, 1);

function remove_breadcrumb_schema_ref( $piece ){
	unset( $piece['breadcrumb'] );
	return $piece;
}

This might need some tweaking with different sites, but that's the nature of filters; Don't blindly copy-paste.

Closing this issue as not applicable.

@Djennez If someone wants to disable Yoast breadcrumbs completely, why not remove the reference as well? What's the point of a reference to nothing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Yoast: SEO Features Yoast Feature
Projects
None yet
Development

No branches or pull requests

5 participants