Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Så här inaktiverar du JSON REST API i WordPress

Vi fick nyligen en fråga från en läsare som vi ofta hör här: ”Hur inaktiverar jag REST API på min WordPress-webbplats?” Och ärligt talat, det är en befogad fråga.

När WordPress version 4.4 släpptes med JSON REST API öppnade det upp en värld av möjligheter för utvecklare. Många webbplatsägare tycker dock att det är onödigt och oroar sig för de potentiella sårbarheter som det medför.

I den här artikeln visar vi dig hur du enkelt inaktiverar JSON REST API i WordPress.

Disable JSON REST API in WordPress

Varför inaktivera JSON REST API i WordPress?

Det går inte att förneka att API:et har många fördelar för utvecklare av WordPress. API:et gör det superenkelt att hämta data med GET requests, vilket är användbart för dem som bygger appar med WordPress.

Med det sagt kan detta potentiellt öppna din website för en new front av DDoS-attacker. Det kan också vara resurskrävande och göra din WordPress website långsammare.

Att inaktivera JSON REST API liknar att inaktivera XML-RPC, som många site admins inaktiverar på sina WordPress-webbplatser bara för att vara på den säkra sidan.

Vi kommer att visa dig två metoder för att enkelt inaktivera JSON REST API i WordPress. Använd bara snabblänkarna under för att hoppa till den metod du vill använda.

Metod 1. Inaktivera JSON REST API i WordPress med kod (rekommenderas)

Vi rekommenderar att du använder pluginet WPCode för att inaktivera JSON REST API i WordPress.

WPCode

WPCode gör det säkert och enkelt att add to custom code i WordPress, utan att editera functions.php-filen i ditt theme. På så sätt finns det ingen risk för att du gör ett error och förstör din site.

Dessutom kommer det med ett built-in kodbibliotek som innehåller kontrollerade code snippets för populära funktioner som gillar inaktivera REST API, inaktivera XML-RPC, och mycket mer. Detta förhindrar att du behöver installera en massa tillägg för engångsbruk.

För att komma igång måste du installera och aktivera det gratis pluginet WPCode. För steg-för-steg-instruktioner, läs vår guide om hur du installerar ett plugin för WordPress.

Note: Den gratis versionen av WPCode har allt du behöver för att enkelt add to custom code i WordPress. Men om du vill ha avancerade funktioner som gillar ett privat molnbibliotek med snippets, page- och device-specifika snippets, code snippets, revisioner och mer, kan du uppgradera till WPCode Pro.

När pluginet är aktiverat går du till Code Snippets ” Library från din WordPress dashboard.

Sök sedan efter snippet ”Inaktivera WordPress REST API” och klicka på knappen ”Använd snippet”.

Select the Disable WordPress REST API in WPCode

Pluginet lägger sedan automatiskt till koden och väljer rätt metod för insert.

WPCode automatically adds the Disable JSON REST API snippet

All you need to do is toggle the switch from ”Inaktiverad” till ”Aktiv”.

Klicka sedan på knappen ”Update”.

Switch the code snippet to Active and click Update in WPCode

Nu är det klart. Nu är JSON REST API inaktiverat på din WordPress site.

Metod 2. Inaktivera JSON REST API i WordPress med ett plugin

Du kan också enkelt inaktivera JSON REST API med hjälp av ett särskilt plugin.

Det första du behöver göra är att installera och aktivera pluginet Disable REST API. För mer detaljer, se vår steg-för-steg guide om hur du installerar ett plugin för WordPress.

Pluginet fungerar direkt från boxen och det finns inga inställningar som du behöver konfigurera.

Vid aktivering kommer pluginet att tvinga tillbaka ett autentiseringsfel till alla API-begäranden från källor som inte är inloggade på din webbplats.

Detta kommer effektivt att förhindra att obehöriga requests använder REST API för att hämta information från your website.

Du kan testa detta genom att besöka http://example.com/wp-json page. Se till att du först loggar ut från WordPress admin area eller växlar din webbläsare till inkognitoläge.

Glöm inte att ersätta example.com med ditt eget domain name. Du kommer att se detta message, som indikerar att REST API requests är blockerade.

REST API Disabled

Alltså har du utan problem inaktiverat obehöriga REST API requests på din WordPress site.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du inaktiverar JSON API i WordPress. Säkerhetsmedvetna användare kanske också vill kolla in dessa tips om hur man skyddar WordPress adminområde eller se våra förväntade val av de bästa WordPress backup-pluginsen.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

41 kommentarerLämna ett svar

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Dayo Olobayo says

    I appreciate the clarity of this tutorial on disabling the JSON REST API in WordPress. However, I wonder if completely disabling the API is the best approach considering its potential benefits for website functionality and integration with other services.

    • WPBeginner Support says

      It would depend on the site owner for if it would be best for their needs or not or if they’ve run into an issue with using it.

      Administratör

      • Dayo Olobayo says

        Thanks. I guess it’s essential to weigh the potential security risks against the benefits of the JSON REST API by considering owner’s unique needs and circumstances.

    • Jiří Vaněk says

      Hi Dayo Olobayo,
      The question is more about whether you use this API or not. It’s the same as with XML-RPC. I don’t use it on my website, so I deactivated it. Of course, this means I can’t connect to the website using, for example, the WordPress mobile app, which uses XML-RPC to connect. It is always a compromise between what you want to disable and what you want to allow. Any API that accepts external requests can be a potential risk, and it’s simply a matter of whether you want to use it or not.

  3. RJW says

    I’d try the disable plugin in Dev environment before production, I found installing it broke some features

    • WPBeginner Support says

      If you have many different plugins and tools and have a testing environment then it would definitely be good to test new plugins and how they interact with your site.

      Administratör

  4. Ricky says

    Similar to an above commenter, I’ve noticed the ”wp-json” request when using Pingdom and other testing sites. Unfortunately, mine takes over 10 seconds (Yes really!!) to load it. This pushes my overall website load time and I can’t figure out how to fix this. The plugin doesn’t change it at all. Any suggestions?

  5. Janice says

    How do I know if I actually have JSON API on my website?
    This article about removing it is good – if I need it – but I have often been alarmed by certain warnings only to find that they’ve not even applicable to me.

  6. Logan Cale says

    I hate adding yet another plugin to do a simple tasks, and I found that we can disable this functionality by adding the following code snippet to the functions.php file.

    add_filter(’rest_enabled’, ’_return_false’);
    add_filter(’rest_jsonp_enabled’, ’_return_false’);

  7. J.L. says

    Is this just for self hosted blogs or including free blog sites? Per mostly all plug ins…etc are done for you when you’re not self hosted

    PS I’m not subscribing…just want a response

  8. Elaine says

    How can I check if my site has Rest API. I turned off a lot of extras when I first set it up but now don’t know where to look to see if it’s there. Not keen to download a plugin unnecessarily.

    • WPBeginner Support says

      Hey Elaine,

      You can check if rest is api is enabled on your site by visiting the url like this example.com/wp-json. Make sure you are signed out of WordPress before doing that. If you see lots of information in plain text, then this means REST API is enabled on your site. Follow the above instructions to turn it off

      Administratör

  9. Ken Dowling says

    Is disabling REST API suitable for e-commerce sites such as WooCommerce? My understanding is that WooCommerce uses REST-API quite a bit.

    Further, my buyers do not have to login to buy, so what happens to the transaction when a REST API call is rejected?

    Regards, Ken

  10. D. Joe Chaffin says

    Plug-in makes no difference for me in WP 4.7.2. With the plugin activated or disabled, the example.com/wp-json (with my domain replacing ”example”) pages gives a massive list of settings for my site.

    • D. Joe Chaffin says

      Hmmmm. Now that I look at it, I only see the list in Safari, while Chrome and Firefox for Mac show the expected message specified in this post.

    • WPBeginner Support says

      Hi,

      Make sure you are logged out of WordPress admin area or use incognito mode before testing the example.com/wp-json page. The plugin disables access to the page only for unauthorized users. As an administrator you will still be able to see it.

      Administratör

  11. Audra Carpenter says

    Hey Guys,

    First off thanks so much for what you do! I’ve learned so much about WordPress from you and sent a ton of folks your way!!

    Ok, I installed the plugin, but I am not seeing what you suggest above? I have a full screen of information…?

    Thoughts?

    Thanks!

  12. Treasure says

    I followed these steps, but when I went to check it with the example etc., I got 2 pages of code, not the response showed above. Hmmm, don’t know what to do.

  13. Doug Nix says

    When I ran the test I think it failed, as I got a boatload of data on screen. Any idea what might have not worked? I installed the plugin as described…

    • Doug Nix says

      Works perfectly when I check using an incognito window. Thanks for the explanation regarding authorised vs unauthorised or anonymous users.

  14. Karl says

    Thanks for the tip as well as for the hook to disable XMLRPC.
    Is there a chance for a filter hook for the REST API as well?

  15. Andrew says

    When I check the speed of my site using pingdom.com, the first html entry that tried to load shows the link as mydomain.com/wp-json and it has over 2 seconds of ”wait” time. Is this the same as what this article is talking about? I’m hesitant to simply disable it since I would assume it will be used in the future. Any idea why it would add a 2+ second delay to loading anything on the page?

  16. Stephen Cronin says

    Hmm, the REST API is going to become the standard way for plugins and themes to make Ajax calls back to the server from the front end, replacing admin-ajax, so I wouldn’t be disabling it… Hopefully they will fail gracefully, but you will almost end up missing some functionality.

    Also, if you really want to protect against DDoS attacks, you better disable html as well! ;)

    • reza says

      ver 50,000 WordPress websites have been hacked due to a major security vulnerability that was discovered in the WordPress REST API.

      • Jim S Smith says

        OUCH!

        That’s disturbing to know. I have noticed a LOT of access attempts in my site’s logs.

        What’s more,

        I think the folks at WordPress could have done a little better in letting the users decide how much, if at all, they want the REST API exposed.

        Again,

        More of this, ”The developers know MORE about the user’s needs than the user does!” – I also was not too happy about being forced to support EMOJI and remote-loaded fonts from fonts.google.com, even though my sites do not use them!

        The REST API may be a boon for (some) actual web-application developers, but what about the rest of us who will not very likely use this? ? ?

  17. Kasey says

    How likely is it that a plugin will be using this functionality. Just for example would contact forms be utilizing this? Not keen on turning it off in case it breaks anything.

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.