• Resolved Till Krüss

    (@tillkruess)


    Hello,

    is there any way to filter the “application/ld+json” output?

    I’ve tried using the wpsso_json_data_https_schema_org_thing and other filters, but the data returned is always NULL. Why is that?!

    My callbacks are definitively called and I’ve tried 1, 10 and 1000 as filter priority.

    Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author JS Morisset

    (@jsmoriss)

    That would be the correct filter name – don’t forget that WPSSO caches it’s meta tags Schema markup, so you’ll need to clear the WPSSO cache to execute the filter.

    ‘wpsso_json_data_https_schema_org_{item_type}’ ( $json_data, $mod, $mt_og, $page_type_id, $is_main )

    See the following pages for more info:

    https://wpsso.com/docs/plugins/wpsso/notes/developer/

    https://wpsso.com/docs/plugins/wpsso-schema-json-ld/notes/developer/

    You may find that using the WPSSO JSON add-on can be useful as well (instead of coding your own filters). ??

    js.

    Thread Starter Till Krüss

    (@tillkruess)

    Could you explain how I’d change for example the in this:

    `
    <script type=”application/ld+json”>{
    “@id”: “https://wordpress.test/#id/website&#8221;,
    “@context”: “https://schema.org&#8221;,
    “@type”: “WebSite”,
    “url”: “https://wordpress.test/&#8221;,
    “name”: “WordPress”,
    “description”: “Just another WordPress.”,
    “potentialAction”: [
    {
    “@context”: “https://schema.org&#8221;,
    “@type”: “SearchAction”,
    “target”: “https://wordpress.test?s={search_term_string}”,
    “query-input”: “required name=search_term_string”
    }
    ]
    }</script>
    `

    I’ve registered these filters:

    ‘wpsso_json_data_https_schema_org_thing’,
    ‘wpsso_json_data_https_schema_org_creativework’,
    ‘wpsso_json_data_https_schema_org_website’,
    ‘wpsso_json_data_https_schema_org_organization’,
    ‘wpsso_json_data_https_schema_org_blog’,

    But all recieve NULL as the first argument.

    Thread Starter Till Krüss

    (@tillkruess)

    Either I’m not using it right, or something is wrong.

    I use DELETE FROM wp_options WHERE option_name LIKE "%_wpsso_%"; between test to flush the cache fully.

    Plugin Author JS Morisset

    (@jsmoriss)

    $json_data is null by default, return null or an array as desired. If an array is returned by the filters, it will be JSON encoded as a json+ld script.

    Don’t forget that filters cascade (except for Thing, all Schema types have parent types).

    If you don’t use the WPSSO JSON add-on, then you’ll have to build your own Schema data array.

    js.

    Thread Starter Till Krüss

    (@tillkruess)

    We use the pro version of the WPSSO JSON add-on.

    Is there any way to override what WPSSO is spitting out, or can I ONLY use the filter to add additional data?

    Plugin Author JS Morisset

    (@jsmoriss)

    The WPSSO JSON add-on hooks filters at priority 10, so make sure you’re hooking using a higher priority to manipulate the output from WPSSO JSON’s filters.

    
    add_filter( 'wpsso_json_data_https_schema_org_website', 'custom_json_data_https_schema_org_website', 20, 5 );
    
    function custom_json_data_https_schema_org_website( $json_data, $mod, $mt_og, $page_type_id, $is_main ) {
        return $json_data;
    }
    

    js.

    • This reply was modified 5 years, 6 months ago by JS Morisset. Reason: Fixed the filter name prefix. :)
    Plugin Author JS Morisset

    (@jsmoriss)

    FYI – If you enable the SSO > Advanced > Add Hidden Debug Messages option, it’ll disable most transient caches so your filter should be executed for every page load. ??

    js.

    Thread Starter Till Krüss

    (@tillkruess)

    Thanks for all the tips, but for some reason, I can’t seem to get any data from WPSSO. This is my code:

    
    <?php
    
    $wpsso_filters = [
        'wpsso_json_data_https_schema_org_thing',
        'wpsso_json_data_https_schema_org_creativework',
        'wpsso_json_data_https_schema_org_website',
        'wpsso_json_data_https_schema_org_organization',
        'wpsso_json_data_https_schema_org_blog',
    ];
    
    foreach ( $wpsso_filters as $filter ) {
        add_filter( $filter, function ( $json_data, $mod, $mt_og, $page_type_id, $is_main ) {
            var_dump($json_data, $mod, $mt_og, $page_type_id, $is_main);
    
            return $json_data;
        }, 99999, 5 );
    }
    

    This is the output I get:

    
    wpsso_json_data_https_schema_org_thing
    NULL
    array(18) {
      snip...
    }
    array(12) {
      snip...
    }
    string(7) "website"
    bool(false)
    wpsso_json_data_https_schema_org_creativework
    NULL
    array(18) {
      snip...
    }
    array(12) {
      snip...
    }
    string(7) "website"
    bool(false)
    wpsso_json_data_https_schema_org_website
    NULL
    array(18) {
      snip...
    }
    array(12) {
      snip...
    }
    string(7) "website"
    bool(false)
    wpsso_json_data_https_schema_org_thing
    NULL
    array(18) {
      snip...
    }
    array(12) {
      snip...
    }
    string(12) "organization"
    bool(false)
    wpsso_json_data_https_schema_org_organization
    NULL
    array(18) {
      snip...
    }
    array(12) {
      snip...
    }
    string(12) "organization"
    bool(false)
    wpsso_json_data_https_schema_org_thing
    NULL
    array(18) {
      snip...
    }
    array(12) {
      snip...
    }
    string(4) "blog"
    bool(true)
    wpsso_json_data_https_schema_org_creativework
    NULL
    array(18) {
      snip...
    }
    array(12) {
      snip...
    }
    string(4) "blog"
    bool(true)
    wpsso_json_data_https_schema_org_blog
    NULL
    array(18) {
      snip...
    }
    array(12) {
      snip...
    }
    string(4) "blog"
    bool(true)
    
    Thread Starter Till Krüss

    (@tillkruess)

    I don’t have any caching plugins.
    I’ve tried 20 and 99999 as priorites.
    WPSSO debug mode is on.

    So I’m getting the filter calls, but your example doesn’t allow me to overwrite values:

    https://wpsso.com/docs/plugins/wpsso-schema-json-ld/notes/developer/filters/examples/assign-a-custom-field-value-to-a-schema-property/

    Thread Starter Till Krüss

    (@tillkruess)

    Nevermind. It seems like the filters are empty unless the JSON/LD plugin is activated…

    Plugin Author JS Morisset

    (@jsmoriss)

    Looks fine to me.

    Try adding the following to your function:

    
    $wpsso =& Wpsso::get_instance();
    $wpsso->debug->log( 'HERE HERE HERE HERE' );
    

    Go to to your home page, force-reload the webpage (to clear the browser’s cached version), then do a “View Source” and search for ‘filter_json_data_https_schema_org_thing’ in the webpage HTML. When the ‘filter_json_data_https_schema_org_thing’ method executes, you should see ‘common json data filter’ begin/end markers in the debug log. Your ‘HERE HERE HERE HERE’ string should be right after that end marker.

    Assuming the “Include WebSite Information for Google Search” option is checked in the SSO > Essential > Google settings, you should also see the ‘filter_json_data_https_schema_org_creativework’ and ‘filter_json_data_https_schema_org_website’ methods being executed (CreativeWork is a sub-type of Thing, and WebSite is a sub-type of CreativeWork).

    js.

    Plugin Author JS Morisset

    (@jsmoriss)

    Nevermind. It seems like the filters are empty unless the JSON/LD plugin is activated…

    That’s what I said earlier:

    $json_data is null by default, return null or an array as desired. If an array is returned by the filters, it will be JSON encoded as a json+ld script.

    Don’t forget that filters cascade (except for Thing, all Schema types have parent types).

    If you don’t use the WPSSO JSON add-on, then you’ll have to build your own Schema data array.

    js.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Filter JSON/LD output’ is closed to new replies.