Filter JSON/LD output
-
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 alwaysNULL
. Why is that?!My callbacks are definitively called and I’ve tried 1, 10 and 1000 as filter priority.
Thanks!
-
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.
Could you explain how I’d change for example the in this:
`
<script type=”application/ld+json”>{
“@id”: “https://wordpress.test/#id/website”,
“@context”: “https://schema.org”,
“@type”: “WebSite”,
“url”: “https://wordpress.test/”,
“name”: “WordPress”,
“description”: “Just another WordPress.”,
“potentialAction”: [
{
“@context”: “https://schema.org”,
“@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.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.$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.
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?
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. :)
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.
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)
I don’t have any caching plugins.
I’ve tried20
and99999
as priorites.
WPSSO debug mode is on.So I’m getting the filter calls, but your example doesn’t allow me to overwrite values:
Nevermind. It seems like the filters are empty unless the JSON/LD plugin is activated…
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.
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.
-
This reply was modified 5 years, 6 months ago by
- The topic ‘Filter JSON/LD output’ is closed to new replies.