Tip for adding schema to a wordpress site with Yoast

1

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.

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!

Member

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 ;-)

Member

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.

1

@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:

Member

Member

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.

1

@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.

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.