How to make a basic WordPress plugin

There’s no doubt that the web is full of great tutorials on getting started with real WordPress development. Lots of developers far more talented than myself have provided helpful guides on plugin development. Even so, I am going to take the time to contribute my own tips to those wishing to take WordPress to the next level.

The basics

I think its always constructive to begin with the fundamentals. So…what are plugins?

Plugins are packages of code (can be very small to very large) that when activated within WordPress, modify or extend the features and functionality for your site.

For clarification purposes, there are (basically) four components that work together to make WordPress work:

  1. WordPress core
  2. Themes
  3. Plugins
  4. MySQL database

Whenever you want to do something cool or when you have problems these are basically the only areas of interest. It is worth noting that numbers 1 and 4 are basically never tampered with directly (unless you are super advanced or out of your mind).

Lets make a plugin

WordPress is built on PHP so that language is the basis for our plugins. If you are not familiar with PHP but have experience in other languages, this should be pretty easy for you at the basic levels. If you have no programming experience at all, this may be a little challenging but I’m confident that you can do it.

For starters, to make this as easy as possible, I created a starter plugin that should give you a great head start.

https://github.com/brashrebel/starter-plugin

All you have to do is unzip the file and begin editing files within. I recommend a program like Sublime Text but some other good options could be:

Upload that unzipped folder to the wp-content/plugins directory and you are ready to get moving.

basic wordpress plugin
The file structure for this starter plugin

Lets check out the code

For starters you’ll see that I created a couple files and a couple folders. Not every plugin requires this much stuff. In fact, many basic plugins are just a single PHP file. However, it is good practice to segment your code in a logical way so that it is easier to modify, troubleshoot and understand later.

The first file we’ll look at is the starter-plugin.php file. You’ll see that this file starts with a comment. This is an essential part of every plugin. It tells WordPress “Hey, we gotta plugin here” so it can display it as an installed plugin in your dashboard and so you can activate it.


<?php
/*
Plugin Name: Starter Plugin
Description: If you want to make a plugin, this gives you a great starting point.
Version: 1.0
Author: Kyle Maurer
Author URI: https://realbigmarketing.com/staff/kyle
*/

Below that you’ll see some more descriptive comments that are my explanations of whats going on.

The next thing you’ll see is:


require_once (dirname(__FILE__).'/admin/admin.php');
require_once (dirname(__FILE__).'/shortcodes.php');

What this does is grabs a couple of the other files included in this plugin so that they can be used as well. Just throwing files into our plugin’s directory isn’t enough, we need to include them as above.

It is worth noting that one could also use ‘include’ or ‘include_once’ instead of ‘require_once’ here. I found a great explanation of the difference between ‘require’ and ‘include’.

Next you’ll find some code for including a stylesheet. Notice that the ‘add_action’ line is commented out since I don’t actually want to enqueue this stylesheet if this plugin gets inadvertently activated.


//add_action('wp_enqueue_scripts', 'my_styles');
 function my_styles() {

 //This is for setting an optional condition for when you want to include your style (if you don't want it everywhere)
 global $post_type; //variable for getting current post type if needed
 if ($post_type == 'my-post-type' || is_singular('my-post-type')) :

//Now we actually register the stylesheet
 wp_enqueue_style("starter-plugin", plugins_url("/css/style.css", __FILE__), FALSE);
 endif;
}

The last thing in the main file is a function for including some Javascript. Just like with stylesheets, it is appropriate to enqueue your scripts properly so that they are output in the right place and at the right time.


//add_action('wp_enqueue_scripts', 'my_cool_script');

function my_cool_script() {
 wp_enqueue_script("coolscript", plugins_url("/js/script.js", __FILE__), FALSE);
}

The other files

I won’t bother to go through all the other files as they are mostly just placeholders with nothing in them but maybe a comment. I’ll mention the shortcodes one though.

I often write quick, little shortcodes for content related items I, or other authors, may use frequently. So many of my plugins have a shortcodes file. Here’s the example one from the starter plugin:


function my_shortcode() {

return "Three is the number to which thou shalt count.";
}
//This part first creates a shortcode, then names the function that gets run when we use this shortcode
add_shortcode('yippee', 'my_shortcode');

One thing worth noting, for those familiar with PHP, is that ‘return’ is used to output content in shortcodes instead of echo. You can read the codex on shortcodes for more detail.

Taking it further

Well, now you have the basics of writing a plugin. The next step is…to think of some plugins to create. There are a million-trillion possibilities (fact!) for making WordPress plugins. Here are some other suggestions and notes:

  • When adding basic functionality to your site, the option of adding your code to your theme’s functions.php file OR creating a plugin will typically be a consideration. Generally speaking, you would only use the theme option if your function is ONLY useful with the current theme AND you have little to no interest in reusing it on other sites later.
  • WPSnipp is a fantastic resource for lots of simple scripts that I think are perfect for plugin beginners. Just grab one of the many useful snippets published, modify to suit your needs and then drop it in your plugin. Boom. Done.
  • WPBeginner, DoItWithWP, WP Tuts+, CSS Tricks and DigWP are just a few of my favorite resources for cool scripts that can inspire and empower you.
  • If you manage to create a plugin that works well for you, you might consider publishing it on the WordPress Plugin Repository so that others can benefit as well. Just go here to submit one. The requirements are…not really a big deal. If it works for you and there is a chance others might also find it useful, I’d recommend just publishing it. Other users will be able to notify you of bugs and possible features to add. Its really a win-win.

I hope this was helpful to some. I’d love it if anyone shared comments with ideas on how this could be made easier as well as other tips for first time plugin developers.

770 Responses to “How to make a basic WordPress plugin”

  1. Hello Kyle,

    I have installed a localhost on my computer to try a get the plugins working on wordpress but i am pulling my hair out trying to work out why wp plugins will not work after they have clashed with each other. They worked fine by themselves, but since the clash there seems to be an error or something in the files some were.

    Know matter what i do i just can not get them to work again I have deleted and reinstalled the plugins but the script wont even work with no other plugins in the directory.

    I have even done a re-installation of wordpress

    The backend works fine, it is the script on the frontend that is not working.

    I have come across your sight after about 2 days of searching for a solution. i compelled to ask you for any advise that you may have to share.

    One thing that i do know is that more than 2 scripts need to have a load order, but don’t know how to implement it on wordpress to over come the problem.

  2. naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

  3. Все больше мастеров, как домашних, так и профессиональных, стремятся приобрести лазерные уровни нового поколения

  4. This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!

  5. Dikatakannya lanjut, jika administrasi yang ditampilkan mumpuni maka akan menjawab pertanyaan dari para pengawas program baik dari TNI maupun Pemerintah dengan hanya melihat di papan tabulasi/sketsel.

  6. I hope this was helpful to some. I’d love it if anyone shared comments with ideas on how this could be made easier as well as other tips for first time plugin developers.

  7. Marriage by accident sangat amat melekat dilingkungan saya. Mulai dari teman, sepupu hingga akhirnya kakak saya sendiri mengalami. Saya tidak pernah mau ambil pusing, itu kehidupan mereka asalkan ketika sudah terjadi tolong bertanggung jawab pada apa yang telah mereka lakukan.

  8. အာမခံအဖြစ်ပေးအပ်သည့် မြေ၊ အဆောက်အဦပိုင်ဆိုင်မှုအတွက် အောက်ပါတို့ကို တင်ပြ ရပါသည်-

  9. ဘဏ်သို့အာမခံအဖြစ် ပေးသွင်းထားသည့် ပစ္စည်းအားလုံးကို ဘေးအန္တရာယ် အသွယ်သွယ်အတွက် လုံခြုံစေရန် အာမခံထားပေးရပါမည်။

  10. naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

  11. साल पूरे होने के उपलक्ष्य में होने वाले समारोह में भी शिरकत करेंगी। इसके बाद राष्ट्रपति शांतिनिकेतन जाएंगी, जहां वह विश्व-भारती के वार्षिक दीक्षांत समारोह में हिस्सा लेंगी।

  12. ကြပ်မတ်ခြင်းမပြုနိုင်သဖြင့် ကုန်စည်စာရင်းတင်သွင်းခြင်းနှင့် ၎င်းတို့ကို စစ်ဆေးခြင်းများကို လက်ရောက်ပေါင်ခြင်းထက် ပို၍ဆောင်ရွက်ရမြဲဖြစ်ပါသည်။

  13. စောင့်ကိုလည်း အစောင့်အကြပ်အဖြစ် ခန့်ထားပါသည်။ ကုန်စည်များကို ဘဏ်ကသိမ်းဆည်း ထားကြောင်း ဖော်ပြသည့်စာတမ်းကိုလည်း ကပ်ထားရန် တရားဥပဒေအရ လိုအပ်သဖြင့် ကပ်ထားရမည်ဖြစ်ပါသည်။

  14. အဖြစ်သာ လက်ခံလေ့ရှိရာ ဤကဲ့သို့ အပေါင်ခံရာတွင် ပစ္စည်းများ ကို ဘဏ်ပိုင်ဂိုဒေါင် (သို့မဟုတ်) ငွေချေးသူက အသုံးပြု၍နေသည့် ဂိုဒေါင်တွင် ထည့်သွင်

  15. 全新升級的大撈家 娛樂城正式上線!全新介面設計、優化操作流程,讓你玩得更順、賺得更多!快來體驗不一樣的撈金快感!

  16. လုပ်ငန်းလုပ်ကိုင်ခွင့် လိုင်စင်ရယူရန်မလိုအပ်သည့် လုပ်ငန်းအမျိုးအစားများ အတွက် လုပ်ငန်းအမှန်တကယ်လုပ်ကိုင်ကြောင်း ခိုင်လုံသည့် အထောက် အထား၊

  17. ခွင့်ပြုသည့်ချေးငွေစုစုပေါင်းအပေါ် ဘဏ်ဆောင်ရွက်ခ ၁% နှင့် ထုတ်ချေး ခြင်းမပြုသည့်ချေးငွေအပေါ် ကတိခံကြေး ၁%ကို (၁-၁-၂ဝဝ၆) မှ စတင် ကောက်ခံပါမည်။

  18. WordPress is built on PHP so that language is the basis for our plugins. If you are not familiar with PHP but have experience in other languages, this should be pretty easy for you at the basic levels.

  19. ဘဏ်မှရယူသောအတိုးနှုန်းသည် နည်းပါးသော်လည်းပုံမှန်အတိုးငွေပေးသွင်း ခြင်းမရှိပါက အတိုးပေါ်အတိုးဆင့်တွက်ချက်ပါသည်။

  20. များနှင့် မှတ်ပုံတင်ခများကိုမူ ငွေချေးယူသူက ကျခံရပါလိမ့် မည်။ ယင်းသို့ ကုန်ကျရမည့်စိတ်များကို အကြမ်းအားဖြင့် ဘဏ်ကပြောပြနိုင်ပါသည်။

  21. Nhiều quan điểm cho rằng, lối sống tối giản của người Nhật sẽ khiến cho những điều họ yêu thích và trân trọng trong cuộc đời trở nên rõ ràng.

  22. カートリッジを購入した後のアフターサポートも充実しており、よくある質問への回答やメールでのお問い合わせに対する迅速な対応が提供されています

  23. メールで問い合わせる際には、製品の型番や購入時期など、具体的な情報を提供することでスムーズな解決につながります

  24. カートリッジの交換手順は、製品に同梱されているマニュアルに従って行います。取り付け方は非常にシンプルで、古いカートリッジを取り外し、新しいものをしっかりと固定するだけです

  25. カートリッジを選ぶ際には、正規品を購入することが大切です。偽物や互換品は、ミラブルプラスの性能を十分に発揮できないことがあります

  26. Kara Scannell is a renowned journalist. She is serving as a CNN correspondent based in New York covering law, crime, and business investigations, with a special focus on the US Attorney’s Office in the Southern District of New York

  27. ミラブルシャワーヘッドの評判は、ユーザーの声やランキング、評価サイトなどから分かります。ユーザーの声では、肌の潤いや透明感がアップしたという声が多くあります

  28. ミラブルシャワーヘッドとは、美肌への一歩をサポートするために開発されたシャワーヘッドです。その特徴は、水の微粒子化技術を採用していることです

  29. UNDERAGE FUCK WITH DOG, UNDERAGE GIRLS NAKED, UNDERAGE MINOR GIRLS , RAPED CHILD PORN VIDEO, UNDERAGE PORN XNXX, PORN SEXTAPE, RAPED CHILD PORN VIDEO, UNDERAGE PORN XNXX, UNDERAGE FUCK WITH DOG, UNDERAGE GIRLS NAKED, UNDERAGE MINOR GIRLS PORN SEXTAPE, RAPED CHILD PORN VIDEO, UNDERAGE PORN XNXX, UNDERAGE FUCK WITH DOG, UNDERAGE GIRLS NAKED, UNDERAGE MINOR GIRLS PORN SEXTAPE, RAPED CHILD PORN VIDEO, UNDERAGE PORN XNXX, UNDERAGE FUCK WITH DOG, UNDERAGE GIRLS NAKED, UNDERAGE MINOR GIRLS PORN SEXTAPE, RAPED CHILD PORN VIDEO, UNDERAGE PORN XNXX, UNDERAGE FUCK WITH DOG, UNDERAGE GIRLS NAKED, UNDERAGE MINOR GIRLS PORN SEXTAPE, RAPED CHILD PORN VIDEO, UNDERAGE PORN XNXX, UNDERAGE FUCK WITH DOG, UNDERAGE GIRLS NAKED, UNDERAGE MINOR GIRLS PORN SEXTAPE, RAPED CHILD PORN VIDEO, UNDERAGE PORN XNXX, UNDERAGE FUCK WITH DOG, UNDERAGE GIRLS NAKED, UNDERAGE MINOR GIRLS PORN SEXTAPE, RAPED CHILD PORN VIDEO, UNDERAGE PORN XNXX, UNDERAGE FUCK WITH DOG, UNDERAGE GIRLS NAKED, UNDERAGE MINOR GIRLS PORN SEXTAPE,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>