Create a WordPress Plugin from Scratch - Part 3 - Basic PHP OOP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys Alex here thank you for checking this video and welcome to the lesson number three of the series of tutorial on how to build a wordpress plugin from scratch [Music] welcome again in this tutorial we're gonna take a look at the basic of a PHP object-oriented programming in order to build our own custom theme so in the previous lesson we saw some basic methods to secure our plugin I decided to go with this one because it's kind of like my favorite one that I like to use but you can use one of the three methods the one that suits you the best it doesn't really matter so as I said at the beginning of this series we're gonna just use object-oriented programming if you don't know anything about PHP object-oriented programming probably you're gonna have a little bit of a hard time following the series but I will try to explain everything I do step by step and I'll try also to teach you a little bit of object-oriented programming or opie shortened version while we build our own amazing first WordPress plug-in so let's get started so the first thing that we have to do we have to create a class and a PHP class to create one you just need to write class of course and then the name of the class and also in this case every time we create a function in WordPress it's always better to try to find and assign a unique name to our class otherwise we're gonna have some real bad problems a real bad moment so let's create a class by in my case Alucard plug-in and we're using the pascal case type of writing there are a different type of writing when you create methods name or classes name you can use camel case underscore pascal case so if you want to use a camel case you can use for example Alucard plug-in and this is camel case because it starts lower and every time you have a new word there's a capitalized letter otherwise there's a pascal case that starts with a capitalized letter and then every single word is capitalized as well otherwise you have hundred scores that in this case alec and so again these are different methods let's follow kind of like the recommendation of the codex and use pascal case now let's go to another line and open the curly brackets you will see sometimes users also me my videos using and opening the curly brackets for a class in line that's not a mistake it doesn't really matter if you open in line or another line it's not an issue it's just you respect the Codex or WordPress is just to respect like PS are two coding standards and stuff like that let's maintain an a coding standard that it's globally recognized so everyone can read our code properly without getting confused so inside the class here we're gonna write all our methods that we're gonna see in a moment of course if we save it and we go in our administration panel and we activate our plugin nothing happens especially like we don't even initialize the class because the class in PHP a class doesn't initialize by itself nothing happens if we want to use this class we need to create a new instance of the class and store that instance inside a variable so in order to do that we have to create a variable that you can name however you want in my case is gonna be a lake add plug-in cold weed camel case and inside here we're gonna store a new instance of the Alec had plug-in class and we don't need to pass any parameter to the class these example here creating a class and inside here you write whatever method and then you stored a new instance of the class inside a variable its basic PHP object-oriented programming you create a class you create a variable and you initialize that class you could potentially if you don't need to reuse that class or call it in other sections whatever you could actually just initialize directly the new instance of the class without storing a variable but we will probably use it in other section we will probably dynamically call methods of that class via this variable and I'm gonna show you how to do it's gonna be like super cool otherwise you can just call the class but before doing this we need to check if this class actually exists and that's a safety precaution that it's recommended in PHP when you create a class especially when you're creating a class inside a plugin that probably other developers are using many many other plugins you could have the same class name or stuff like that it's better always to do a quick check so before initializing the class we need to wrap these initialization inside a condition so if and only if the class exists and the class name that we need to check if it exists has to be the name of the class of course bypassed as a string this is a string this is not a variable so it's passed as a string single quotes double quotes doesn't really matter but check if the class exists and only if the class exists let's initialize a new instance of this class that's perfect let's save it let's go back in our backend let's refresh nothing happens if we deactivate and activate that's pretty standard knotting nothing is broken that's amazing so now what happens this is probably gonna be a little bit complicated but follow me so if we have for example a function call custom function something like that and we need to pass some variables so let's say that these here we have an argument and then these variable everything that it does echo that argument in order to call this function what we do in a procedural PHP is just calling the custom function like this and then passing whatever argument like these is my argument to echo stuff like that right this is pretty straightforward so if we have something like this and we want to do the same for the class you see that when we initialize the class we open and close the brackets the regular brackets but we're not passing any parameters so if we want to pass a parameter for example string that says Alec add plug-in initialized something like that how in heaven do we store these argument like if in procedural code when we create a function we have the arguments that we're passing inside regular brackets why in the class we can't because the class doesn't allow us to generate a class we the regular brackets this is wrong you don't have to do that in order to basically simulate and have the same result of this one we need to use the construct method of the class so inside a class you can write whatever method you want so function method one let's say and this method could do whatever you want now you can have another function that is method two and also this one you can set whatever you want to do but when we initialize the class what is the method that gets called whenever a new class is initialized the method that gets called by default is the constructor and you write a constructor by writing underscore underscore construct and they construct of course as you probably guessed is the method that accepts the parameter that you pass when you initialize a new class so here we can have our argument or whatever our string doesn't matter and here we can echo these is gonna be just an example but let's echo the string just to see if it actually works let's remove these two example methods save it and of course also the construct is a function so let's say that let's go back in our back end and let's refresh and do you notice here what we have if we inspect the element and we delete temporarily the man you look at halicarnassus so we know that our plug-in works of course we're doing something really really bad or we shouldn't do like initializer plugging passing a string and just echoing the string with a construct but this is just an example to tell you how the classes work and how by initializing a new class and passing a variable how you can use the construct method to pass a string and of course there are many many other options a way more advanced I'm not touching about the type string like for example if we are expecting this variable to always be a string you can say that hey dc's a string and by saying days like here we still have a penalized if we pass like an array instead of a string and inside array let's put some data like 1 2 3 if we pass an array and we refresh our back and you see that we trigger error and the error that it's federal error so it's blocking the execution of a website says that in the construct the method passed must be of type string and we passed an array so by declaring these as a string we are forcing the acceptance like we're forcing our method our constructor method to accept only string to the specific variable here of course we can say that these has to be an array or these has to be an integer and so on so on I'm not gonna touch I'm not gonna use these because it's kind of more advanced and could give you some confusion so let's not do that let's delete all this stuff let's delete this variable we don't need to pass it and let's leave it like that for now so if we save it and we refresh everything's back to normal so now we are in this situation where we have our class and we are initializing our class when the website is loaded when the plug-in is loaded now it's time to use the built-in methods of WordPress to actually properly activate unique sections of our plugin and when it comes to plugins WordPress automatically triggers three default actions when you use your own plug-in so these actions are and are triggered in different moments the moments where these actions are triggers are on activation and then on deactivation and then on unistall so these are our three different steps of the usual life of a plug-in you activate a plug-in you T activate a plug-in you unistall a plug-in it doesn't exist a method to trigger when you install a plug-in because WordPress doesn't allow that WordPress needs to take care of the installation of the plug-in until the user activates that plug-in you don't have the capabilities you don't have access to anything on the WordPress installation the user days using your own plugin so now what we're gonna do we're gonna create for now we're gonna delete the construct because we're now using the construct in a class and you can totally remove it is not obligatory to use a construct we're gonna create some default methods to call when these actions are happening so first let's create a function call activate then let's create a function called deactivate and then let's create another function call Union stalled and as you can see here I'm creating these methods without using the usual prefix like alucard underscore activate because we are using object-oriented programming all these methods are inside the plug-in class the alikat plugin class that has a unique name so these methods cannot be accessed by other classes or other functions if I don't want you and even if I have another class with the same method name is not gonna interfere we're not gonna have a PHP error because the method it's inside a class so it's not publicly accessible and all this kind of stuff so no conflicts are there this is really awful to keep our code cleaner and avoid to use all those two-bit prefixes that we're forced to use but let's continue now we have these three methods we need to or we want to call these three unique methods with the built-in hooks activation actions that WordPress gives to a default plug-in so the first method is called register underscore activation hook and as you can see my autocompletes just is to use underscore underscore file underscore underscore this is super cool because these is actually the proper way of writing the hook by passing this global variable as the first parameter we're saying that the register activation hook needs to work only in this very own file so let's maintain this let's now go around and search for other files outer includes or other stuff just like check for the function that we're gonna pass inside this file and usual for every action this is a default action it works like a default action we need to pass the function name that we want to activate as a string so in my case we want to pass the activate but won't work because the activate function the activate method it's inside the class in order to tap these activate method inside the class we need to pass an array here so let's pass an array with two parameters the first parameter is the class instance that luckily for us we're storing inside this variable and the second parameter is the string of the method the function that we want to trigger so in my case is activate these way you're writing it basically identical to the usual add action for example in it and then function name this is an example of course I'm not writing this code for real but these register activation hook it's identical to this but the reducer activation loop needs as a first parameter which file do have to consider by writing this global than I make define variable these file and then we're passing the array to say hey these activate method it's inside the Alec a plug-in class so let's access this class and let's check for the activate nothing too complicated as you probably guess also the deactivation method is identical to the activation so let's copy-paste deactivation hook and let's write simply register deactivation hook and here let's call the deactivate for now we're not gonna touch base the unistall method because there are actually two different methods to hook the unistall action and i'm gonna show you both and i'm gonna also tell you which one is my favorite and i guess the one that you should use but anyway let's stick with the activation and deactivation because that's those are the two methods that we can actually test I don't want a unistall my own plug-in because otherwise I'm gonna delete my own poll again but we're gonna test that in the future for now just stick deactivation deactivation so now in order to test if these works we're gonna trigger some errors let's say that on activate we can echo a message the theme was activated oh actually is the plug-in was activated but if you know a little bit of PHP you know that we are echoing a string inside a PHP function that it's cold after the header was sent so these will trigger an error and will trigger most likely will trigger the header already sent error but if these triggers an error it means that the activation method works and let's do exactly the same for the deactivate let's save it let's go back in our back and our plug-in is not active so if we refresh the page nothing happens we don't have any error message if we activate the plug-in okay that boom the plug-in generated 24 characters of an expected output during activation headers already sent message because we are trying to echo these on activation these means that on activation the hook called that unique method that's perfect if we leave the plug-in active and we refresh look we don't have anymore the error message because the action the hook doesn't trigger the activation because we're not activating the plug-in the plug-in is already active so we're not doing everything again but instead if we deactivate the plug-in we're not gonna have any error because the plug-in is actually deactivated so we are yes triggering the deactivation hook we did deactivate but we cannot HECO whatever message we have there because the plug-in is not active so we a wordpress is not considering this plug-in anymore so why these two methods are useful well we're gonna see in in the next lesson but just to give you a hint or a preview these two methods are really useful when you need to edit or update the database of a user when they activate or deactivate plug in so for example here like clears and the easiest example that you could do is generate a custom post type and then flush the right rules on the activation because the plug-in is not active anymore so the custom post type is not visible we simply flush the rewrite rules as well and Unionist all that it's something that we're seeing next lessons you have to actually delete the custom post type because if we deactivate we don't want to actually delete it because probably the user will reactivate it so we don't want to delete any data but here on unistall what you usually do when you resell you delete a custom post type or you delete all the plug-in data from the DB that's a overall cleanup so that's pretty much it for this lesson we saw how to write our very first own and super simple PHP class in object-oriented programming we created three methods that we're gonna use in the next lesson and we checked the first two built-in registration hooks for a plugin so it's pretty much it for today's lesson I 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 a super me page of my website where you can find 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: 149,257
Rating: undefined out of 5
Keywords: wordpress, plugin, development, php, oop, tutorial, help, coding, web development
Id: 34HJlgkxieg
Channel Id: undefined
Length: 19min 41sec (1181 seconds)
Published: Mon Aug 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.