PHP Basics for WordPress - A Beginners Guide to WordPress PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the channel hope you're doing well wherever you are today's video is gonna be a very special tutorial where i'm gonna teach you the basics of php but specifically for wordpress so if you'd like to become a wordpress developer and develop your own themes and plugins and functions it is essential that you learn at least the basics of php because wordpress was actually built using php you don't have to become a master at php that's not what this course is about this course is intended to just provide you with the basics of php so that you would have learned enough to be able to become a wordpress developer or start learning about wordpress development so coming up i'm going to teach you what php exactly is how it's different from other programming languages i'll show you how you can write php code properly and then i'll also show you how you can combine php with html in the exact same file then we'll move on to the main topics of this course where i'll show you how you can write your own wordpress functions i will introduce you to the concept of hooks actions filters i'll show you how you can work with the if else conditional statements and tags i'll also show you how you can work with variables and arrays and finally we'll talk about the single most powerful function in all of wordpress which is the almighty wordpress loop so by the end of this tutorial you'll have learned enough of php to get started in wordpress development so if this sounds like the kind of tutorial that you're interested in let's not waste any more time let's get started before we get officially started with the course there are a few things i need to mention and clarify first of all this is purely a basic introductory course to php it is not going to be an intermediate level or an advanced level or anything like that this is purely for beginners so if you're new to php this is this course is going to be perfect for you uh second for wordpress ideally you should be an intermediate level user of wordpress you can be a complete beginner you need to have an intimidated level knowledge of wordpress you should be comfortable working with wordpress in order to take this course now there are two main websites we will be using one is php.net which is the main official website for php second would be the wordpress codex which is the main repository for all functions and anything pertaining to wordpress development so you can bookmark these two pages now if you're working locally on your computer or on your mac you don't need ftp access you're fine however if you're trying to work on the website that's live on the internet you're going to need ftp access because we're going to be working with our theme files we will be using the wordpress uh 2021 theme by the way the official 2021 theme so in order to access the theme files on the server you're going to need ftp access if you don't know how to create an ftp account or how to get ftp access i do have a full tutorial here on youtube showing you how you can do that now i'm also going to be using notepad plus plus as my text editor we're going to make changes to our files and then upload them back so i will be using notepad plus plus for that again i do have a full tutorial here on youtube showing you how you can work with notepad plus plus but if you have another text editor you're working with maybe like sublime for example you don't have to use notepad plus plus it is not compulsory at all and then last but not least i do have a full course about wordpress theme development it's available on webmonkeyacademy.com it's also available on udemy in fact my main purpose of making this short free course is to introduce you to wordpress development and then in the hopes that if you're convinced that hey i really want to become a wordpress team developer you can then sign up for the more advanced course that goes a lot deeper uh into wordpress uh theme development so you can check it out if you're interested and then there is one thing i forgot to mention as i was making the course and that's because i took it for granted and i don't want to take anything for granted okay uh php files uh you end them with dot php for example right here you can see all these files right now fourfold.php archive.php for the php and so on there's a php file so you end your files your picture files with dot php and then finally you open up a php code with the uh less than symbol question mark and then php so right now this would be the opening of any php statement and then down here where you have the question mark and then the greater than symbol that's when you're closing the php code so you have to open up your php code and then close it to avoid any thing breaking so i forgot to mention this during the course and it was only when i was going through the videos i realized that oh i forgot to mention this very very simple fact but it's important that i need to mention it because it's something people can take for granted so uh that's it i hope you enjoyed the course let's now get started officially with the learn php for wordpress course okay so let's start by talking about what php actually is and what it does and php stands for the hypertext preprocessor and what it does is that it actually retrieves data from a database so basically it is a server-side scripting language and that's what separates php from other languages like say javascript most other languages will do their job on your computer or on your browser but php does its job on the server side so if you're trying to access data from a server maybe it's a web server or a database server you would use php if you're trying to send information or data to a server php will also be involved now i would highly encourage you to bookmark this particular website it's called php.net and it's the main website for everything related to php you can take a look at the documentation and uh a lot more information there about php so how does php actually interact with wordpress what's the relationship i don't know if you've noticed this before but whenever you install wordpress typically you would also create a database as well it depends on how you install wordpress or what hosting panel you use but in my case i use a siteground and when i try installing wordpress from the backend i always have the opportunity to name the database table and do things like that so when you install wordpress you're also creating a database as well now it is in your database where most of your files are stored in your pages your posts your user data passwords all those kind of information is stored in your database now i'm going to give you a live example in here i do have one of my demo websites the moviecharacters.com it's just a sample blog with some sample posts and things like that now i'm over here on siteground the backend for siteground you can see i do have the website moviecharacters.com now i've accessed the database and this is the database right here the name is america618 underscore wp329 okay i'm going to expand the database and right now you can see i have access to different kinds of tables and even from the name you can kind of tell what they're about down here as an example you can see i have the uh all the way down here okay oh i'm sorry it was right up here okay wpde on the skull posts all right you see this table right there i'm going to open it and i'm going to show you some stuff if i go over to my website right here you can see i have posts related to movies and games uh titanfall review you can see i have kangaroo box that you like hits the web uh legion makes 900 i have tags like action or terminator 2 i do have a post about terminator 2 as an example if i was to go to my database right now can you see the post titles in here you have box office numbers rising terminator 2 review uh acquire please review there's another one here terminator 2 on its own and so on so right now you can begin to see the titles of the post that i have you can see the post dates the actual post content itself and then if i scroll over here to the right you can see all the information like the published status whether it's published or in draft mode the comment status whether comments are allowed or not uh if the post has a password that you need to use to you know to access the post the post name and so on and so forth post modified and a lot more information so right now you can see that all this information is on the database if i go back to my database and i opened up another table just to give you another example down here users wpde underscore users that should tell you that this table contains the user information people who have accounts on the website so right now you can say i do have two login ids movie and developer you can see the emails and you can also see some other information about the particular user account so the old point here is that whenever i try to do something on my website or anybody tries to do something on my website say for example they clicked on bruce willis words for the fifth time in order for wordpress to be able to display this post display the featured image the title the content and so on it has to go to my database and pull the information from there using php now how wordpress knows what content belongs to what post is through the id all your posts have ids i'm not sure the post id for this particular uh post but check this out i can actually find out if i was to go back to the posts table and i searched for bruce willis waits for the ninth time uh let me scroll down here and see okay it's down here you can see the id is actually 59. so when i clicked on the link for that post wordpress knows that oh the id for this post is 59 let me go to the database using php and search for the content for the post with the id of 59. that's basically the relationship between uh php and wordpress so once again php is used primarily to pull information or send information to a server it's server side scripting language it does its job on the server side and that's what separates it from other languages like javascript most other languages will do their job on your browser php does its job on the server side so in the very next video i'm going to show you a lot more about the relationship between php and wordpress and how our php is actually used to create different kinds of functions in wordpress let's now talk about how to write php code properly in other words we're talking about php coding standards and i do have quite a list in front of me so let's get started the first thing i'm going to talk about would be uh how to name your functions and your variables and in here we have a classic example we have the function 2021 or widgets underscore in it the job of this function is to register a sidebar but notice that the actual name of the function is widgets underscore in it but they start off first by saying function so we have function and then you want to add a prefix before the name the prefix in this case is a 2021 and the reason why you want to add prefixes to your function names is because there could be another plugin somewhere on your website that uses the same function and if both your theme and your plugin are using the exact same name for the function you could end up having a conflict so to avoid that it's best to just use your own unique uh prefix it could be the name of your theme it could be your own name it could be anything just have a prefix before the actual function name next would be the use of dashes and underscores when do you use them typically you would use your underscores whenever you're naming your functions or your variables in this case right now but the prefix we have 20 underscore 20 underscore one underscore widgets underscore in it so you would use underscores to separate words whenever you're naming your variables or your functions you would use dashes when you're trying to add a css class in this case right now down here you can see it says h2 classic calls widget dash title so when you're adding classes or ids you would use the dash you would also use the dash whenever you're naming your files in here in the content folder you can see we have files called called content accept content on content page content dash single that's when you would use your dashes don't worry as you work more with php you'll get used to when to use the underscore and when to use the r dash all right next would be the use of single quotes and double quotes now right here i have the example where basically echoing a statement i am sorry this is the header of the page now one way i could i could rewrite this would be to simply say uh i am sorry this is the header of the page okay however in most cases you would typically use the single quote single quotes are used a lot more often than double quotes now this is the ideal way you would write this kind of code echo single quote and then the html code i am sorry this is the header header of the page but what if you wanted to use the short form of i am so right now i say uh i'm without the a you can't have a single quote inside of another single quote it will break the entire code so what you could do in this case right now would be to wrap the single quote inside of double quotes so right now you can see oh sorry about that right now you can see i am using uh the double quotes to wrap the single quote inside of it now the code would work you can't have a single quote inside of another single quote and you can't have a double quote inside of another double quote in most cases the double quotes are actually inside of the single quote the reason why is the reverse to where you have the single quote inside of the double quotes because this is english when you write i am sorry you don't use double quotes you typically would use the single quote however if i was to go back to my functions.php right here notice when we're adding the title the h2 class equals widget title the name of the title is in double quotes while the actual html code is wrapped in single quotes so once again you can't have single quotes instead of single quotes you can't have double quotes instead of double quotes and in most cases the double quotes would be inside of the single quote you would only use a single quote inside of a double quote when you're trying to write sentences like this in english i'm sorry i didn't shouldn't stuff like that okay next would be the closing php tag now in most cases when you open up your php tag you should close it right however don't be surprised that you may see some files where all you see is the opening php tag but you never see a closing php tag here for example in the pages of php we have the opening php tag but there is no closing page better right there that's because there is no need to do so in fact page base is one language where you can open it up add code add code add code and you never have to close the php tag you could only close the php tag if within that block of code you want to add code from another language maybe like html or javascript in this case right now everything in here is pure php code there is no html there is no javascript in fact if you go to your function.php file at the very end maybe some themes might not do this but the vast majority of themes the functions.php file you will never see the closing php tag nah it's just going to be right there empty now this was the opening php tag right up here and you can see right now that we've had we've had several functions there's a function right here uh there's another function of idea all do another function another function all the way down until we wanted to add a javascript code that's why we closed that opening php tag right there we added our javascript code once that was closed we then opened up the php tag again until down here once again so in most cases it's safer to just open and close your php tags okay but don't be surprised if you do see files where there is no closing a php tag okay the last two would be identitation and comments so if you've worked with html code before your css code you're already familiar with this concept make sure your code is is well indented so that it's very very easy to read as an example right now you can see this function to register the sidebar you can see how it's all indented i know that this right here this is a block of code for registering sidebar and then right here for example you can see how you have the main array up here and then inside of the average you have the child always with all the properties and values you can see how everything is laid out properly so keep that in mind and then let's talk about the our comments how do you add comments in php comments are a good way to add commentary to describe what a function is doing so the ideal way would be to have the forward slash and then your aesthetics and then you have your comments and then to close the comment you would have your asterisks and then the forward slash again like in here as an example okay that's how typically you would write your comments you don't need to use uh the single aesthetics symbol before the actual comments it's not necessary but a lot of wordpress users prefer to do it that way so if you want to stick to the original standards uh by all means you can do that so that's pretty much it there are still a few other coding standards that you will come across but these are the most important ones that i've covered so far so hope you enjoyed the video and i will see you in the next one so now let's talk a little bit more about how php is used in wordpress now i've switched things up a little bit i do have another website in here another sample website and right here let me just quickly show you i do have one post and it's titled php for wordpress and it's a typical wordpress post with a title featured image and some content okay and then down here you can see we have the category of dev tutorial and some other information let's go back to the back end i also have a page called recipes let's view the page and that's the page right there okay i am using the default wordpress 2021 theme by the way now i'm gonna go over to my uh text editor in here notepad plus plus but i'm connected via ftp to my 2021 theme and you can see we've got the files right here and the first file i have opened in here is the functions.php file this is basically where the vast majority of the functions used in your theme are located now if you scroll down in here you'll begin to see different kinds of things like array you'll see names of functions you will see if been used a lot if else while all these are php okay you can see down here array array array in here we have another function if is customize preview do something right here you do have the function for creating and registering a sidebar you can see right here it says register sidebar and then the array basically has different kinds of information about that sidebar like the name the id the description and so on okay let's move on you now have the index.php and this is easily quite possibly the single most important file on in any wordpress website or any wordpress theme the reason being that this is typically the main template file that wordpress uses to display content on a page now wordpress will use this particular file if there is no uh if there isn't any author file that is higher in the template hierarchy what i'm trying to say here is that how does wordpress know how to display contents of a page and then how to display contents for a post you know those are two different post types right well here's the thing you do have a file or a template file here called page.php this is typically used for displaying pages okay now to prove it to you i'm gonna do something right here okay right here at the very very top i'm gonna say this is a page and i'm gonna save this change note i'm saving it in the page.php file now if i was to go back to my website this right here is a post remember php folder is a post if i refresh the page if i refresh this post you can see right now we don't see the this is a page statement right but if i was to go to my back end and i viewed the recipes page let's view the page right here you can see right now it says this is a page that's because wordpress using the template hierarchy knows that okay this is a page it checks to see is there a file called page.php oh there is a file called page.php let me now use that template to display the contents of the page and because i went to my file right here and i added this as a page wordpress outputs this is a page okay so going back to the whole template hierarchy thing if page.php did not exist in my theme wordpress would use index.php it's kind of like the fallback option it's always the fallback option whether you're displaying posts pages or even content from other custom post types index.php is always the last option if there isn't any more specific file now you've seen page.php displaying contact for pages right let me let me remove this comment right here real quick let's cut that out let me remove my space let me save this now the file that is being used to display contents for my posts is the contents single dot php template file now right here i'm going to do the exact same thing i'm going to paste the same content and i'll say this is a post let's save this and as you can imagine i'm going to go back to the backend right here now i'm going to refresh my page you can see nothing is showing up but if i was to go to my post right here and i refresh the page or the post weather you will see that oh that did not display uh let me go back and see what happened this is a post okay let me just make a quick change once again i'm not quite sure what happened there okay so it definitely saved right now so if i was to go back and i refresh i think it should be down here actually it should be around here oh this is this isn't the main i'm sorry i'm an idiot this is the home page this isn't actually the the actual uh post page itself this is the home page for the website so if i was let's now go to page before press the actual page itself now you can see it says this is a post i'm sorry i do lose my train of thought when i'm recording it's not easy okay so you can see it says this is a post specifically for this bus right here okay that was close i'm gonna go back and i'm going to remove this okay and once again wordpress is of course using php to pull in all this information now there is something else that you should be aware of when it comes to php and wordpress there's this thing called template tags template tags are kind of like pre-built php functions that are used to display certain kinds of information one of the best examples i can give you is right here in this template file the content.single.php so here's the thing wordpress is this plane let me just reverse this page wordpress is displaying the title of my post right it's displaying the featured image it is displaying the content and then down here it's even displaying the publish date the author and even the category of the post how does wordpress know how to do this that's because of something known as the template tags the template tags are pre-built functions that display certain kinds of content dynamically i'm going to prove it to you if i was to go back to the template file right here for our posts do you see this thing right here called the title php the underscore title this right here is a template tag used specifically for displaying the title of our posts that's why we have the title showing up if i was to go right here right now and i remove this let's just let's just cut it out and i save if i was to go back to my post and i refresh you can see right now the title is no longer there it's gone that's because we moved the template tag let me go back and i will undo what i did let's add another template tag right here we're going to display the category of the post just beneath the title and how can i do that i can make use of another tag known as the category it really is that straightforward so if i was to go right here and i changed the title to the category and i saved okay i'm gonna go back to the post file right here refresh the page and right now you can see we have the category dev tutorial that's basically what template tags are there are many many many template tags and in fact i should have opened up a page to show you uh what the different wordpress template tags are and i've opened them up and i didn't want to do it live because my internet here is pretty pretty bad so i had to pause the video open up the page and here it is so if you go to codex or wordpress.org forward slash template underscore tags right here you can see so many of them you have the general tags uh get header get future let me show you those by the way if i was to go back right here and i go to uh page.php actually let's go to index.php this is the main template file right here i'm sorry not index.php page.php let's stick with page.php you can see right here it says get header get header and then down here it also says get footer these are functions that pull in the header of the website and also the footer of the website now if you look over here to the right in my theme folder i do have the file called header dot php it's a file right there used specifically for displaying contents in the header which will typically be your logo as well as your main menu now if i was to go up here and i did the same thing and i said this is the header and i save this file as you can imagine if i was to go back to my page now because the header is global the header is on every page on every post if i refresh this page right now you can see right up there it says this is the header if i was to go to my post and i refresh it also says uh this is the header and so on now likewise you also have the exact same thing for the footer.php as well so these are functions that will pull in pages header.php thread.php you'll come across them a lot more as we are progress okay let's go back to the template tags in here so you have other ones like you know get the sidebar are the login form archives author information category information comment tags and so on and so forth you're more than welcome to spend time going through all these tags if you wanted to but that's basically it for wordpress and php again as you progress in this course you'll begin to understand more of the relationship between php and wordpress hope you enjoyed the video i'll see in the next one now before we begin writing proper php code i want to quickly cover one very very important topic and that is how to combine html and php in the exact same file now i'm going to use the title template tag as an example let's say we want to display the title of our post but using the h1 tags from html the easiest way would just be to open up the h1 tag first of all and now open up php and say the underscore title close it up and now close the h1 tag all right i'm going to go ahead save this and let me come back in here refresh my page and there it is php for wordpress very very straightforward let me walk you through what i did so the whole point here is that we want to display the title in the h1 format so we opened up html first of all with the h1 tag now we open up php edit the template tag the title then close it then we close the h1 tag and that's exactly what we got this by far is the easiest most straightforward way of combining html and php whenever possible make sure you have your php code inside of your html code it's very very straightforward and it's the easiest form of syntax to work with we could do the reverse where you have the html code inside of the php code but then you would use echo or print which are functions on the php but in all honesty i don't want to cover that because it gets a little bit tricky and it can be very very messy however there are certain situations where you can't avoid having html code inside of php code now i've actually created a function in here to show you what i'm talking about and i'm going to walk you through what is going on in here all right let me just first of all indent this properly okay so the whole point here is that we want to display the text you're very very well you're very welcome but using the p tags actually you know what let me change this to h1 just so we have some consistency all right so we want to display you're very welcome but in an h1 format but it's going to be with the use of a function so right here let us go ahead and save this first of all just to confirm that it actually works so let me go back in here refresh the page and there it is you're very welcome okay let's go back to the code right now what's going on in here i've created a function called our print greeting it's php so i have my opened php tag right there function and then the name of the function now because we intend to introduce html code we have to indicate that hey html code is coming up next that's where this opening curly brace comes in it's the signal that coming up next is going to be html code however because we need to close the php tag before we can introduce the html code that's why we have to close it right here you can see we've closed the php tag but because of this curly brace the function knows that hey coming up next is going to be html code that is the html code right there and now because we have the opening curly brace we need to close it and because we've already ended php because we had to introduce our html tag we have to open up php again now close the curly brace we have the closing curly brace and now finally close the function and then down here we simply called the function no we will cover functions more uh later on in the course don't worry about that but that's basically one scenario where you will have html code inside of your php code and you have to use the curly brace and the closing of places to include html within php there are so many other situations where you will have html and php being combined but these two are the usual or the two most common so um that's it thank you for watching and i'll see you in the next class all right so let's first of all take a look at the conditional statements with php and as a wordpress developer you will come across conditional statements every time they're very very very common and i thought that it would be best before we talk about functions to actually talk about uh the else if and conditional statements so right here in php i do have the documentation pulled up and you can see right now it's very very simple very easy to understand conditional statements basically just check to see if one condition has been met and then if that condition has been met do something otherwise if the condition was not meant then you can do something else in this case right now you can see a very very simple example you have two variables a and b and we've said the very first condition if a is greater than b if the value of a is greater than b then echo say a is bigger than b and now we're saying else if which means we're now providing a second condition if a the value of a is equal to the value of b then echo a is equal to b and then the third condition else if this first two are not met which means that if the value of a is not greater than the value of b and if the value of a is not equal to the value of b and obviously the value of b should be greater than the value of a so echo a is smaller than b very very very simple uh to understand and let me just stress right now that you don't need to have an else if statement you can just run one single condition if this is this then do this you don't have to set the alternative for okay well what if the first condition was not met what should php what should wordpress do if you don't set that then wordpress will simply not do anything so i'm going to give you some examples in here directly now from wordpress we do have something called the conditional tags these are text that we created by wordpress already that help to check if setting conditions have been met so let me just give you a few of them right now you can see it checks to see if it's the home page if we're on the front page you scroll down now you check to see if it's an admin uh let's go down a bit more uh it checks to see if a post is sticky if comments are active and so on you can check for so many different kinds of conditions if a particular page is in the category with the id number nine and so on like i'll encourage you to take a look at the page because you have so many conditions however i want to give you an example and the conditional tag we're going to work with is the has post thumbnail this checks to see if a post has a featured image attached to it now over here for my post i do have an image right here we can see the php featured image now what i'm going to do is i'm going to create a conditional function that will check to see if my post has a featured image and if it does we're going to change the size with which the image is actually displayed now if i was to go to my notepad file in here and i go to content.single.page with the 2021 theme we do have this function in here called the 2021 post thumbnail this is the function currently displaying my featured image and my post however we're going to change it okay i'm going to go ahead now to remove this function okay now if i save this if we go back to the post and i refresh right here you can see right now the featured image is no longer shown because i have removed the function cool let me now go ahead and grab this function i wrote earlier let me copy that let's now go back to my notepad file and right here right here i'm gonna go ahead now and paste this and what do we have here all right check this out i have created my conditional statement my conditional function in here that says if and now the actual conditional tag has post thumbnail so first of all i'm checking to see if my post has a thumbnail then display the thumbnail this condition this tag this template right here the underscore post underscore thumbnail is what is used to display the featured image of your post just like uh the underscore title is used to display the title the underscore except is used to display the accept the underscore post underscore thumbnail is the template tag pre-built in wordpress that will display your featured image now the post underscore thumbnail template tag can accept the size of the image that we want to use in this case right now i have set the size to medium so now i'm telling workplace that hey if my post has a featured image display the featured image above the size medium i'm going to go ahead now to save this and if we go back to my website to refresh the page now you can see we have the image now been displayed in the medium size now please keep in mind that by the size we're not necessarily talking about how large the image is going to be the reason why is because with the 21 with the 2021 theme it has been designed in such a way that the featured image is going to occupy a setting width and height as such the size with which we're displaying the image will determine how clear or how blurred the image is going to be now because we're displaying the image with the medium size of 300 pixels by 300 pixels which is the default with wordpress wordpress is now going to stretch the image to occupy the initial width and height that has been set by the theme that's why it's looking very very bloat but guess what if i go back in here and i change medium to an even smaller size which is thumbnail which i believe is 150 by 150 that's usually the default uh i think i misspelled thumbnail thumbnail not thumb mail let me save that again now if i go back check this out okay i'm going to refresh the page and now can you see it's even a lot more blurred now it's even a lot taller right now because i basically forced wordpress to stretch the image that's why it's looking all very very blood right now but that's not the whole point of this video the point here is just to show you that we are actually able to change the size of the image now if i go back in here and i change this now from thumbnail to large okay now the image is going to be a lot clever because we're displaying it with using the large size now if i refresh now you can see that the image is now very very clear because we're no longer stretching it with the use of the medium size or the thumbnail size so that's basically a conditional uh statement right there you will come across them in so many situations and going back to my file right here like even in the header.php actually you don't have any of the conditional statements in there if i come down here to my single micronutrient.php file right now you can see one conditional statement in here that says if and then is not singular notice you have this sign right there okay this right here is the not not value so basically i say right now that if this page is not a single up page then uh do something okay so this is another symbol that you come across a lot with conditional statements now let me also mention one thing real quick because it's important you may have noticed that with this if statement there is the end if condition right here so basically we're ending the conditional function you don't always have to use the end if if you're running the if statement okay it's not always necessary however for good practice i would recommend that you always close your if statement so in this case right now up here i will simply come come back in here and say php and then end if and there it is head the semicolon and then close the php tag it's good practice but let me just mention that you're not always going to find developers who would always close every single one of their if statements in my case right now the reason why i did not close this initially was because within this block right now i'm not wanting any other conditional statements like this is the only if statement in this block however if i was going to run another conditional statement within this block i would have ended the first one but like i said it's often good practice to just end all your if statements and to avoid any potential issues with your code so that's it thank you for watching and i will see you in the next class okay so let's start talking about functions and how you can write your very own functions using php in wordpress now previously we saw this particular function that i created called our print greeting and the whole job of this function was just to display the text you are very welcome and it's very very straightforward you have the function and then you have the name of the function you will have your brackets and then you will have the open curly brace to introduce html you close it up and then right here this is where the function gets called okay let me show you something else right here if i go to php.net and then take a look at the function reference right here you will see all the pre-built functions available with php now this can can be used to do so many things like you know check the day to today's date it can check whether a user is logged in and so on and then down here if you scroll through you begin to see the different kinds of extensions uh right here you have the date and time related extension so if you want to do something with the time this is where you'll have the available functions now one of them is the date function very very very straightforward it simply displays the date now there are different ways how you could choose to display the date so as an example down here you have access to the different examples so if you said echo date and then in brackets you have l this would simply print something like monday tuesday wednesday if you wanted something different like let's say monday 8th of august 2005 and then the time you would use something like this and so on okay i'm gonna go over to my notepad file in here let me grab this real quick go right here to my header.php and i'm gonna go ahead now and open this up let me paste the code right there so what i'm doing right now is i'm simply echoing today's date in my header i'm going to go ahead now and save this let's go back to the browser go back in here refresh the page and there you go monday 31st of may 2021 now notice that i didn't have to create a function or anything like that all i simply need to do was just to use the actual php function directly date so this is from patriot directly and i simply used echo to print out the dates in this particular format so that's one way how you can create your own function so that i use functions directly from php but what if you wanted to use your own functions right here's the thing with wordpress we already have access to a very wide variety of php functions now if you go to developer.wordpress.org and then you search for theme functions right here you will have a bit more explanation about functions.php file and things like that now the whole job of functions are like plugins plugins are used to perform certain kinds of actions on our website you could have a plug-in that does something very very simple and of course you could choose to install that plug-in but guess what you could even write a function your own function that does exactly what that plug-in is supposed to do so in other words if you're capable of writing your own functions you will not need uh certain kinds of plugins so as an example take a look at this all right i'm going to open this up right here you have your functional reference on the wordpress now just like with php that has several pre-built functions wordpress also has its own set of pre-built functions that you can use to either create your own new set of functions or you can simply customize them so right here under the function of reference you have access to plenty of functions you have those on the posts or custom post types pages so anything related to a page or a post and so on you will have all the functions right here and then under categories and tags you have them right here as well let's go down you can see so many and then anything related to like the user email address user password things like that you will have them in here you have your feed functions and then you have anything related to comments ping back track back functions and so on you scroll all the way down here you have simulator functions and so on and so forth okay with that being said let's create a very very simple function that will check to see whether a user is logged in or logged out now right here we actually have such a function which is called is user logged in all this does is that it checks to see if a user is locked in or locked out and then you can then do anything you want the function to do so let me go over here to my file and i'm going to grab this code real quick let's go back to a notepad file right here i'm going to remove this let me paste this out here and let me explain to you exactly what is going on in here so what we have here is a very simple php code that checks to see if a user is logged in and then if the user is logged in we're going to echo welcome registered user else if the user isn't logged in just say welcome visitor so this is what we refer to as a conditional statement or a conditional function if you prefer basically we're offering two alternatives it's either the user is logged in and if the user is logged in then just say welcome registered user but if the user is not logged in then just say welcome visitor i'm going to go ahead now to save this and let's see what it's going to do i'm going to go to my firefox browser in here now as you can see right now i'm logged out i'm not logged in so let me refresh and you can see it says welcome visitor because i'm not logged in however if i go to my chrome browser all right you can see i'm actually logged in right now as the admin if i refresh now it will still welcome our registered user so you can see right now that the function actually works properly now going back to wordpress right here there are so many functions in here that will allow you to do so many things you can check to see if the user is using a mobile device or not you can print out the title of a post you can check to see if it posts as a featured image and so on so what we've done in this particular video is just very very very simple functions to do certain things join me in the next video where we'll now take a look at more advanced functions let's now take a look at some more advanced functions and specifically hooks actions and filters what exactly are these a hook is a very generic term that is used to describe a location or a spot where one piece of code can interact with another piece of code now don't worry you will see what i'm talking about in just a moment now when it comes to hooks there are generally two types all right you have actions and then you have filters an action literally acts it does something so let's say for example you wanted to create a scenario where once you've published a new post on your website you want wordpress to send out a tweet to all your subscribers the action of sending out that tweet is an action right so basically that's what actions are and right here if you go to the wordpress codex and you search for the action reference in here you will see a whole list of different actions that have already been created by wordpress which will do something for you at a particular point so say for example you wanted to do something before the theme is loaded this would be of course the advanced stuff but right here you have the action called setup underscore theme when you run this action you will be able to do something before the theme is loaded you can use this action register our sidebar to basically create side bars and so on but what about a filter a filter is used to modify data before it is used or displayed on your browser that's basically what filters are so filters they change things they change stuff in fact a very easy way to describe actions would be they do stuff while filters change stuff one other major difference between actions and filters is that an action would not provide you with any data to work with actions just act they do their job and then they get out filters on the other hand will give you something they'll give you a piece of data to work with before they now run the function a classic example would be the typical uh excerpt that you have for wordpress by default i think it's about 50 words or 55 was that wordpress would use for your excerpt if you wanted to change the number of words in your post excerpt you will need to use a filter because you need to get your hands on the accept first and then modify the number of words and then you will now pass your modification back to the filter which will then run let's take a look at some examples in here of an action and a filter and right now i've already written out a function so let me just quickly grab that right here and of course we're going to go to our functions.php because this is where we typically would add our function our actions and filters so what is going on in here by the way i'm not sure if i've mentioned this but another way of adding comments in your pdf i will just be to add the forward slash two times and then you can add your text or your comments so what i'm trying to do here is that i am trying to add a statement that will appear on the front page but then if it's not on the front page don't do anything so this is right here the function is called uh add promo text that is the name of my function and you can see right now i've added a comment that says if we're not on the home page don't do anything otherwise if it is not the front page then i want you to return or print out hot promo is back again so to take this a little bit further i'm using echo to print out the statement hotkey mode is back again but then we're also using the h2 tags to wrap it up to make it a little bit bigger so once again check this out i am saying that my function is called add promo text right in fact let me expand this just a little bit so you can see it a bit clever so once again the name of the function is add promote text okay and the job of this function is to echo hot promo is back again only on the home page because right now it says if we're not on the home page don't do anything otherwise if we are on the home page then echo this out that is the function however we now need to add it as an action and that is the statement right here in fact you can actually even have this uh add action at the bottom okay so sometimes you might see some developers add in the action first and then the function in other cases you'll have the function first and then the action so we have this actual action or function itself called add action and for add action it requires two parameters the first one is basically going to be the name of the action in this case right now it is after header and then the name of the function which in this case is add promo text fine we've written out the function we've added the action but now how do we actually execute this action keep in mind that just creating a function on its own doesn't necessarily mean that it will run you need to call the function you need to run the function yourself so how are we going to run this well let me go ahead and save this first of all okay and then let me just quickly grab the code right here real quick to action i'm going to go over to my header.php and right here this is where we want to print out the statement so i'm going to say php i'm going to paste the code add my semicolon and i'll close the php code this function right here do action this is now the function used to call out an action that we've already created and in this case right now the name of the action going back to functions.php is what after header that is the name of the action so right here we are now telling wordpress to do do the action called after underscore header i'm going to go ahead now to save this and let's see what the results are let's go back to wordpress to my site i'm going to refresh the page and okay so right now okay we are on the homepage so it says hotmo is back again however if i was to go to the post for page before the press you can see right now there is no hot permit is back again uh let's go to the archives page for example you can see it doesn't show up until we go back to the home page and then it says hot promo is back again so to give you a quick recap this right here is the action that we've created called after header and whenever you're adding an action you need to first of all name the action which is after header and then you need to provide the function which you want the action to actually run that function is add promote text and this right here is the actual function and then going to the header.php this is where we want to call that action that we've created and we know to do so we need to use the function called do action and then the name of the action this right here is what you might call a hook because now we are hooking our header.php to our functions.php via the action called after underscore header that's basically an action at work okay now let's go back and let's talk about filters right now just like with actions filters basically i'm sorry not unlike unlike actions filters modify data they change data before it's actually used or displayed on the browser and just like with actions you have access to a wide variety of different kinds of filters and right here you can see many of them you have filters for pages posts image sizes and so on let me give you a very very quick example let's say we wanted to change the number of words in our excerpt right now the default i believe is 55 characters but let's say we wanted to make the except only four characters how are we going to do that i want you to realize first of all that there is a function called the excerpts this right here will display the post except on its own all right keep that in mind now what i'm going to do is i'm going to introduce you to a filter called excerpt length this filter right here has been created by wordpress specifically for us to be able to use to modify the excerpt length you can see right now it filters the maximum number of words in a post except check this out i'm going to go ahead and grab my code right here okay let's go to our functions.php and then down here i'm gonna paste that what is going on in here i have my function called my custom except length and check this out i am returning 30 characters 30 words and now just like with add action i am adding my filter and just like when you use the add action you first of all need to provide the name of the filter we are working with which is what excerpt length this is the filter from wordpress that we're using called accept length and then just like we do action you now need to provide the name of the function that you're using to modify the accept which is my custom excerpt length however unlike the action where we had to say do action and then provide the name of the action because we already have a function called the excerpt we don't need to do anything like that all we need to do is just to call the except we've written out the function to modify the the except so all we need to do right now is just to call the excerpt and how do we do that let's go to our content.single.php and right here just below the title i'm going to say php the underscore excerpt semicolon and close let's go ahead now save the file and let's go back to our browser let's go to the page right here let's go now to the single page for php right here and there it is there is our except right here in 30 words let's actually confirm that it's true i'm going to go back change ah 30 to 4. okay just to make sure it's actually accurate let's save that 4 is a lot easier to count than 30 so let's refresh and there you go one two three four that is a filter in action we use the filter again because we needed to get our hands on the accept first and then we then modified the number and then we sent it back one other thing i want to mention is that when it comes to using actions or filters we do actions right you do an action this is the function it's called do action but for filters we apply filters so when you're going through your wordpress files you will see apply filters in some locations while you will see actions you will see do action you don't do filters or apply actions you do actions and apply filters there's a lot more to actions and filters but at this point you have a basic understanding of how they work and what they are thank you for watching and i'll see in the next class so now let's talk about php variables and arrays and how they are used in wordpress now think of a variable as a container that can store the value of something that something could be a function it could be text it could be a number it could be anything now according to documentation in here variables are typically represented by the dollar sign now i've always found that rather interesting like why did they choose the dollar symbol as the sign for a variable it's not important that's a whole topic for another day but usually you will have a variable that will start with the dollar sign and then you will have the actual name of the variable so that's how you can identify a variable and there are different types of variables used in php you have integers integers would be your numbers you know zero one two three you have string string will be your text uh you also have your boolean variables which would be uh true or false uh and so on now scrolling down here you can see examples the first example in here you have a variable var equaling bob so the vowel of the variable val is bob and then notice the second one in here that has the value of joe this variable here has the capital v so please note that variable names are case sensitive if one is smaller teddy that one is a capital letter those are two different variables entirely so when you try to echo out these variables you're going to have bob and joe and in fact let me give you a live example in here i've already written out the code so let me go over to my notepad uh let's just jump down in here open up my php dump the code and then close the php so what you have here right now is that i have created a variable called text you can see the dollar sign right there text equals php for life so this is an uh a string variable and i'm now echoing out the text so instead of me writing echo php for life i'm just simply echoing text because text variable has the value of php for life and of course if i save this and i go back to my website right here and i refresh the page up there you're going to see a php for life so very very straightforward you can also assign functions to variables i have another example in here i've written out this is a this is a function that we've taken a look at earlier the one with the date so let me just go ahead open up my php tags again dump the code close it out there so again we've assigned the variable date to be equal to the function of date the php function date and then in brackets we have l which means that it will represent uh the day of today basically i'm gonna go ahead now to save this and then if i go back to my website right there and i refresh the page you can see that today is thursday so php for life and today is thursday so that's basically how variables are function going back to the manual in here they have so much more examples on how our variables are used but let's move on now to always because this is actually the more interesting so in here according to documentation it says that array in php is actually an ordered map a map is a type that associates values to keys as an example right now you have your array right here you have the key and then the key will have its value you have the second key that will have its own value and so on basically think of an array as a particular function or a variable that has a set of attributes or characteristics you can think of your regular text in your website right a text can have an array and that area could be things like okay we want to have a specific color for this text so that color right now would be a key and then the color will have a value is it red is it blue is it green you can have the font size that font size could be the key so the value could be 26 pixels uh 30 pixels it could have a fun family which would be monstera to tahoma and so on that's basically what an array is you will find always a lot in wordpress and i do have several examples in here to show you if i go to my functions.php right here this is the function to register a sidebar right so it's called 2021 widgets and then this is the actual pre-built function register sidebar but now note that we have the array and in the array we now have several very uh several keys which would be the name for example what is the name of the sidebar oh the value is it's called the footer what is the id the id is sidebar dash one uh what is the before title we're going to wrap an h2 class before the title and then wrap the closing h2 tag after the title and so on so right there you have the array with the keys and then the values of those keys i'm going to give you another example up here and this one is actually very very very interesting right here so you have this function called add theme support this is a pre-built function with wordpress that allows you to add our customization options to your theme now in this case right now we're adding the support for a custom background i'm going to show you something very very cool because i want you to see how this actually works if i go to customize this is for the 2021 theme just to remind you if i go to customize right here and we go to my apologies my internet is acting all weird again today okay so if we go over to colors and dark mode right here you see background color we have the ability to customize the background color because of this function right here add support for custom background color now look at this we have the array default color is set to d1e4dd which is kind of like a lightweight of green if i go back to my website right here do you see the background color this is the default and if i click in there what's the value de1 for dd it is right here but notice that down here we have other clothes that we can quickly choose you have like the black color you have another shade of black you have like a pink and so on all these values are right here do you see them this is the black variable that has this value dark gray has that value orange has that value and so on so the thing is as you begin to work with wordpress you will come across variables and arrays always are very very common open up your function.php of any theme and you will see them everywhere all way away away you will see the keys and then you see the values of those keys so that's it a very brief introduction to the world of variables and always thank you for watching i will see you in the next class no cost about wordpress and php would be complete without talking about arguably the single most powerful function in all of wordpress which is the wordpress loop what exactly is this i've got some books in here with me and don't worry i'm not trying to sell them to you i have three books in here right and each of these books has its own title they have their own author their own number of pages uh their own classification and so on let's imagine that you're a librarian and it was your responsibility to record all this information about each and every one of these books you could do it manually but what if you could actually create a function or a program that could scan these books and then automatically put out the information that you want that would be awesome right now think of these books as your wordpress posts think of that program that can scan this post and split information as the wordpress loop so the job of the loop is basically to go into your posts and then start picking out the information the title of the post the author of the post the date of the post and then we'll display it on your website that's basically what the loop does now i do have another article in here what the loop can display it can display the next post link the previous post link the category of the post the author the content the accept the id the short link tags title time it also works with conditional tags as well if this is the home page then display this if this if this is not the home page do that and so on now let me open up my notepad file in here and right here if you open up your index.php in fact any wordpress theme i don't care how sophisticated the theme is you're gonna have a loop in your index.php and right here this is the loop while half post it starts from if half post so basically we're saying if the website has posts and as long as it continues to have posts then do this right now you see this function called get template part this is basically a function that links one uh one file to another file so we are linking the index.pitch right now to the uh to the content dot php file which is inside what you have here your content.single.php file so we're linking the index of page file in here to this particular file right here so once the loop has started in the index.php this is where we now begin to output the information so we have the title you have the thumbnail you have the content and so on now of course you can customize the wordpress look to display certain kinds of information you can tell the look that hey instead of displaying all the posts display posts only from this particular category instead of displaying posts why not display my custom post type of movies in fact i've written an example in here just to show you real quick how it might look like so let me just dump that right here real quick so right now you can see we've done what we've created a variable called augs this could also i could also call this loop custom loop and so on but for the purposes of this video afternoon my variable args and this variable now has an array we talked about arrays previously and look at the keys the first one right here is the post type so now i'm telling the look that hey pick the information from the post type of movies now we're not dealing with regular good old wordpress posts by dealing with movies and then for the category name pick movies that are from the specific category of action action movies only right and then post per page that is in this loop only display three movies this would be how you can customize the wordpress loop so again the job of the loop is to go into your posts or whatever custom poster that you've set it to focus on and then begins to pick out the information that you want the number of posts the category of the posts and so on and then using our template tags like the tie tool the excerpt the content we can then dictate what information the loop should display that's basically how the loop works there's of course so much more to the wordpress loop which i will go into of course in my main uh wordpress theme development course but for now i think i've been able to give you a brief introduction to the wordpress loop and what it does and how it actually works thank you for watching and i will see in the next class so that's it we've come to the end of today's tutorial where i've shown you the basics of php for wordpress and if you're interested in becoming an actual wordpress developer why not check out my course title the complete wordpress theme development course is available on the webmonk academy my own private platform it's also available on udemy however if you decide to take the course on my own platform i'm going to offer you a 10 discount check out the links in the box below for more information so this course will teach you everything about what president development will talk about how you can create your own page templates your own custom fields custom post types how to write your own wordpress functions how to create your own sidebars your own menus basically by the end of the entire course you'll become a real wordpress developer and you will be able to take any wordpress theme develop and customize it as much as you want so if you're interested i'll be sure to check out the course if you have any questions or comments be sure to put in them in the comment section below if you enjoy the video do give it a thumbs up share the video with anyone who feel my benefit and if you haven't subscribed to the channel please do subscribe hit the bell so that you're notified whenever i upload a new tutorial my name is alex it's been a pleasure i'll talk to you next time cheers
Info
Channel: Web Monkey
Views: 10,962
Rating: 4.9204545 out of 5
Keywords: php tutorial for beginners, php wordpress theme development, php wordpress developer, php wordpress course
Id: mgGAvq9hmyU
Channel Id: undefined
Length: 76min 14sec (4574 seconds)
Published: Thu Jun 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.