Using Composer & PSR-4 autoloading for WordPress Plugin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to learn how we can implement PSL for auto loading and it will press plugin from scratch so for example if you want to utilize PHP name spacing or MVC structurally in your plugin then this would be really helpful I really prefer building my web apps or plugins using this approach as it keeps my codebase streamlined which in turns makes it easier for me to modify or extend its capability whenever to quiet this video will not cover how to properly integrate MVC structure for your WordPress plugin but rather on how to use the name spacing logics that being said I will be showing how to properly set up a laravel inspired will press plugin from scratch in my next video all right here I have a blank project open called wpm easy plugin with just one file which is a circuit ignore and the vs code folder with just some PHP top block settings which is not relevant to the project now to make sure we have everything that's required for this which is just composer we can open up a terminal and run a command called compose just like that to make sure we have two installed so if you get an output like this then that means we are good to go but if you get any error or you don't see the same response that means your system does not have composer so if you do not have composer installed on your machine you can go to get compositor Obagi slash download for Windows user make sure you set the path variable for composer so that you can invoke it from anywhere alright once that's done we need to create this file called composer to chasten in the root of our plugin we need this file because our composer is going to read off of this file and register the configuration so I'm just going to create a new file in the root let's call it compositor Chasen here we are going to write down the autoloading logic for our plugin so I'm just gonna write it down and then I will be explaining it you and that's app so right here we were asking composer to use the autoload schema for our plugin with psr-4 standards of autoloading and in here you can call your namespace or your folder name whatever you feel like but for simplicity sake I'm calling it app and app as my folder name respectively now once that's done we have to open our terminal again and run this another composer command which is composer dump autoload then this command is going to create a folder in your root directory called window and this when the folder contains a file called Auto load and a foldable composer this folder is going to contain all the older load logics and class maps which we do not have to get into right now but all we have to worry about is this file called Auto load as the name suggests this file contains all the auto loading and namespacing logics that we are going to leverage in have a wordpress plugin and to do so we have to require this file in our plugin that being said in this video i'm going to require this file in the main file of our plugin ok so let's create the main file for our plugin we are going to create it in the root and let's just call it WP MVC plug-in just like our plug-in name and it's going to be a very basic plug-in with very basic plugin comment made up with the plug-in name let's just give it a name called MVC plugin we should also give it a version for this video I'm calling it zero point one point zero and this is always good to use this version convention for your beta or alpha plugins and for your major releases you can go with a normal convention like so but let's stick to our beta convention also give it a description a nice description and the author why not all right once that's done we will require the order load file from the vendor folder in here so let's just do that so it's a vendor all the load PHP and that is that once that's done we are going to spin up our browser and go to the admin panel of our basic WordPress setup to see if everything is fine and we do not have any errors I have it opened up already so now let's go to plugins ok great we can see it we're plugging in here let's try to activate it great no errors just a protip always said that there will you be debug option to true in your WP config while you are developing a plug-in or a theme so if in any case our plugin is throwing a PHP notice or a warning it would be visible in hair and we will be able to fix it immediately all right so if you remember you never composite adjacent file we have defined of a namespace and a folder name called app which you can call anything but we do not have this folder yet so let's create it in this folder you can have as many files or folders as you want but it's always better to group your files inside a specific folder I'll show you what I mean so for this video I'm going to create an admin menu for our plugin just below the settings menu now I'm going to do this by leveraging the name spacing and Auto loading functionalities so since it's going to be inside admin or we can say it is admin specific I am going to create a folder called and been inside app folder notice the capital A for my folder name I'm doing this because it is recommended in psr-4 standards that we start our folder name or class names with a capital letter alright inside the admin folder let's create this file called menus controller dot PHP here we are going to create a very basic class boilerplate everyone is familiar with this basic PHP class boilerplate but our psr-4 autoloader is not going to identify this file because we haven't defined the name spacing for this file so let's try to namespace it now how do we start our namespacing if you remember in the composite adjacent file we have defined that our namespace starts with a p-- now these double backslashes are nothing more than a single backslash which is escaped by another backslash so now we know that our namespace starts with ab which in your case can be anything so now let's type in camp in here now after that we know that men use controller exist inside the admin folder so we can call it headman great so many controller is an admin folder which is further in app folder which is what we have right here so the folder structure is what we are following with our namespacing structure now let's start creating our admin menus and for that we are going to create a function and let us call it initialize menus and what I like to do here is create an area of all the menu items which makes it easier for me to edit in future whenever required so we are directly going to return this error now the every key item in this error is going to be the slug forever menu so for our main menu the slug is going to be WP and basic plugin which is basically the name of our plugin and it will contain all the menu items such as page title many title capability I'm basically listing out all the arguments of the ad venue page function of WordPress so it's nothing fancy so now let's add the function attribute and we also have the icon URL and the priority all right great so now let's start typing the page title let's give it a very basic page title here let's call it MVC plugin and we are going to use the same name for every minute at level capability I like to give it as administrator function now since we are inside class the function is going to be inside the array and we will reference the class where the function is defined so in this case it's this class and the second argument is going to be the name of the function let's just call it menu view I can URL and priority we can leave blank for now okay now let's quickly create the menu view function this is going to be a very basic menu view nothing fancy is going to go here so let's just give it an h1 tag and call it a page title and that's it now we have initialized in menu items but we haven't written the logic where we are going to actually add the menu so let's quickly write it down here function and menus all right so let's create a for each loop here the key is going to be the menu slug as we have discussed already so let's just call it menu slug here and rest of the items can go in the menu variable ok now we are going to call the default WordPress function to add the menu page which is add manual page that's it now the first argument that it takes is page title ok I'm just going to copy this to save some time second one is going to be the menu title the third argument is the capability first one is this slug so we can use the menu slug the fifth one is going to be the menus call that function so let's just call it function the sixth one is the icon Ural and the seventh one is going to be priority all right looks great again we are still missing the action hook which is actually going to trigger this function so let's create a constructor for this class all right so in this constructor we are going to add the action and the action hook is going to be admin menu oops and just call the function add menus and that is it now I like to compact my code by putting the name space in the first line with the PHP tag like so okay so now that's done it's not going to change anything in the front end as we can see because we are not calling this file anywhere we are going to call the menus controller in the main plugin file so here we can use the use keyword to require or include any file that we born now after the use keyword we have to define the name space of the file that we want to include so in our case it's going to be tap had been in the name of the file which is menus controller so app has been manuals controller now since we are including this file we can call it like so neoman is controller and that's it now let's take a look in the front-end and see what happened oh ok we can see there are some others so this is what I was talking about we can immediately see all the notices and warnings ok let's try to figure out what we did wrong seems like the priority that we are setting in cannot be a string yeah that takes care of the error but it still does not solve our problem let's try to take it out of our array and let's see if that solves the problem it does so in our case you were defining a false priority but instead it needs to be a number so if I give it five as you can see here up here so let's just give it 94 now typical so this worked now if you click on it we are not seeing anything so we have to echo the page content for now so this basically wraps up the video we were successfully able to include the menus controller file using the psr-4 autoloading and with the help of the PHP use syntax now this was a very basic example to demonstrate how we can implement peers are called autoloading and leverage the PHP namespace in convention for a wordpress plugin as mentioned earlier in my next video I will be showing how we can improve this whole structure and how we can properly call all these files in a more liable inspired manner which is going to be really interesting since this video is already getting too long I will be ending it now so stay tuned for my next video I hope this helps and thanks for watching
Info
Channel: LazyCodeLab
Views: 601
Rating: undefined out of 5
Keywords: php, wordpress, wordpress plugin, composer, psr-4, autoloading, lazycodelab, development, creators, clean code
Id: rOFmgbcMCDw
Channel Id: undefined
Length: 16min 50sec (1010 seconds)
Published: Fri Jul 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.