 Welcome back to the channel, today we will be talking about the namespaces and custom routes in our REST API The reason is that classic press already by default gives you particular endpoints that are taken up by the software for example the OM bed and the WP version 2 and version 1 are namespaces already taken So when you go to the WordPress.org developer subdomain you find that in the REST API handbook you are given ideas of how to actually go about modifying and making your own custom endpoints and we see given some sample code of how to basically start our own route by adding custom by adding the latest post title or author and that's what we are going to just jump on and that's what we are going to use to springboard into our own idea So they give a very good explanation of what namespaces are, what routes are, what endpoints are So this is stuff that you can read and make yourself aware So you get a good explanation of what namespaces are, what endpoints are, what routes are and the arguments that are needed and it's good for you to get yourself acquainted with this information Now it would be a terrible idea for you to play around in SM namespaces assuming that classic press decides to make some changes in its system that would end up breaking your data or it would end up breaking your site So the best idea actually is for you to create your own namespaces and to create your own routes and that's what we are going to be looking at today So I'm just scrolling through and looking at the namespaces that are already existing and that are already taken up So by default this is a, we usually have our domain then we have the WP-JSON which is already by default how you add and make your own custom endpoints So when you look at the different namespaces like OMBED However if we go into the default WP version 2 which came from WordPress you find that this is where actually most of the data comes from and actually if we run this and add posts to this and hit enter we start seeing how our posts come up, our default posts come up with the different GUID, the slags and titles and everything actually comes on point So what we are going to do is we are going to create our own namespace that we are going to shoot our data from So what we are going to do is we are going to be watching this namespace area and then we shall look at the routes of what we do as we continue to code our for our custom endpoints on our REST API So what we'll do is we'll dive into our editor I've created a file called LabCustomEndpoints and I've basically put the starter files and details that are needed If you don't know what these are you should be able to watch the first video I did about the must use plugin So the next thing we are going to do is we are going to use a hook called addAction and what we are going to do is terminate it here So we'll need to use the REST API init hook and what it's going to take in is we are going to add a function that we shall call getDistricts and we shall denote it with our own name so we shall call it lab getDistricts and what we are going to do is create this function down here in our page We now need to use the default registerRESTROOTS function that is in ClassicPress and it takes three parameters One is the nameplace that we are going to be working with and I'm not going to interfere with that the default WordPress REST API name spaces that are already taken up by WP slash V2 So I'm going to create my own and I'll call it maps and the next thing that I'm going to do is I need to add the next requirement or argument that is passed in this particular function which is adding a route name route is actually the right name so we'll have our name space called map and then we'll have our route called district so lastly we're going to put our last argument we're going to put our last argument which is an array and that takes in a couple of things it takes in first of all the method and we know that all APIs, especially REST APIs all APIs in general take about a number of methods we have the get method that returns information just for the user and then we have the post which sends information to the server then we have update, delete and so on which can be used to exchange lots of information so we're going to be giving information to our user so we'll use the get and then the next thing that it takes in is that it's going to require a callback and this callback is basically a simple function that will input information on our route on our end point so I'm just going to give it a function which I'll call getDistrictInformation and this is the function it's of that we're going to use so we'll write a function for it here and then inside it we're going to use the default method of REST and SUA response which makes sure that our particular end point is actually getting some information so what we'll do is we're going to call this API works just to shout out something and this has to be returned so that there is information on our end point so what we're going to do is now is going to go to our browser and we're going to reload actually you get to see that our namespaces before were just two but now we have a new namespace here called map and so when we go when we go into our routes here, when we go into our routes we actually see that there are a number of things to pick from so we have the default ombed1, 2 and then we have our namespace of map showing up here it has a method of get and it has a number of methods in there and then finally we come to the namespace of map districts that has get and then it has also a method a route of map districts so when I click this we actually get to see what's on our end point and we actually have our API works here and in its raw form it's just a particular text so what I'm going to do now is I'm going to query for the terms of our districts and then I'm going to shoot them on this end point and so we're going to jump in and see how that looks like so now what we're going to do is we're going to pass in here a variable which I'm going to call details and basically it's just going to be an array of information now for what we need our details first of all we're going to register it as an array so we'll say it's equal to an empty array this is the old world doing PHP for an array however you can be more than about it depending on your server and just use those brackets to show that it's an empty array now what we'll do next is we're going to get our terms and we'll just say terms is equal to getTerms which is a default word term method and this method takes in an argument it takes in the arguments so you pass in an array and then you of course pass in that you want to query for the taxonomy that is called district remember that when we're registering when we're registering our post taxonomy we had district as our so you pass in district and then you also choose to say that one whole districts to be returned whether they have any content in them or not we can hide even if it says this person is empty so we hide and say this should be false hide empty should be false so we say this should be false and then we save that so the next thing that happens our content if it ends our content so we'll say for each term that is returned so for each terms as term we're now just going to look over our result what we're going to do is say let's get details we're going to push in an array of information and basics we're going to pass in what we know is a name and then for that name we're going to pass in the term and then in we know in each term something is going to come back we know that we're going to get a name as part of that term so you can actually get this information by looking up by looking that up in your browser so you can look up how to google terms so you can actually look that up and then you find that information you can actually look up how to find terms and return the names that come and then the next thing that I want to do is I also just want to pass an id just for right now and then I know when I pass this id I'm going to just get the term and then in that particular I just want the term id so the term id is actually just coming from my query of the terms when I save this here it should be equal to so once we save this and then just come back here to our here and refresh this page we actually realize that we have two pieces of information coming back in from here so if I run back into our plugin into our admin dashboard here and we go to villages and go to districts we're going to find that we have two pieces of information here so let me just add another district which I'll call Arua Arua is not anything so I'll just save this district so we have a new district folder Arua here and whenever we add anything new for our clients or the people using our API they will be automatically added with this information that I can even have someone who has no technical knowledge on how to update so having made our new districts come in here the next thing that I'm going to work on is actually the other items like the sub counties so I'm going to go through the same thing you can try to also make your counties under the namespace of map add county or district or whatever it is so that the next time we meet we're actually being on the same page if you like this video please give it a thumbs up or subscribe to our channel and make sure you click on that bell icon just doing this update enjoy the rest of your day calling bye bye