• I’m using the code from https://schema.press/docs/extend-schema-output/ in my functions and it isn’t modifying the schema output.

    In fact, if I remove conditionals and just use the filter to echo a string the string is not echoed; it’s like the filter isn’t being applied at all.

    The plugin is working as expected and the standard schema json is present in the page source. Example page

    I do know the PHP is being loaded because if I malform the PHP the page throws a PHP error. The PHP is loaded but filter not applied.

    I’ve tried using the code verbatim and also modified it for my testing. Here’s the modified version:

    add_filter( 'schema_output', 'to4_modify_schema' );
    
    function to4_modify_schema( $schema ) {
    
    	global $post;
    
      // Test debug. 'Foobar' is not present in the markup.
      echo '<pre> foobar'; var_dump( $schema ); echo '</pre>';
    
      if ( empty( $schema ) ) return;
    
    	// Debug
    	echo '<pre>'; print_r( $schema ); echo '</pre>';
    
    	// Modify NewType to present the actual schema.org type
    	// if ( $schema['@type'] != 'NewsArticle' ) return $schema;
    
    	// Modify / Override values
    	$schema['title'] = 'Foobar';
    
    	// Add new values
    	// $schema['name'] = 'Event Name';
    
    	// Unset extras
    	unset( $schema['author'] );
    	// unset($schema['ArticleSection']);
    
    	return $schema;
    }

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Hesham Zebida

    (@hishaman)

    I think the reason why the filter isn’t working is that Schema plugin still reads the old data which is saved in post meta, or maybe your pages are cached.

    Can you try saving the plugin settings once, then check if this solves the issue. Also, if you are running a cache plugin try delete cache.

    Thread Starter slamorte

    (@slamorte)

    Thanks for the quick reply.

    It doesn’t work on my localhost (MAMP server, Webpack, yarn) which doesn’t do any caching.

    I have saved the plugin settings, upgraded from 1.6.x to 1.7, and turning Use Yoast Schema off and on, thinking there might be a conflict.

    Oddly enough it does seem to work when I publish the code to my production site. I pushed only unset( $schema['author'] ); code to the production server and the author field does seem suppressed on this article page.

    This makes it hard to develop and test customizations, but at least it works.

    Any thoughts on why it wouldn’t work locally?

    • This reply was modified 6 years, 7 months ago by slamorte.
    Thread Starter slamorte

    (@slamorte)

    Hi again,

    Yes, it’s the post_meta.

    I checked the post_meta field and the custom schema is written to post_meta the first time the schema modify code is used. If the modification code uses $schema['author'] = 'foo', then "Author":"Foo" is written to post_meta.

    However, modifying the code and reloading the page does not rewrite the post_meta field. Schema is pulling the post_meta version of the schema rather than rebuilding it from the updated code. If I update the code to $schema['author'] = 'bar', and reload the page, "Author":"Foo" is still displayed.

    I can delete the post_meta entry and reload the page and see the updated results, but this make testing and development rather slow.

    The modification code may need an additional function that removes the post_meta or expires the post_meta (I see there is a _schema_json_timestamp field which I assume is used to expire the _schema_json)

    Plugin Author Hesham Zebida

    (@hishaman)

    I understand what you mean.. Yes, the saved _schema_json post meta value expires after 24 hours.

    Also it got deleted on some occasions, example:

    – If you updated the post.
    – If a new comment is submitted on a post.
    – If you update Schema -> Type.
    – If you updated the plugin settings.

    So, maybe update the post or plugin settings will come more handy when developing.

    Thread Starter slamorte

    (@slamorte)

    Thanks Hesham!

    Those are some good workaround to this issue. Now that I know what the issue is I can probably figure out other workarounds.

    If I could request a feature it would be for a function that forces schema updates sitewide during development. Something like:

    force_schema_cache_rebuild()

    Or optionally force it on one post:

    force_schema_cache_rebuild( post_id )

    There is a WordPress function that would work for a single post at ID 1234:

    delete_post_meta( 1234, '_schema_json_timestamp' );

    A variant of that might work for a more general schema cache clearing:

    function force_schema_cache_rebuild {
      $post_id = get_the_ID();
      if( ! empty( $post_id ) {
        delete_post_meta( get_the_ID(), '_schema_json_timestamp' );
      }
    }

    But I have not tested this.

    I just finished writing code that populates $schema['author'] with multiple authors from an ACF relationship field. It works great but took ages to test without a schema cache clear during the 20 or so revisions it took me to get it rights.

    Thanks again for a great plugin!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Modify/Extend code not working’ is closed to new replies.