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

Vad, varför och hur man skapar ett site-specifikt tillägg till WordPress

Vill du lägga till ett site-specifikt plugin till din website i WordPress?

Ett site-specifikt plugin allow you att enkelt lägga till code snippets till din WordPress website utan att förlita sig på ditt theme.

I den här artikeln förklarar vi hur du skapar ett site-specifikt WordPress plugin och varför det är viktigt.

Site Specific WordPress Plugin

Vad är ett site-specifikt tillägg till WordPress?

Ett site-specifikt WordPress plugin är ett fristående plugin som du kan använda för att add to all customomize snippets som inte är theme-beroende.

När du arbetar med din website kommer du ofta att hitta WordPress tutorials som ber dig att lägga till kod i ditt temas functions.php-fil eller ett site-specifikt plugin.

WordPress levereras inte med ett plugin som är specifikt för webbplatsen. You will need to create your own and then install and activate it.

Varför skapa ett site-specifikt WordPress tillägg?

Som vi nämnde tidigare kommer du ofta att stöta på tutorials som visar kod som du kan add to functions.php-filen i ditt theme eller ett site-specifikt plugin.

Dessa custom codes kan användas för att add new post types, taxonomies, shortcodes, och massor av hacks för att förbättra din webbplats.

Om du addar den customizen till ditt temas functions-fil kommer den att försvinna om du updatear eller byter tema.

Du kan skapa ett barn tema och använda barntemats functions-fil för att save your code. Din kod kommer dock fortfarande att försvinna om du byter theme.

Ett site-specifikt WordPress plugin allow you to easily add custom code snippets to your WordPress website and make sure they are theme-independent. Det är ett fristående WordPress plugin, vilket innebär att det inte är beroende av ditt theme, och you are free to update or switch your theme.

Med detta sagt, låt oss ta en titt på hur man enkelt kan add to custom code med hjälp av ett site-specifikt plugin. Vi kommer att visa dig två sätt att göra det, och du kan välja den metod som fungerar bäst för dig (ledtråd: Metod #2 är lättare för Beginnare).

Metod 1: Skapa ett tillägg till WordPress som är specifikt för webbplatsen manuellt

Vi vet att det här kan låta lite nördigt för Beginnare, men vi ska försöka göra det så enkelt som möjligt för You.

Först måste du skapa en new folder på skrivbordet och döpa den efter din website. Till exempel: mywebsite-plugin.

Creating your site-specific plugin folder

Öppna nu en plain text editor på din dator som gillar Notepad eller TextEdit.

You need to create a new file and save it as mywebsite-plugin.php in the plugin folder on your desktop.

Creating the plugin file for your site-specific plugin

Din plugin-fil behöver en specifik header-kod så att WordPress kan känna igen den som ett plugin. Gå vidare och add följande kod till din mywebsite-plugin.php-fil:

<?php
/*
Plugin Name: Site Plugin for example.com
Description: Site specific code changes for example.com
*/
/* Start Adding Functions Below this Line */

/* Stop Adding Functions Below this Line */
?>

Du kan ersätta example.com med ditt eget domain name. När du har gjort det är ditt site-specifika plugin klart.

Det finns två sätt att uploada ditt site-specifika plugin till din website. Du kan antingen uploada det via din WordPress adminpanel eller använda FTP.

1. Installera site-specifikt tillägg från WordPress Admin Area (rekommenderas)

Denna metod är enklare och rekommenderas för alla användare.

Först måste du skapa en ZIP-fil med din site-specifika plugin folder.

Windows-användare kan helt enkelt högerklicka på plugin-mappen och select Skicka till ” Komprimerad (zip) folder.

Creating zip file in Windows

Mac-användare måste högerklicka.

Välj sedan ”Komprimera mywebsite-plugin”.

Creating a zip folder on Mac

När du har pluginets ZIP-fil, gå till Plugins ” Add New page i din WordPress admin area.

Klicka sedan på knappen ”Upload Plugin” högst upp.

upload plugin

Klicka sedan på knappen ”Choose File” för att selecta zip-filen som du skapade tidigare och klicka sedan på knappen ”Install Now”.

WordPress kommer nu att uploada och installera pluginet åt dig. När det har laddats upp måste du klicka på knappen ”Activate Plugin” för att börja använda ditt site-specifika plugin.

Activate plugin

2. Uppladda ditt site-specifika tillägg till WordPress via FTP

För den här metoden behöver du inte skapa en ZIP-fil. Du kommer att uppladare pluginet via FTP.

Först måste du ansluta till din website med hjälp av en FTP-klient.

När du har anslutit, gå till /wp-content/plugins/ folder under remote site column. Därefter måste du välja din site-specifika plugin folder och uploada den till din website.

Uploading plugin via FTP

Din FTP-klient kommer nu att överföra din site-specifika plugin folder till din WordPress website. Detta installerar pluginet på din website.

Du måste dock fortfarande aktivera pluginet för att börja använda det. Du kan göra detta genom att gå till sidan ”Plugins” i ditt admin area i WordPress och sedan klicka på länken ”Activate” under ditt site-specifika plugin.

Activate plugin

Nu är allt klart. Ditt site-specifika plugin är nu redo att användas.

Lägga till anpassade code snippets till din site-specifik plugin

Det finns två sätt att edit your plugin och add custom code snippets till det.

Den första metoden är att göra det via WordPress admin area. Du kan gå till Plugins ” Editor page.

Du kommer att se ett varningsmeddelande och du måste klicka på knappen ”Jag förstår” för att fortsätta. Om du vill ha ett säkrare sätt att editera filerna kan du hoppa till den andra metoden under.

Heads up warning in the plugin editor tool

Därefter måste du välja din site-specifika plugin från rullgardinsmenyn märkt ”Select plugin to edit.

The editor kommer att hämta din plugin-fil och du kommer att kunna add to code snippets till den.

Select a plugin to edit in the plugin file editor tool

När du är klar klickar du på knappen ”Update File” för att save your changes.

Om det saknas något i din kod eller om det har potential att förstöra din website, kommer plugin editor automatiskt att ångra dina ändringar.

Men om editorn Misslyckas, och du ser den vita vyn av död, kan du använda FTP för att redigera din plugin-fil och ångra dessa ändringar.

Den andra metoden är att direkt editera plugin-filen med hjälp av FTP. Gå helt enkelt till plugin foldern med din FTP-klient. Högerklicka på filen med pluginet och välj sedan ”View/Edit”.

Editing plugin file via FTP

You can also download the plugin file to your computer, edit it, and then upload it back.

Metod 2: Lägga till Custom Code med hjälp av WPCode (rekommenderas)

Den här metoden är mycket enklare och ger dig ett bättre sätt att hantera dina enskilda code snippets i WordPress med hjälp av WPCode.

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 Step-by-Step guide om hur du installerar ett WordPress plugin.

Vid aktivering kommer pluginet att lägga till ett nytt menu item märkt ”Code Snippets” till din WordPress admin menu. Om du klickar på det kommer du till en page där du kan hantera alla dina custom codes.

Om du vill lägga till ditt första code snippet klickar du på knappen ”Add New”.

Click the Add New Button to Add Your First Custom Code Snippet in WordPress

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 a custom code, navigera till alternativet ”Add Your Custom Code (New Snippet)” och click the ”Use snippet” button.

Add your new custom code snippet in WPCode

Nu kan du enter en titel för din custom code snippet. Det kan vara vad som helst som hjälper dig att ID:a koden.

Efter det kan du gå vidare och klistra in din code snippet i kod boxen. Du måste också välja typen ”PHP Snippet” från dropdown-listan ”Code Type” till höger.

Adding custom code

You can also add notes for the code in the ”Basic info” section.

Du bör använda detta area för att skriva ner vad koden gör, var du hittade den och varför du lägger till den på din website. Detta kommer att hjälpa ”framtida you” att komma ihåg varför ”tidigare you” adderade den här koden.

Add notes and tags to identify and organize your code snippets

Du har också alternativet att tilldela taggar till dina code snippets, vilket kan hjälpa till att organisera code snippets efter ämne och funktionalitet.

Pluginet tillåter dig också att selecta hur du vill köra code snippet. I section ”Insertion” kan du selecta metoden ”Auto Insert” för att automatiskt infoga och exekvera koden på din site.

Du kan välja mellan alternativen admin area, front-end eller överallt. Om du är osäker behåller du standardalternativet ”Run Everywhere”.

Pick the insertion method for your custom code snippet

Eller så kan du välja ”Shortcode”-metoden. Med den här metoden infogas snippet inte automatiskt. När du har saved snippet får du en shortcode som du manuellt kan insert var som helst på din site.

You can also use the ”Smart Conditional Logic” section to either show or hide auto-inted snippets based on a set of rules.

Use smart conditional logic to decide when snippets should load

Du kan till exempel ladda code snippets endast för inloggade användare, ladda PHP code snippets endast på specifika pages URL:er, visa code snippets baserat på typen av page, med mera.

Slutligen kan du toggle omkopplaren från ”Inaktiverad” till ”Aktiv” och sedan klicka på knappen ”Save Snippet”.

Save and activate your custom code snippet

Om du vill spara code snippet utan att aktivera det kan du bara klicka på knappen ”Save Snippet”.

När du har saved och aktiverat ett code snippet kommer det automatiskt att börja gälla på din website om det är den insert-metod du har valt.

För mer detaljer, se vår guide om hur du enkelt lägger till custom code snippets i WordPress.

Vi hoppas att den här artikeln hjälpte dig att lära dig varför och hur du skapar ett site-specifikt WordPress plugin. Du kanske också vill se vår lista med användbara tips om funktionsfiler och de mest efterfrågade WordPress-hackarna för att bäst använda ditt site-specifika plugin.

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

120 kommentarerLämna ett svar

  1. Syed Balkhi

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

    Is it possible to add code snippets like Google Analytics Code, Adsense code in Site Specific Pliugin. Or only possible to add codes we generally put in functions.php file.

    I mean, is it possible to add codes in site-specific plugin what we generally put in Header.php file.

    • WPBeginner Support

      You can do that if you would like, you can also use the insert headers and footers plugin.

      Administratör

  3. Bola Oussou

    hi
    Thanks for this tuto
    however is there a way to make this plugging(not to be visible) not in plugging list and also in a specific folder (not wp-content folder) ?

    Thanls

    • Tor-Bjorn Fjellner

      Actually yes, there is:
      Instead of placing the plugin in …/wp-content/plugins/ you put it in …/wp-content/mu-plugins/ (You may have to create that directory, if it’s not there yet.

      Plugins that are placed in mu-plugins are ALWAYS run, so the only way to deactivate a MU plugin (MU currently is construed as ”must use”) is to delete it (or remain the directly mu-plugins to something else…).

  4. Judith

    Hi I tried to do the Site-specific plugin, but it is not working, I get this message ”The package could not be installed. No valid plugins were found.

    Plugin installation failed.”
    What do I do?

    • WPBeginner Support

      Hi Judith,

      This usually means that the plugin header information is incorrect. You can try again and make sure that your plugin header is exactly in the format displayed in the tutorial.

      Administratör

      • Jonathan Manheim

        I get this same error. I have tried maybe 10 times. Uploaded through admin and on FTP. Cannot get it to upload. I am using TextEditor to paste the code. Notice several things you don’t mention in the post that I wonder could be an issue. First, won’t let me save the file as .php. I have to save it as .php.rtf OR .rtf then go back and rename the file after it is saved.
        Second, not sure what you mean by making sure the format for the plugin header is exactly the same. Should the line number be in there? If so, I can’t get that to work. If I copy and paste with the line numbers, the green vertical line doesn’t show up and there is no space between the line numbers and the code. Or am I supposed to just copy the code (without the line numbers)? Either way, I’ve tried everything I can with my current Textedit application and it will not work.
        Any ideas? I can send you screen shots of exactly what I have if you would like

        • WPBeginner Support

          Hi Jonathan,

          You will need to save file as .php. Please use a different text editor on Windows you can use Notepad or download Notepad++. On Mac, you can use TextEdit or download and install TextWrangler.

          You don’t need to copy and paste line numbers.

        • Kacper

          In my case the issue was that the file had Unicode encoding. Changing it to ANSI fixed the issue.

  5. Ikram

    Hi,
    Good day!
    Would like to know how to set a white paper in my blog post article to download it with add email address from the visitor.
    Thanks, Ikram

  6. Alain Fradette

    Great info! Thanks so much. I have been using WordPress for years and this little gem made it so much easier!

  7. Abhijeet

    I have edited some code in Style.css . Will it be overwritten if i update a theme?
    is it possible to create style css file in this site specific plugin?

  8. dave

    White screen on publishing pages.
    The code works to show child pages on a parent page using the shortcode however I am getting white screen. Anyone know why?

  9. Tommy

    The plugin generated 3 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

  10. Saurin

    I believe best approach to add custom code to site is child theme. No matter if we need to customize theme or not. I mad a practice to always use child theme even if there is no customization. This habit saved my time & effort many times. I dont think it is good to create extra plugin for such customization.

    I believe in minimum use of plugin no matter how simple you created one.

  11. Jason

    Which is better, site-specific plugin or Child Theme? or both?

    I need to create a custom post type to add some major functionality to my site so is the plugin better or the child theme and its functions.php?

    Thanks!

    • Josh

      The thing is, is that you’re creating the plug-in yourself, using the functions the same exact way. So the answer to your question would be that neither is better, because you should be using them WITH each other.

    • Josh

      Think of the site-specific plug-ins as functions that are independent of your theme.

  12. Vickey

    How would I create a create a new plugin directory in Azure? I have WordPress running on Azure using MS SQL Server, which I activated using Project NAMI.

  13. vishal

    not working on multisite with publisher theme can you help me this is my site and also tell me how can i remove featured image inside the post not from front page latest post

  14. Nadia Khan

    Hi there,
    You WP Beginners are rock. and I want to thank you for your Struggles for us.
    Now come to the point. I have installed an theme and in that theme, there is an function that is showing wp toolbar for guests, I have tried so many times to find this function in theme files to remove this, but i can’t. So please help me to remove that toolbar, that is showing to every visitor with wordpress logo and wordpress’s own link to their website. Screen Shot is attached.

  15. Manuel

    Does something like this work for plugins too? Im finding that I am making a few changes to plugins that I add to my site….many times deleting certain lines of code I dont want. I’m assuming that after I update that plugin, all my changes will be erased. How can I avoid this?

  16. Elizabeth

    Hi, thanks for the post. I’m getting an error when I activate the plugin for some reason:

    Parse error: syntax error, unexpected ’*’, expecting identifier (T_STRING) on line 9

    Any ideas what I’ve done wrong?

    Thanks!

    • WPBeginner Support

      Hi Elizabeth,

      There is an unexpected asterisk sign in the code on line 9 which is blocking the php script. Edit the file again and copy the code exactly as it is shown above. Make sure that you don’t copy line numbers in the code click on the Copy button at the top right corner of the code snippet box

      .

      Administratör

  17. Steven Denger

    I tried naming the new folder as you did in the example but I got an error that said something in the way of you cannot use a name like this. This Instruction may be helpful to some – but it is very difficult and confusing to me and does not help at all. As I said – you need to explain this down to the fine details or otherwise these tutorals will not help. The name of this site is wp BEGINNER – and that is what I am. You project these tutorals to the more experienced.

    I guess I will have to start going elswhere to get beginners instructiions because, most of time, wp beginner is not helpful – it is more like a techs only club.

  18. Steven Denger

    How do you get to the /wp-content/plugins/yoursitename-plugin/ in wordpress and upload a file there? I am lost on the first line of instruction. You guys always assume that we know these things in your instructions but they are not clear enough.

    I went to cpanel and opened up the wpcontent > plugins – and it shows a list of plugins – but I am lost from this point. How do I go about uploading a file in here? I am a beginner – and you guys seem to always explain things like your instructing experienced techs. I am not but I go to WP Beginner to try to learn. It is usually difficult to follow your instructions and I end up having to go somewhere else in google search to find things explained down to a beginners level.
    If you want to be more helpful try breaking things down a little more simple.

    • Manuel

      Hey Steven,

      I know the feeling! I’m far from a expert with wordpress and I pretty much live on google searches and this site trying to learn. I know a little bit and was able to get this to work. Send me your email and I’ll send you the steps with screenshots. I havent tested putting actual code into the plugin so we’ll see if that works….but I was able to see the newly created plugin on my admin page

      • Steven Denger

        thank you manuel

      • NiceRazer

        Hello, can you please tell me how to do that to? I’ve tried finding posts that would help me with this same prob but still didnt found it.

  19. JoAnn Chateau

    Thank you for the great advice and simple how-to instructions. I easily created a Site-Specific WordPress Plugin for my site and enabled shortcodes for widgets. Now I have less worry when changing themes, and I didn’t need to install another plugin. Again, thanks!

  20. george

    hi i followed the instruction on how to make a site plugin for my site. ive uploaded it but cant see it in my list of plugins.

    any ideas why?

  21. Dawn

    I cannot find where the folders live in the plug in directory. I tried searching for: /wp-content/plugins/yoursitename-plugin/ (with my site name).

  22. Kara

    Never mind, I figured it out and got it activated. My hosting server was acting up, must have timed out or something…cheers!

  23. Kara

    I followed your instructions but i don’t know how to activate it. It doesn’t show in my list of plugind in my wp-admin. Can you help?

  24. Sheyejinkazama

    Thank you. Thank you so so much.

  25. Molly

    How do you get the plugin to ”Network Activate” on a multi-site?

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.