More threads by James Watt

Joined
Oct 25, 2013
Messages
752
Reaction score
285
By tip, I mean removing the JSON-LD schema in Yoast so that when you add yours, you don't have two. I'm convinced that Google doesn't get confused when they see two markups (many sites have it) but still, it's easy to disable Yoast's schema, so you might as well.

Yoast has no UI button to get rid of schema, but they do have a hook in the API you can call. Go to the functions.php file in your theme, and add this line of code before the ?> at the end of the file.

EDIT: I previously had only part of the code needed, this is the updated, full code.

PHP:
function remove_json_ld_output( $data ) { 
 $data = array(); 

 return $data; 
} 

add_filter('wpseo_json_ld_output', 'remove_json_ld_output', 10, 1);

As always, if you aren't sure what you're doing, make sure you save a backup. Hosing one of the core theme files is never a good thing, and you can easily break your whole site temporarily just by having a ; in the wrong place.

To test it, refresh a page on your site, view source and do a ctr+f looking for "json", and you shouldn't see the Yoast schema anymore.

Fixing it this way will last even if you update the Yoast plugin, so do this instead of changing code in the actual plugin file.

Is this already common knowledge? Do you bother removing Yoast's schema when you add proper schema for a client? Did this work for you? Hope it helped!
 
Last edited:
Hey James,

Nope. Not common knowledge to me anyway. I began wondering about how to remove Yoast's schema, while adding my own schema mashup via Tag Manager and then validating. I noticed two sets of schema, with mine being the more accurate of the two ;-)

Anyway, your filter worked great, thank you!
 
Thanks, James. I've started using All-in-one SEO more because they include an option to disable schema output.
 
Interesting. Had no idea Yoast forces you to use their schema? It's not on any of our sites that I'm aware of?
 
Thanks for sharing this, James. We add our JSON plugin to all of our client's sites and we use Yoast religiously but I have never seen Yoast's schema when we check the structured data. I will definitely keep an eye out from now on since you brought it up.
 
Hey James,

A few days ago when I first added the code, things were working great, however, I just checked-back on the site, and now I am getting the following Warning in the browser:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'remove_json_ld_output' not found or invalid function name in /wp-includes/plugin.php

Any idea how to resolve this?

Thanks!

Dino
 
James - Thanks for the useful tip. I tried this on a staging site and it removed the Yoast Schema successfully. While I use Wordpress, I usually avoid making modifications to the function.php file.

My main question is, if there is an update to Wordpress and/or the theme in use, how likely would it be that this modification to the function file would cause an issue?

Thanks
 
@Tim Sweeny - yeah, you need a little knowledge of what's going on under the hood before the function file can feel safe to use, but for a lot of features and changes there's no way around it. Even something as basic as setting up a child theme requires editing the function.php file. For this change specifically, there's basically two things that are going on.

First, wordpress has something called 'hooks'. It lets developers create ways for you to change their plugin/theme functionality, without having to change the core files themselves. They create a hook that gets called right before a certain action, and let you feed in functions you want to be executed before that action is run. You can use hooks to edit the email before Contact Form 7 sends it out, or in this case, Yoast has a hook for the JSON-LD output, giving you a chance to change, extend, or in our case, remove the code before it gets put on your site.

Wordpress will never get rid of hooks, the add_filter function will always be there. Even if they go in a different direction for future versions of wordpress, they'll still keep add_filter as a deprecated function. It's used extensively all over the place, probably in almost every single plugin you've ever used. If add_filter is ever removed as a function you can call, it'd break an enormous number of plugins and themes.

Having action hooks and filters isn't unique to wordpress either, I have a background in more traditional software engineering, and it's a programming concept that's been used for decades in all kinds of applications, from Drupal (a CMS competitor to wordpress) to videogame physics APIs. It's basically used anywhere that a developer wants to let you extend their code, without actually giving you access to the code you're altering.

So if the add_filter function is always going to be there, there's only one other thing that could change in a future update. Yoast could stop using the wpseo_json_ld_output hook. If you try and add_filter on a hook that hasn't been defined though, nothing happens. There's no error or anything, your code just doesn't change anything if there's no filter defined to add your function to.

Hopefully all that made some kind of sense. The tl;dr version though, if this code breaks in the future, all it will do is stop removing yoast's JSON code, it won't actually break anything.

@seoWest:

Looking at my top post, it looks like I somehow only added part of the code you need, I can't believe I did that. Sorry for the misunderstanding. Basically the line I had in my original post registers the function you want to be called with Yoast's JSON hook, but you still have to define that new function. Here's the full code I meant to paste in originally:

PHP:
function remove_json_ld_output( $data ) {
 $data = array();

 return $data;
}

add_filter('wpseo_json_ld_output', 'remove_json_ld_output', 10, 1);

If you'd like to use that code again without the warning this time, this is the full code block you'll need.
 
James - Thanks! Since I'm more of a marketer than designer/programmer I try to tread lightly when it comes to modifying something such as the functions file. Making backups, trying it on a staging site has so far saved me from a major issues. Thanks again.
 
To answer your other question, Tim, any edits to core Wordpress files will indeed be overwritten the next time Wordpress updates.
 
To answer your other question, Tim, any edits to core Wordpress files will indeed be overwritten the next time Wordpress updates.

There are, however, some Wordpress plugins that allow you to insert scripts into the < head > section of your Wordpress plugins.

That way, your additions would not be overwritten when Wordpress updates.

This may be a way to do this without having to edit core files? (I haven't tried this.)

See https://wordpress.org/plugins/search.php?type=term&q=head+section
 
@Tim, yeah, I know more than the average bear maybe, but I'm no seasoned developer. Sometimes I know just enough to get myself into trouble... and even the most seasoned developers should all still use staging sites and testing. My background's in videogame programming originally, and I could tell you some stories about just how important testing is in a big team environment. I know a guy back in college working at a game company that had several days where no one could do work, always because someone committed some broken code at 1am the night before and hosed everything. The next day you'd have everyone sitting around, while one guy was furiously trying to fix what he'd accidentally broken. Testing, no matter your skill, is always important, especially on live client sites.

As far as editing the core wordpress files, and changing the <head> and such... yeah, never edit any wordpress core files. You should never edit any plugin files either, or any theme that you think might be updated someday. Child themes are there in fact so that you can make changes without risking them getting overwritten next time the theme updates. As for adding stuff to the head section, usually I'm working in a custom theme, and you just change things in the header php file, but if you don't have that set up, a plugin might be the way to go.
 
Last edited:
Hey James,

A few days ago when I first added the code, things were working great, however, I just checked-back on the site, and now I am getting the following Warning in the browser:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'remove_json_ld_output' not found or invalid function name in /wp-includes/plugin.php

Any idea how to resolve this?

Thanks!

Dino

Check for extra spaces or missing brackets/missing elements. Simple errors like that in the php files will crash the site.

Always use a child theme to make custom changes like this to a site, otherwise everything you do will be overwritten (as others have pointed out).
 
Check for extra spaces or missing brackets/missing elements. Simple errors like that in the php files will crash the site.

Always use a child theme to make custom changes like this to a site, otherwise everything you do will be overwritten (as others have pointed out).

missing semi colons and such can definitely cause problems. In this case though, it's just because the remove_json_ld_output function wasn't defined. I updated my code snippet, it should work fine now.
 

Login / Register

Already a member?   LOG IN
Not a member yet?   REGISTER

LocalU Event

  Promoted Posts

New advertising option: A review of your product or service posted by a Sterling Sky employee. This will also be shared on the Sterling Sky & LSF Twitter accounts, our Facebook group, LinkedIn, and both newsletters. More...
Top Bottom