Create a WordPress Plugin from Scratch - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys Alex here thank you for checking his video and welcome to the episode number one of these brand new series on how to create a wordpress plugin from scratch [Music] welcome again in this first tutorial we're going to take a look on all the basics to setup our first amazing plugin in our beautiful wordpress installation before starting because we are coding and we're going to code a lot of different things remember to set your wp-config.php file in your root directory installation of wordpress we define the WP debug global settings to through if it's not set to true every time you have an issue or every time you write something wrong like a bad PHP code or something like that your entire website is just not gonna print you're gonna see a blank page we don't want that because we're under development so we need to get all the debug information possible all the information that we need to understand what we did wrong and how to fix our code so just remember this is the only requirement that you need for like to follow this tutorial set your WP debug to through okay let's get started if we access the installation like a regular WordPress installation this is the usual structure like folder structured are you gonna see and as I said at the very first lesson of the WordPress 101 the only folder that you should care about your WordPress installation is the WP content everything else all the files WP admin that will be includes you should never touch those files because those are part of the core installation of WordPress so by accessing the WP content you're going to have a list of all the folders that you can use and you can touch in order to create your custom things and I'm sure you already guessed that we need to create our plugins in the plugins folder by default WordPress comes with a cassette and Hello Dolly to pre install plug-in it they show like a pretty regular structure and then the usual index dot PHP with an empty comment just to avoid the direct axis of the this folder there at access of these files if the user gains access to your WordPress installations through a browser so let's start by creating a new folder and let's name the folder with the same name that we want to assign to our custom plugin so in my case is going to be Alec add - plug in something like that and the requirement the kind of the only requirement of WordPress on how to about creating a plug-in is having a unique name for your plugin and these unique name has to be carried around from the folder to the main PHP file if this name is not unique your plug-in will have some conflicts of updating or versioning with other pre-existing plugins so before creating your folder name just do a check inside the official plugins repository just click add new and search for the plug-in name that you want to create so in my case is going to be a lake add plug-in and see if we have any results no matching result so that's perfect that plug-in name doesn't exist and I am safe it to use it let's access this folder and let's open this folder in sublime text inside our newly generated plugin folder the first file that we need to create is a PHP file with the same exact name of our plug-in folder so in my case is alak add the - plug-in dot PHP and it is really important because by default WordPress does this wordpress checks all the folders inside your plugins installation and the text the PHP file that has the same name of your folder and automatically lists all those things inside your plug-in directory but of course having an empty file is not enough for WordPress to recognize that this is a plug-in and you want it to be listed in your plug-in directory so let's create let's open the PHP tags and as usual because this is just a PHP file we're not going to have any HTML written here we can avoid you close the tags and this is actually really recommended not closing tags then we define a global setting for our package and say that these is a package called alak add plug-in something like that and now we need to define some global settings in order for wordpress to recognize our plugin and these section is pretty much identical to the comment section that you write in your style dot CSS when you generate a new theme to give the wordpress the ability to recognize the theme and specify some unique attributes to customize the theme so we're going to do exactly the same let's open again our comment block and don't use inline comment but use comment block that's really important and let's write the first is the plug-in name attribute and remember to write it exactly as I do otherwise WordPress can not recognize that specific attribute and the plug-in name is Alec add plug-in oops plug-in then the second attribute is plug-in you are I and no URL if you are I and here we need to specify the unique URL of that plug-in in my case it's going to be just simply HTTP slash slash Alec add calm but whatever if you have your plug-in hosted on a city file or on a github repository you can set up that specific URL so if for example I have this plug-in in a subdirectory of my website I can specify a unique subdirectory so if they click to view more details about my plug-in they're going to get redirected to that specific URL the third parameter is description and we can write simply this is my first attempt on writing a custom plugin for these amazing tutorials series something like that do we like it I guess so and here you can write HTML just really simple HTML you can have like strong bold italic underscore like underline all this kind of stuff like really really simple HTML don't use like crazy HTML markups are not recognized then we need to specify the version and my version is the 1.0.0 then we need to specify the author the author is alessandro double-quote Alec had castellani is my name and then the author URI also in this case and this is gonna basically redirect the user whenever they click on the name or the author of the plugin so in this case on my name then you need to specify the license of this plugin and usually the plugins are all open-source license like releasing a plug-in on WordPress on your future repository of WordPress that is not open source is really hard I don't think it's like recognized of stuff like that is like not allowed to publish a plug-in that is not under open-source license so usually when you're building a plug-in always set at GPL v2 or later that's my kind of my favorite open-source license otherwise there's like apache license MIT license just check in the new official documentation to see which license matches properly your needs and just use that and then we need to specify liking the theme the text domain of our plug-in in my case is going to be other CAD plug-in I'd save it let's go back in our WordPress installation let's access our plug-in directory let's refresh and look what we have here that is pretty amazing right we have Alec hat plug in the description the link to click visit the plug-in side and if we visit the plug-in side we're going to go to these fake URL that doesn't exist I like add slash plug-in I don't have anything but it goes to my website and if I click on the my name it goes actually to my website so I click on the other name and then we have the version and we have by default all the native links of WordPress of a plug-in so to activate edit or delete the plug-in and of course if we click Edit WordPress automatically goes inside the editor of the plug-in lists all the files that we have here so we could potentially also write our own Pauline directly inside WordPress but that would be kind of crazy right and of course if we try to activate it it's gonna be activated but there's pretty much nothing here because we didn't write anything so we can activate and deactivate the plug-in and nothing is going to break our website to work if i refresh I'm using the latest 2017 theme of WordPress and nothing is happening because of course it's an empty plugin but hey we few lines of code we generated a pretty legit first plug-in installation isn't that great the last thing that I want to do before concluding this first episode is screening a really important and really necessary file in order to have a little bit of security in your plugin and this file is the standard index dot PHP inside your installation because if someone tries to access directly your plug-in folder it will not go into all the available files that you have because you cannot access and see all the files but it will be redirected just in case that happens it will be redirected to the index file and in the index file what we want to do you want to just print an empty PHP we'd silence is golden and is the same technique used in another PHP file here like in the index dot PHP in the default plugins and if you know this it's also in the index dot PHP in the wp-content folder and that's how wordpress deals like in a super quick and dirty way unlike direct access into folders that we don't want to really give to read access to it because it's kind of catchy right the last thing that you could do you could simply in the main PHP file of your plug-in just print right after the basic attribute of your plugin just by leaving an empty row the license that you're actually using and I grab these license directly from the new website and the license that is described here is GPL version 2 this is not 100% required especially when you're building a plug-in but it's it's absolutely required if you decide in the future to publish your plug-in into the WordPress official plugins repository so why not put in it by default as the first thing so we will not forget in the future and that's pretty much it this is the very first and very very simple video about the plug-in development series in the next video we're going to spend a little bit of time on tackling all the security precaution and safety precaution in order to start developing a plug-in without worrying too much of exposing vulnerabilities or leaving backdoors open to our WordPress installation or whatever the WordPress installation we'll use our word plug-in so next lesson is going to be way more awesome than this one I really hope so well it's pretty much it for this video hope you enjoyed if you did please give it a thumbs up or subscribe to my channel and if you want you can spend a couple of minutes on the support me page of my website where you can find all the different ways and methods to support me support my channel and help me to do better videos and better tutorials for you thank you again guys and until the next lesson has usual happy coding
Info
Channel: Alessandro Castellani
Views: 314,316
Rating: undefined out of 5
Keywords: wordpress, tutorial, plugin, development, help, coding, php
Id: Z7QfH-s-15s
Channel Id: undefined
Length: 12min 21sec (741 seconds)
Published: Fri Aug 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.