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 lägger du enkelt till Customize-kod i WordPress (utan att förstöra din site)

Du hittar ofta WordPress-handledningar, även våra egna här på WPBeginner, som ber dig att lägga till anpassade kodsnuttar i ditt temas functions.php-fil eller i ett webbplatsspecifikt plugin. Vi förstår att detta kan vara skrämmande, särskilt om du inte är bekväm med kod.

Och du har rätt att vara försiktig. Även ett litet misstag i koden kan förstöra hela din webbplats.

I den här artikeln visar vi dig ett enkelt sätt att lägga till anpassad kod i WordPress utan att förstöra din webbplats. Den här metoden är perfekt för både nybörjare och erfarna användare.

How to easily add custom code in WordPress

Problemet med Custom Code Snippets (och hur man Fixar Det)

Ofta hittar du code snippets i WordPress tutorials med instruktioner för att add dem till ditt temas functions.php-fil eller ett site-specifikt plugin.

Det största problemet är att även ett litet misstag i det customize code snippet kan förstöra din WordPress site och göra den otillgänglig.

För att inte nämna, om du uppdaterar eller ändrar ditt WordPress-tema, så tas alla dina customizes bort.

Det andra problemet är att om du addar flera code snippets i ett site-specifikt plugin kan det bli svårt att hantera filen.

Lyckligtvis finns det ett enklare sätt för användare att add to och hantera custom code snippets i WordPress.

WPCode är det mest populära pluginet för code snippets som används av över 2 miljoner WordPress-webbplatser. Det gör det enkelt att add code snippets i WordPress utan att behöva editera functions.php-filen i your theme.

WPCode - Best WordPress Code Snippets Plugin

WPCode gör det också enkelt att add to tracking-koder för Google Analytics, Facebook Pixel, Google Adsense med mera till din sites header och footer areas.

Du behöver aldrig oroa dig för att din site ska gå sönder eftersom den smarta bekräftelsen av code snippet hjälper dig att förhindra vanliga error i koden.

Dessutom kommer WPCode med ett inbyggt snippets-bibliotek där du kan hitta alla de mest populära WordPress-kodsnippets som att tillåta SVG-filuppladdningar, inaktivera REST API, inaktivera kommentarer, inaktivera Gutenberg, ta bort och mycket mer.

Detta eliminerar behovet av att installera separata plugins för varje funktionsbegäran.

Det bästa är att du kan hantera alla dina code snippets från en central vy och add to taggar för att organisera dem.

Det finns också en gratis version av WPCode som har alla de grundläggande funktioner du behöver för att add to custom code i WordPress, inklusive support för 6 code types, smart villkorlig logik, med mera.

Med det sagt, låt oss ta en titt på hur du enkelt kan add to custom code snippets i WordPress med WPCode.

Video Tutorial

Subscribe to WPBeginner

Om du föredrar skriftliga instruktioner är det bara att fortsätta läsa.

Lägga till Custom Code Snippets i WordPress

Det första du behöver göra är att installera och aktivera det gratis pluginet WPCode på din website. För mer detaljer, se vår steg-för-steg guide om hur du installerar ett WordPress plugin.

Note: Det gratis pluginet WPCode har allt du behöver för att add to custom code i WordPress. Om du vill ha avancerade funktioner som gillar tidsinställda snippets, code revisions, eCommerce konvertering tracking, och mer, kan du uppgradera till WPCode Pro.

Vid aktivering kommer plugin att lägga till ett nytt menu-item märkt ”Code Snippets” till din WordPress admin bar. Om du klickar på det visas en lista över alla de custom code snippets som du har saved på din site.

Eftersom du just installerat tillägget kommer din lista att vara tom.

Gå vidare och klicka på knappen ”Add New” för att lägga till ditt första custom code snippet i WordPress.

Click 'Add New' in WPCode to create a new custom snippet

Då kommer du till sidan ”Add Snippet”. Här kan du välja ett code snippet från det färdiga biblioteket eller add to din custom code.

För att add to custom code klickar du på knappen ”Use snippet” under alternativet ”Add Your Custom Code (New Snippet)”.

Add custom code in WPCode

Du måste börja med att ange en titel för ditt custom code snippet. Det kan vara vad som helst som hjälper dig att ID:a koden.

Därefter kan du copy and paste ditt code snippet i Code Preview boxen. Se till att du också väljer korrekt code type i rullgardinsmenyn till höger.

Adding your first code snippet

I screenshot ovan har vi lagt till ett custom code snippet för att ta bort WordPress versionsnummer från vår test site.

function wpb_remove_version() {
return '';
}
add_filter('the_generator', 'wpb_remove_version');

Under kodboxen ser du alternativ för infogning. Det finns två huvudsakliga alternativ för inserter: Auto Insert och Shortcode.

Choose insertion option for code snippet

Om du väljer metoden ”Auto Insert” kommer snippet automatiskt att infogas och köras på din site.

Du kan automatiskt runna snippet endast i WordPress admin area, on the front end av din site, eller överallt. Du kan även välja att inserta ditt snippet på page-specifika locations som före eller efter posts eller inom post content.

Automatically insert code snippets in page-specific locations

Om du är osäker väljer du standardalternativet ”Run snippet everywhere” (kör snippet överallt).

Med metoden ”Shortcode” infogas snippet inte automatiskt. När du har saved snippet får du en shortcode som är specifik för snippet som du kan använda var som helst på din site.

När du rullar längre ner kan du limit var du vill att snippet ska hämtas efter typ av device. Som standard hämtas snippets på alla typer av enheter, men you kan också ställa in att de bara ska hämtas på skrivbordet eller bara på mobilen.

Choose device type for your custom code snippets

Därefter kan du använda den kraftfulla ”Smart Conditional Logic” section för att antingen visa eller dölja automatiskt infogade snippor baserat på en uppsättning regler.

Du kan till exempel visa code snippets endast för inloggade användare, hämta code snippets endast på specifika pages URL:er med mera.

Use smart conditional logic to show or hide snippets

Slutligen kommer du att se ett area för ”Basic info”. Här kan du add to vad som helst som hjälper dig att förstå vad koden gör, var du hittade den och varför du lägger till den på din website.

Add code description and tags

Du kan också tilldela taggar till ditt code snippet. Detta hjälper dig att sortera dina code snippets efter ämne och funktionalitet.

The priority field allows you to control the order in which the snippets are executed when you want to display multiple snippets in the same location. Som standard får alla snippor prioriteten 10. Om du vill att ett snippet ska visas tidigare än andra ställer du bara in snippets prioritet på ett lägre tal, gillar 5.

När du är slutförd med att välja alternativ, toggle omkopplaren från ”Inaktiverad” till ”Aktiv” i det övre högra hörnet och klicka sedan på knappen ”Save Snippet”.

Save and activate code snippet

Om du vill spara code snippet och ej aktivera det klickar du bara på knappen ”Save Snippet”.

När du har saved och aktiverat code snippet kommer det att add to din site automatiskt, om det är den infogningsmetod du valt, eller displayed som en shortcode.

Hantera error i customize-kod

Ofta, om du gör ett misstag när du lägger till den anpassade koden i din webbplatsspecifika plugin- eller theme-fil, skulle det omedelbart göra din webbplats otillgänglig.

You would start seeing a syntax error or a 500 internal server error on your site. För att fixa detta måste du ångra din kod manuellt med hjälp av en FTP-klient.

Det fina med WPCode-tillägget är att det automatiskt upptäcker ett syntax error i koden och omedelbart deactivate den.

Error handling in your custom code snippet

Det kommer också att visa dig ett användbart felmeddelande, så att du kan debugga felet.

WPCodes smarta code snippet-validering kommer också att upptäcka eventuella error när du lägger till din custom code.

Smart code snippet validation to find code errors

Om du hoverar över error:et visas instruktioner som hjälper dig att fixa det.

Hantering av dina custom code snippets

WPCode plugin ger ett enkelt användargränssnitt för att hantera dina custom code snippets i WordPress.

Du kan save code snippets utan att aktivera dem på din site, och sedan aktivera eller deactivate snippet när du vill. Det är också möjligt att filtrera code snippets efter typ och location, och använda tags för att organisera dina code snippets på ett enkelt sätt.

WPCode - WordPress Snippets Organized by Tags

Du kan också exportera specifika code snippets eller bulkexportera alltihop.

Gå bara till Code Snippets Tools och click på tabben ’Exportera’.

Export your custom code snippets

Om du flyttar webbplatser till en annan server kan du enkelt importera dina code snippets till den nya site.

Besök bara Code Snippets ” Tools Import page och ladda upp exportfilen.

Import code snippets with WPCode

Bonus: Generera anpassade code snippets med AI

Även om du inte kan koda kan du enkelt skapa egna snippets för din WordPress-webbplats med hjälp av WPCodes AI Snippet Generator. Så istället för att kämpa med PHP, HTML eller CSS kan du beskriva vad du vill ha på din webbplats på vanlig engelska.

Från sidan ”Add Snippet” hoverar du över alternativet ”Generera snippet med AI”. Klicka sedan på knappen ”+ Generate Snippet”.

Click on the 'Generate snippet using AI' option

Därefter kommer en text box att dyka upp där du kan beskriva vad du vill att ditt snippet ska göra. Försök att vara specifik om vad du vill, men gör det inte för långt, du kan alltid använda funktionen AI Improve för att göra ytterligare ändringar senare.

När du har skrivit din request klickar du på knappen ”Generate”.

Describe what you want your snippet to do and click the Generate button

AI kommer nu att skriva your new code snippet, samt select the auto-insert location och ställa in villkorlig logik, om det behövs.

WPCode AI will automatically write the code

Om du vill justera ditt nya snippet kan du clicka på knappen ”AI Improve”, som låter dig beskriva de ändringar du vill göra.

Use the AI Improve button to adjust your new snippet

Du kan också använda funktionen AI Improve för att förbättra något av dina befintliga code snippets.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du enkelt lägger till custom code i WordPress. Vill du experimentera med några code snippets på din website? Kontrollera vår lista över extremt användbara knep för WordPress functions-filen, och glöm inte att se vår ultimata guide för att snabba upp din WordPress site.

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

25 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. Moinuddin Waheed says

    I have always had reservation for installing plugins for every functionality even very small functionality.
    inserting code snippets through wpcode has revolunized it as we don’t need to have separate plugins for every small functionality that we need to have.
    The better part is that wpcode comes with most used general code snippets which we have to just use and activate.

  3. Jiří Vaněk says

    I also use WP code for inserting snippets on the website, and I agree that it’s one of the indispensable tools in website creation if someone wants to save on installing numerous plugins, tasks that snippets can often handle well. What’s also good is that artificial intelligence can now suggest tailored snippets for many purposes.

  4. ebere akadonye says

    I have tried using the plugin, but whenever I am trying to activate my code, it returns an error in Line 0, I don’t know what to do next, because I can’t find any line 0. except it is the line with the <?php

  5. Elena Ocone says

    Hi and thank you so much for all your work and support to the WordPress community.

    I used Code Snippets plugin and it was just perfect until after last update, when all code disappeared, and it became impossible to save any change: the result is a 404 error page.

  6. Carol says

    I have a long Authors page with affiliate links and want to have users be able to pull up those books for certain topics.

    For instance, say that they are books covering Gardening, Cooking, and Sewing. I want the user to be able to pull up all the books on Sewing without having to see all the other titles. Plus some titles do not tell you immediately the subjects so browsing the entire list might not catch them all.

    I want to be able to code the individual titles so they would appear on one or more of the predetermined subject lists and then provide a link to the user to click to get a dynamic list of just the titles on. that particular subject Dynamic such that any new titles I add and code will show up for them.

    I am NOT a programmer, don’t want to be, I need a plugin that will do that. Seems that I am probably not the first person wanting to do this. Is there a plugin that will do this?

  7. ton says

    Hi
    i am trying to have a function code added to the checkout page only , so how do I use the snippet to do so?

  8. THadryan says

    I tried using this plug in because everyone seems to love it, but it crashed my whole site before I could even do anything with it.

    This article convinced me to try it again, because everyone seems to like it, but EVEN JUST ACTIVATING it wrecks my whole site and I get the 500 error you refer to.

    Do you have any idea what might be going on?

    I would assume it’s my error but all I am doing is installing it and activating it.

  9. PseudoGeek says

    Would the code entered into this plugin persist through theme updates? Here’s what I’d like this plugin to do: In order to have the year update automatically in the copyright, many developers place a snippet of php code into a child theme to accomplish this. I really don’t want to make a child theme for every website just for that tiny change, but the alternative is to have my changes disappear with each theme update. But adding a child theme for something so minor seems like building a whole new fence when all you want to do is change the color of the latch on the gate. Maybe this plugin is the easy way to do this?

  10. Al Klein says

    I’m an old PHP developer, but this makes managing snippets a lot easier. (And, since I’m human, I made an error in adding a snippet – and it was just deactivated, Doing it manually would have meant making sure that the editor was still open when I tried the site, so I could go back and try to find what would have been a 500 error.

    Thank you for this post.

  11. Amrita says

    Thanks for sharing this.I am also afraid of adding custom code.But this looks easy.If I understood correctly if the code snippet has an error the plugin automatically decativates it .Right?

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.