 Welcome back to the channel. If this is your first time here, this channel is dedicated to WordPress and Wukama stuff, so that you can make yourself business solutions that are worthwhile. In today's video, I'm going to help you understand what categories or taxonomies are, how to create one, how to remove some of these fields that are actually showing up here and how to actually add your own fields inside this particular space. So, what are these taxonomies? Taxonomies are basically a tool that you use in WordPress to help you classify your different posts because WordPress just works with different post types. For example, we have a post type, we have a pages post type, we have products post type, which is basically a custom post type. But you might want to get those different post types or different posts and then group them in different ways. For example, so the taxonomy that we are looking at is actually one that's coming from the Wukamas custom products. But if you go to posts, you'll be able to see that we actually have a category that is here. So we have that categories, which is basically what we call in WordPress as a taxonomy. So you will see that it has these same fields showing up, tag, the parent category and then also the description. Yet, when we go to the tags, you will see that we have only the name, the slug and the description. So one of the big difference that there are is that categories are hierarchical in nature. So meaning they can have a parent, they can have children, while tags themselves do not have children. So you cannot have a parent for a tag. So if you're interested in seeing this different ideas all put together, then let's jump into the video. So you could have products, let's say in a shop, and those particular products are grouped under t-shirts. So that would be a nice place to have a taxonomy for t-shirts so that you can have Adidas t-shirts or you have different brands all together that are grouped under that t-shirts category. So since WordPress offers us this platform, we can be able to create new taxonomies with our different post types, or we can be able to even add different taxonomies to the existing post types like posts and pages that we already have. So let's look at how Wukamas uses its products and how it uses the different categories and tags and attributes coming from the taxonomies. So you'll see that Wukamas uses categories, for example in my shop I have t-shirts, I have deco, I have things like drinks, I have things like music, soups and those are ways I can group the different products that I have inside my custom post type. So the first thing that we are going to do, we are going to create our own custom post type and I'm just going to run to the dev notes of WordPress so I'll go to WordPress. So I'll go look at register post type functionality that is in WordPress and you'll see that we have different things we have provide like labels, descriptions is it hierarchical in nature so that you can have different items as children or as parents post types and I'm just going to scroll all the way down, I'm just going to get this sample post type which is of a book that I need to get here I'll come back to my server I'm going WP content plugins and then I'm going to create our completely new plugin which I'm going to call taxonomies exhibit and then I'll drag and drop this into my editor and now I'm ready to start adding my own file so I'll say taxonomy taxonomies exhibit and I'll make it a .php file start it off with a .php then I'll use my start plugin shortcut or snippet that will help me fix all these things that I need to so plug in them I'll say taxonomies exhibit and then I'll change the author URI link to my github github.com slash phytiteitaki press slash taxonomies exhibit and then of course I'll add my name which is techie press and I'll say showing what taxonomies can do showing what taxonomies can do for you in what press I'll give this a version of 0.1 I'll license this under jpl2 or later so that people can fork it and use it then I'll just use the same text domain here and paste it here once this is done I'll save this I'll come back to my admin in here go to the plugins I'll activate the taxonomies exhibit plugin and then I'll just deactivate these others so I'll go back to my code and then I'm going to paste what we copied already from the other side over what press I'll save this and then let's go back here and reload if you want to know these particular arguments or the pieces that we are putting in here to have this post type then you can watch the video that is in the card that has just appeared on the screen or check the description to actually see it you'll now see that we now have this books post type that has come in and if I go to click on the books post type you'll just see it has only books and it has add new that will allow us to add new books but let's say I want to have genres in my books so what I'm going to do is I'm going to go back to our documentation inside wordpress and then what I'm going to do is register taxonomy and it is right here so in here you'll see that you have different parameters like what you will call the taxonomy the object type and then you provide the arguments that will be used inside that particular taxonomy so it would be good for you to just go and read this and see what's going on or if you want to learn how to add your own taxonomies you can also check the video that is in the card that has just showed up or in the description below to see a video that I made explaining all the different labels and the arguments on how to create your own taxonomy but we're going to see a sample of the code running inside our own plugin so let me just use what we have here I'll expand this and you'll see that this is also going to be creating genres for our book post type so I'm going to copy this code here I'll come back here and then I'm going to say add taxonomy for genres of course this is going to be a comment and then I'll paste this code we're going to save this so this code that we've imported has two taxonomies that it registers one is for writer and the other is for genre so right now what I want to do I'll save this let's go back and see what will happen if I reload this you're going to see that we now have a genre and we also have writers so let's say we have books that are fictional we can be able to add them in the fictional genre here so we have fictional the slag will automatically populate itself and we say books for fictional writing so I'll add this as a new genre and you'll see that it's actually in here so what I'm going to do is I'll say action fiction and I'll say this one is going to have a parent genre of fictional and I'll say fiction with action genre I'll add that and you'll see that this is it's put at a level below the fictional right here so if we go back to our books and we're adding a new book you're going to see that our WordPress actually provides us with this fictional genre that we can choose or we can add this to the action fiction option now I know this looks a little bit weird this is the old way of writing WordPress this is what we call the classic editor and the reason is because while I was writing my custom post type I did not allow the arguments in here to factor in for what we know as the REST API showing so I'm going to just duplicate this and I'm going to add a new option which is show in REST and then instead and I'll just clean this out on the same level and I'm going to choose here and say this should be true so if I go back to my actual page here and reload it you'll see that this actually comes with the new editor look right here you'll actually see that we don't have our books we don't have our genres and writers here we just have a few limited options so what I'm going to do is also just go and show in REST my particular taxonomies so I'll come back here in the arguments I'll add show in REST to make it true and then I'm also going to show this as well in REST so save this come reload and now you'll see that we have the option for genre here and we can choose these two and then you can also be able to add a writer let's say we're going to say this is a John Grisham writing so John Grisham when I press enter you'll see that actually this is a tag that has come in so I can also add a new genre and say maybe this is romance and you'll see that we can be able to check off any of these particular items so I'm just going to go back to our code here and I'm going to remove this taxonomy that is not hierarchical and that's why you see that in here it came as a tag but if I was to change this hierarchical to true and save it come back here and reload I'll leave this page you'll actually now see that the writers are being shown as this category that we had here it's no longer just a tag now you remember when I was explaining to you earlier this is the difference if it's hierarchical then it will allow you to have a parent and a child kind of relationship but if it is actually false like we have here as in hierarchical nature and you reload this you're actually going to realize that this is going to just be a tag so when we reload this you'll see that our writers have here as tags so I can add new tags and say John as I type here you'll see that we have the John Grisham showing up and you can have that all there so let's just focus on having a genre here and then we're going to see how we can actually affect this inside our WordPress so I'm going to come back to our code I'm going to get this writer block that we have here remove it save this come back to make sure we've not broken anything reload it you'll now see that we only have a genre showing up here I'm going to step back a little bit and you'll see that now we only have genre here so let me click on the genre and you'll see these particular fields that are available now one of the challenges that we have is that these particular fields cannot be taken away by a simple hook so the only way we can do this is by using something like JavaScript to hide these particular fields and not have them or we can even use CSS to just hide them but there's no particular way of removing them inside WordPress using a simple hook I really hope in the future this is going to change this is one of those things that I want to submit a ticket for it will be a little bit complex to do in order of implementation and it might not be one of those priority areas for the core team development right now but let's see how we can remove this using JavaScript so I'm going to remove this description box that we have here in our genres so that we don't have it at all so I'll come back to our code here I'll go down here and I'm going to say remove the description field in the genre taxonomy so let's see how to handle this with JavaScript so the first thing that I'm going to do is add an action here and the action that I'm looking at is I want to use what we call the admin underscore footer now inside this admin underscore footer we want for a function I'm going to call this function take it press underscore remove genre fields and then I'll copy this right here I'll come here and say function with this name I'll add our brackets I'll add our curly brackets as well and then press enter here because I'll space this just to make it easier for me to read and inside this function we're going to be JavaScript so the first thing I'll do is escape our PHP and then open it as well so that I can be able to write script in here which is HTML and say this will be a type of and I'll say that's equal to of course in here I'll say it's going to be a text slash JavaScript now I need to end this as well and then close off that script so that we have good HTML now inside this script what I'm going to use is jQuery which comes bundled inside WordPress and I'll say inside our jQuery let's go to the document and make sure that this document has loaded so we'll use the document dot ready function in here and then here I'll say let's have a function and this function is going to run all our different things that we want to do but we are going to pass in the dollar sign so that we can reference it as jQuery inside our code so I'll say using this dollar sign let us get the particular ID of this field so I'll come back inside here I'm going to inspect this element and we'll see that it has an ID of tag description but I don't want to only get this item I want to get this whole div right here so I am going to just get this tag-description and get it's parent so inside our code what does that look like we're going to say we're getting the ID of tag-description make sure you spell it very well and then we're going to say we need to get the parent item and then we're going to use the remove to remove that so I'll save this we're going to come back in here I'll give this a reload and you will see that that takes away that particular item now you can do the same thing to remove the slug so I'll just inspect it and see what ID it has so we have tag slug we also have if we want to take away this whole concept of parent genres we can come back here and say we want to get the ID of parent so let me just take out the tag slug because the beauty about removing this tag slug is we don't have to be so worried because WordPress itself always gives us the slug that we can use here it automatically generates it now you can see that the genre fields here have all gone and if I go back to the posts here and then go to the categories you will see that we're having the same issue down here but I don't want that to be the case we want it to only apply to the genres here and we come here and use the global we're going to tap into the global and say we want to get the current screen this is actually a WordPress functionality I'm going to just say let's vah dump this so that we can see the current screen and save this let's go back to our front page let's reload this I'm going to inspect this item so this admin wrap all I'm going to do is just say position fixed I'll remove that and you'll be able to see that we're looking at the parent file in here we're looking at the post type here which we can get and then we can get all these different items like the ID of edit genre so what I'm going to do here is come back and say so we're going to say if our current screen is actually equal to edit genre so I'll put this in the string and I'll say if it's equal to the current screen and here we're going to say edit dash genre if it is equal to that then we're going to grant this particular code so I'm going to get everything that's in here and I'm going to shift it up one level so I'll save this let's come back here I'll give this a hard reload on a reload you'll see that this is still showing and that's because in my code I said let the edit genre equal to the current screen but we just wanted to actually map the ID from the current screen and we say let this equal to the edit genre so I'm going to come back here and reload this and you'll now see that this has disappeared here but if I go to the post categories you'll see that still show up if I go to the products go to the categories you'll actually see that this actually show up so I'll go back to my genre inside the books here and we still see that we have the slug showing up here we have the description showing up here now we can use the manage columns in here to remove this particular items if we do not need them if we do not need to show them if they're going to confuse our particular users we can actually remove them so how do we do that so we'll come back to our code and this time we're going to use a filter function and I'm going to say here remove column for description and what we're going to do in this case we're going to tap the manage we're going to use the manage edit and then here we say dash give the name of the particular post type or the particular taxonomy that we are working with and then we're going to say underscore columns now after doing that we're going to write a function here so we'll open a new function here which I'll call take it press underscore remove genre columns I'll copy this and then I'll start up the function here we'll add our curly brackets to write our function so every time we use a filter it basically gives us something to work with and this particular filter gives us the columns so I'm going to write this variable columns and I'm going to say we need to return our columns as of first importance so I'll add a semicolon here and then what we're going to do here is use an if statement of course in here we're going to say if we actually have a column so if the columns and we're going to say if in our columns we actually have a description column we're going to say if that is true then we're just going to get this description column copy it here and we're going to say unset and that's a function of php we're going to say unset columns right here then you return the columns so let's go back to our genres I'll reload this and you will now see that we don't have any description showing up here so we could do the same thing even for the slug if we don't want it so I just need to duplicate this and say slug I know you're wondering how I am getting this but by default most of these things are the same or you can actually go inside the WordPress core files and you'll be able to see the names that these have or you can actually just come and inspect these items and you'll see most of them have the IDs already shown in there so I'll reload this and you'll see that our slug actually goes away so we have something that's neat and clean right here and that's all we wanted so what does it take for us to have our own fields in this particular space it's quite simple there is actually a particular hook that we can use that comes after this form that can allow us to save different items or different fields under this space so that's what I'm going to work on next if you're interested in it then let's continue with the video so we're going to use a new action hook and the hook that we are looking for is what we call the add form fields now what happens is that this is going to require the taxonomy that you want to add this particular field and in our case the taxonomy we have here is called genre so I just need to come back here and add genre and add an underscore so after doing that I'll add a comma and then we'll start writing our function which I'll say take a press underscore add so this will be the name of the function that I'm going to create here so function take a press add genre fields I'll add our curly brackets and inside here we are now going to write some particular html so I'll have to escape our php open the php here as well and then here we're going to have a div the div will have a class of form dash fields I'll press enter this whole structure from what we had before so if I come back here to the front end and I inspect this element you will see that it has a form dash field class right here so I'm going to be adding a form dash field I need to correct this and it's going to be right under that parent genre but just above the button so I'm going to come back here I'll change this to form so that we have a uniform class and then here we shall have a label the label is going to be for our genre characterization and that's basically going to explain it's going to be a short field that explains what our particular genre has as the biggest characterization of it all so I'm going to copy this right here and we're going to have an input that's going to be a type of text so it will have a name of genre characterization I'll give it the same ID also here and then I'll decide to have a paragraph that is going to explain what we are talking about so every genre has a particular characterization of it for example so EG if we have fiction we can say it is lies so I'll save this let's come back to our front end I'll reload here and you'll see we have this box explaining this the only thing we don't have is we forgot to add our particular label in here so I'll change this to characterization save this I'll come back to this place characterization and we can add our particular items here so we'll have characterization in this particular space now I'm going to come back to the genre that we have here and then what I'm going to do is I'm going to add a new field I'm going to add a new columns field so I'll add a column and then I'm going to say this is going to be for characterization I'll just copy this this is a lower case just to be like all the others up and then I'll say that's going to equal to and I'll just copy this right here paste it here and we shall say we want to have characterization so let me come back here I'll reload and you'll now see that we have this new field here that shows the characterization in here so we'll be able to add the different characterizations once we add them to this particular spot so I'm going to come back to my code here and now we need to be able to save that particular line of lines if we want to have it in here all we've done is just added the field now we need to be able to save the form field that we have added to our code and the way we do that is we have an action and the action that we are looking for starts off with created underscore and we need to add the particular taxonomy that we are using so we have genre here and then we're going to add a new function in here and I'll say this is take a press created genre fields and I'll copy this here and add a new function with this I'll add the curly brackets this particular action hook actually gives us the term ID so I'm going to get this and say we're going to update the term meta and all we need to do is we need to pass in the term ID in here which we have done so we need to add in the meta key the meta value and the previous value is usually optional so I'm just going to focus on the meta key that we want to have and that meta key that we're going to have is this genre characterization which I'll copy here and I'm going to replace here as a string and then we want to be able to pass in the new meta value and at this spot we're going to use dollar sign post and this comes from PHP so whenever we have a form that we are posting and we submit it PHP allows us to use this post to capture some of those fields that are already in the form so I'm just going to make sure I'm capturing the right item and that is going to be from this ID of genre characterization so I'll pass that in the post here we don't need this value the previous value that is optional and since this information is actually going in our database we need to sanitize it so I'm going to use a default word press function which is called sanitize underscore text field and it's going to take in a string of this particular information so I'll cut this and then I'm going to pass it in here so that we can sanitize whatever we are adding here and then word press will clean it out to make sure that our database is not corrupted by the information that we have here so I'm going to save that and then I'll come back here to our front page and download what we have here and what I'm going to do here is I'm going to add fantasy as our new genre it will have a characterization of dreams so we'll say dream world that will be the characterization I'll click add genre and you're going to see that our fantasy is added here but we do not have the characterization actually showing down here so that's something we need to fix so we're going to add our code as well to be able to show what happens in here so I'll come back to our code I'm just going to come back here under columns and I'm going to add add info to the new columns that's what we're going to be looking for and then here I'm going to just say add new fields to genre so under here is where we're going to add the new info to our columns so here we're going to use an action and here we're going to use a wordpress hook which is manage underscore and we're going to add our genre which is the name of our taxonomy and then after doing that we're going to say custom underscore columns now that will allow us to use our old function to run our code and our function will be take a press and I'm just going to copy this so that it's easy for me to follow through and paste it here and say manage genre custom columns and then I'll come here and add the new function and I'll add our brackets now in here we'll be able to have three different kinds of arguments one will have a string which is usually empty and then we shall have the columns and then we shall also be able to access the term underscore id that is for the term that we are passing through that particular column now that means we need to pass a priority here which I'll choose 10 and I'll also say we're going to pass three arguments that we have given inside this particular function this three is very important if you forget it you'll actually find that you're having errors in your code or your site will break so make sure you have the priority and the number of arguments you're passing to this function now once we get here I'm just going to use an if statement or I can use a switch so let me use a switch for some difference and what we're going to pass here in the switch is we're going to pass in the columns and we shall say whenever we are in the column that we have actually called characterization here we're going to pass it here all we want to do is we want to echo the term id I'm going first of all at the term id which needs to be a variable that is proper so I'll add the dollar sign here and then I'm going to say dash characterization that's what I want to pass and then for the default we shouldn't pass any default so we just want to have that case and then break it so what we have here is correct we add our action which is manage general custom column not columns add our three arguments we have a switch statement that passes in the case I'll echo the term id characterization then break I'll save this and when I reload you'll see that we actually have the different term id showing up here and we have dash characterization so what we want to do here in our term id we're going to use a wordpress function that says get term meta and this requires us to actually pass the term id that we already have here we're going to pass in the key and we're going to make sure that it is single so we're passing through here then our key is going to be a string of genre characterization I'll just clean everything else out here we're going to be getting this term meta that we saved here inside this particular id so I'll save this we'll come back here reload everything and you'll see that we have our dream world that we saved here and we have our fantasy so if I click edit here you will see that we don't have our characterization books anymore in order to show that field right here you can see the description we had removed before is still showing that's because on this particular page we did not remove it so we shall get this screen id and we'll also remove it from here but we're now going to first add our field that we need to have here so what I'm going to do is come back here in this particular case we use the add from fields now we are going to duplicate this code and we're going to say add add the field to the edit screen and what we shall do here is we're going to change this part that says add in this particular cases I'm going to change them to edit so that we have genre edit from fields and then we shall have this from field with this particular text so let me save this let's go back here our reload and you're actually going to see that we have our characterization here we have all this information coming in right here now of course this particular structure changes so we need to copy what we have here I'm going to inspect the element so that we can be able to see what's happening here and then we shall add it properly so we have a table in the form we are having all these different pieces of information in the different table heads so we need to fix this HTML first so that everything can work the way we expect it to so the one thing that I'm going to do is edit this as HTML I'll copy what we have here and then I'm going to come back here and then I'll be able to edit it accordingly so I'll paste this I'll tap this to go in a little bit so we have the form field we are not going to require this and then the next thing that we shall do is add a label in the table head so I'm going to just grab this cut it and I'm going to paste it here as the label we are now going to go for the input field that we have here with the ID and of course the type and the name I'll just cut this and replace the name ID right here we'll have the value which is going to always be dynamic so I'll remove that I'll leave the area required true we don't need that I'll leave this at size 40 just like it is with the other parts and then of course I'll get what we have here as our string I'll remove everything that we have here and then I'll paste our description right here so I'm going to save this let's come back reload this and you'll see that it properly fits in here like a nice shoe so we have this working very well but the only problem now that we have this we don't have our text of dream world showing up here coming from the database so we need to fix that now when we come back to this particular action we'll take a priority of 10 we need to get two arguments from it so the two arguments we get are the term and then we're also going to get the taxonomy now with this term we can be able to get the ID we can be able to get the different things that we need now in order to get this same information that we are having in our input here we need to get term meta and that's going to require the term ID it's also going to require to see if it's single so I'm going to comment this out and what I want to do first and foremost here is just VADAM and what I'm going to VADAM is our term so that we can see how we get the term ID so I'll paste this here save it, I'll come back here I'll reload this and you're going to see that we have the term ID showing up here we have the name slug and all the different things here so I'm just going to get the term ID so I'm going to just comment this out we're going to look for the term and then we're going to map on the term ID and we're going to make sure that what we have here as the key is going to be the general characterization which I'll pass as a string here and we're going to say this needs to be true so comment this out we don't need it anymore I'm going to save this as a value and I'll say our value is going to equal to this and then what I will do in this particular case inside our input we're going to bring back the value attribute we'll say that is actually going to equal to our value that we have here so I'll copy this I'm going to wrap it inside PHP that's all PHP then I'll say echo and then I'll have our value here but to make this nice and WordPress friendly I'm going to escape the attributes so that we are sanitizing this very well and then I'll save this and let's go back to our front end I'll reload here and you're now going to see that we have our dream world showing up here but if I change this from dream world to dreams I'll click update you're going to see that it's not updating anymore so what I'll do is I'll come back to our code here and then I'm going to look at this particular section where we said created genre I'll duplicate it and we're going to say edited genre and we shall just use the same function that we have here to do the same work so I'll come back here, reload this I'll change this to dreams I'll click update and you'll see that we have dreams here if I go back to our genre you're going to see that we have this as dreams in this section if I change from romance we'll say this is love I'll click update you see love is stored if I go back to some categories you'll see that our love is under our categorization so this is working the way we want it to apart from this little part of the description that's still showing up so let me go back to our code up here and I'm going to look for a different screen in this particular case so I'm going to just var down the current screen so that we can see where we are at so I'll reload this we have our information but I'm going to inspect the admin back I'll click this fixed I'll close this off and I'll scroll down here and you'll see we are on the same edit genre I'll just right click this to inspect this element you'll see now here we have it as description we need to get the parent and get rid of it so I'll just duplicate this and we're just going to get description in this place comment out the vadam we don't need it and you're going to see that now we don't have that description showing up but again we still have this table row so I need to be able to get this term description wrap as the ID that I'm going to actually just take out so let me right click this to edit and get the class of term description wrap I'll just change it here class and I'm going to remove the parent because we don't need the parent I'll just remove that and then let's see what happens I'll reload this and we don't have it there because we targeted the class of term description wrap and we removed it so everything is working like we wanted it to be we have our perfect genre showing up we have the characterization we can edit it we can do different things all together inside our particular wordpress and we're not using any other plugins so we are doing very well and that's all we wanted to do if you enjoyed this video if you think it actually has content that benefited you don't forget to give it a thumbs up share it with a friend who needs it make sure that you subscribe for other wordpress interesting stuff see you next time