 Okay, so today we're here to talk about PHP for WordPress. Why should you listen to me? Well, I teach PHP at Treehouse. Those are the stickers I was handing out. I also lead the PHP user group here in Portland. And I've been a web developer for about 20 years now. I'm just going to stick with 20 for the rest of my life. I think it's a good place to stop, right? Okay, so anything that we're going to be going over tonight, all the code that you'll see today, this morning, this morning, will be found at this GitHub repo. So you can find it right there. And you'll have the links. And I will also share the slides so you'll get to see everything here and you'll have that information. Okay, so we're talking about PHP for WordPress because WordPress is written into PHP. I actually really like WordPress. I've done a lot of WordPress development myself, even though I don't get to do very many Meetups anymore. I have three little kids and running the user group, the PHP user group takes up most of my free time. So I'm looking forward to soon again being able to go to the local Meetups because they're really fabulous, the PHP. The WordPress community here in Portland is fabulous, you guys are great. I've been to many of the Meetups in the past and they're just phenomenal. So I'm really excited to actually start doing a little bit more as I get to get out a little bit more as my kids are getting older. So today, we're going to be talking about two things. We're gonna start off the first half, talking about the basic set PHP. How many of you have done some sort of programming before? Woo, everybody has done some sort of programming before pretty much. So most of this should be review and we'll go through it really quick way that. And then we're going to get into, part two is building a plugin and we're going to just so that we can talk about what that means for, what PHP means for working with WordPress specifically. How do we hook into WordPress and how do we make that work? And hopefully not put our code into our feet. Okay, so we're going to start out with some of the basics of PHP. So I have a demonstration for you and that's why I was handing out some candy here. People that start out with programming and we have our variables as you, most of you should well know, variables are very common and useful pieces of information in programming, hard to do much of anything without a variable. A variable is just a way to store something, to reference later. So I handed out candy, some of you who may have had Reese's Peanut Butter Cups or whatever, but that's what you're storing, that's the value. So as we hand them out, you're a piece of candy, that's the value that you're storing. Maybe it's an M&M, maybe it's, maybe it's a take five, maybe it's a Reese's Peanut Butter Cup, that's your value. Everybody has their own value and like with variables, we'd have to call, we would want to know people's names to be able to say what candy do you have. Ben, what candy do you have? I have an M&M. Okay, so if I just said randomly, hey, what candy do you have, nobody has any idea, that's how variables work. We need to name them so that we know what we're talking about and as we go. So, we get a little bit more complicated when it gets to arrays. You probably all know about arrays, so bear with me, okay? Let me start with arrays which are M&Ms, that's why I gave everybody on the front M&Ms to demonstrate our array. So, M&Ms are like our arrays because M&Ms have individual pieces of candy within the bag. So instead of being an individual item, there's multiple items within this item. That essentially is an array. Arrays are great because they let us do a lot of interesting things with arrays. Whether you are making your own arrays or you're pulling an array out of a database, working with objects that have arrays, they're very useful. There are a lot of array functions within HP that we can do for sorting, just like we do. How many of you sort your M&Ms before eating them? How many of you eat your favorite color first? How many of you save your favorite color for last? Okay, so we do this. We do this as people. We sort things all the time. And arrays allow us to do that. We get to work with data in an array, and actually do things with it. Sorting it, filtering it, doing things with this data. A very powerful way to do that. Okay, so now we get a little bit more complicated. So, another great things we can do with arrays is nest arrays. And when you get nest arrays, it starts to get complicated and people start to get confused a little bit when we start nesting arrays. That just means we're putting an array within an array. An array can hold any bit of information, any type of variable, so it can also hold an array itself. Well, this is just like our candy bag contains multiple M&M bags that are themselves arrays. So now, we have multiple M&M bags within our bag. It's like we have an array of candy in our bag. Arrays are really interesting and a really powerful way to do things. One of the great things about each P in arrays is you don't really have to call them different things. Things like JavaScript gets a little funky. They have their regular arrays. You can do associative arrays where you can name the key, but then it's not readily in an array anymore and you can't do any of your array stuff on it anymore. So PHP is great because PHP just lets, they're all arrays. Works with arrays, you can do array stuff with it. But, you can also name the key. This is called an associative array, just like we did with varities. So when I called out badmout candy, you have that allowed me to know what we were doing. So if I wanted to give some sort of name to my candy, so in my trick-or-treat bag, as I'm on trick-or-treating, I may have different types of candy. Maybe I want to name them. Maybe I want to, in my array, I would like to have, I want to name them by the name take five. I have my take five bars and I have how many of those do I have. I want to count them that way. There might be different ways. So I can name the key or I can name the value. And those make it really powerful over how we can store data and what we can do with it. So arrays are nice. We're not going to get too complicated in arrays, but it's a really fun thing to do and it's just a really fun way to get into PHP. So I would encourage you to play around with arrays because we're going to talk about functions a little more and PHP has a lot of array functions that let you do some really neat things. We'll play a little bit. Okay, so next up, we're going to do some quick variables. I'm going to go through this because I'm going to review our whole plugin that we're going to write in PHP. And then once we get how it works in WordPress, you'll already know all of the inner workings of this plugin because we're going to go over it here. Okay, so all of the things that we need to know first are scalar variables, are Booleans like our workday equals false, integers like our hour equals six, our price might be, would be a cloak where we can give it the dollars and cents and our quote here is a string. In PHP, you can concatenate, which means join together strings in multiple ways. This really the period is your thing to look for when you're joining strings. You can just have the string, it ends. We have another, we have a period here, joins the string again, that's joined. Or I can start over dot equals. If I just did an equals, it would replace everything that was already in that string. But if I do a dot equals, it adds it to the end of that string. The other type of scalar, it's not really a type of itself because this could be in store any type of variable, but we have our constants, which should be constant, which means they don't change. So the idea to use them is to use them as a constant. Although you technically can change most constants, it's not a good idea. Use something else if you wanna change it. So we want to know that they're constants and one of the ways that we know that we should be working with a constant is that we uppercase like this. And you'll notice that I'm more pressed. Here's a nice one I'm more pressed for making sure that your admin is actually using SSL. So forcing SSL and admin equals true. It's a Boolean, but it's a constant Boolean that should be there. Okay, so a couple more little things about PHP. Most of you should know if you've done PHP before, this is how it looks. There's always an opening in PHP and a closing in PHP, except when some of the files are at the end. Now, I'm going to warn you, WordPress, the way WordPress combines things, you're probably better off just putting your closing tag at the end. Most of the PHP files you'll find in other places, leave closing tags off at the end of the file, but starting and ending PHP WordPress doesn't give you things. So if you're going to end with the closing tag, just make sure that's the last thing. The reason that some places leave it off is because if you have a space at the end, I can start to do weird things that you're not expecting. So just be careful that the closing tag is actually the closing tag in your file. So here are a couple little quick things on how we work with variables. These should all be fairly benign and easy for you to catch because these are all just regular math stuff and nothing complicated. You don't have to be a math whiz to do programming. My hint for doing math is to, so that you're sure what is actually working here, put in parentheses what you want to happen, then you don't have to worry about what's actually gonna go first and what's not gonna go first. Okay, first I want multiply and then I want this divided over here and then I want to add those values together. Then I use parentheses, I know that's what's happening. So another thing with PHP is that you do not have to tell it which type of variable you're using. You don't have to specify the variable in some languages you do. PHP has new variables and will mold itself to whatever mood it has by whatever information you give it. So you may start with the variable total, that is an integer, but if you then add or subtract or multiply and make it a float, it is now a float. It just changed itself to a float and it's fine with that. So in PHP 7 now you can set up strict types so that they can't change. It can be helpful, it can also be helpful. But just remember PHP generally, doesn't matter what types of variable it is, it'll mold itself to whatever it is that you are using. It's just a whole different name. Okay, and then a couple other things you should know. The echo command comes into the screen. The bar dump gives you the details about it. So like it will say it's a float. If I bar dump my total here, it tells me it's a float and the value. And then I bar dumped the force SSL admin that we saw on the last page and that's a Boolean of true. So I actually got more details about what type of variable this was and the value. Whereas if I just echo, if I, and the differences between single quotes and double quotes, another thing that people often run into, if I have double quotes and I put my variable within double quotes, it is going to use the value of this variable. So it's going to use the 18.5. Within single quotes, it's going to use the variable name. So it is going to use dollars sign total. Those are the different, that's the main difference between single quotes and double quotes. Escaping characters, that stuff is affected. If you want, if you want character by character exactly as you type in, you'll want single quotes. That's why a lot of people just use that by default. And then if you want something to actually execute within that string, like putting the value of a variable, you'll want to use double quotes. So that's what the difference is. You can use either one. If you're not putting a variable or escaping a value, it won't matter. It doesn't act any differently other than that. So yes, you can use either one. Okay, so we talked about arrays. So here's the array we're going to be working with today. We're going to be doing a random quote generator. So we're going to set up some quotes. So we're going to store our quotes in an array. You can also store them in the database. We're just not going to get into database calls today. So we're going to store it in an array, which is fine as long as you don't want to let any users actually change it. So we're going to store our motivations in an array. So we have an array of strings. And here are our quotes that are within this array and they are string values within the array. Now, I can also, I can add a quote from our previous screen here. We had this quote here. Quote variable. With this lovely little square brackets here, easy as way to add another value to my quote. So I can add, subtract things from an array. There are lots of things that you can do. Look at array functions. They're really fun. But easy way to do it, I'm just adding one more thing to my array. So we have arrays. To access an array, you would use its key. By default, if you're not giving it, if you're not specifying the key by giving it a name, it is an indexed array, which are numbers. So it counts the items in the array essentially, which is really fast. And if you can do an array with just numbers as the key, that'll be the fastest way to do things. But if you need more information about it and you want to do the key, that's perfectly acceptable to you. So the thing to note about arrays and their indexes is that they are zero index. That means the first item in the array starts at zero. It does not start at one. So if you would to call motivation one, it would actually take the second value in the array. So that's something that often gets people hung up because, wait, wait, the count doesn't line up. That means we start at one and arrays start at zero. We actually start at zero when we count in our head because we start at zero and then we count one, which is really interesting. My children love to do Alexa. We have an Alexa at home. And it's really interesting to watch them interact with an Alexa. And it's interesting to see how they act. So they ask, if we were talking, see what you could do with math and counting and stuff. Count to 100 by 10s. She started 1, 11, 21, 91, before she made it to 100. So she never got to 100. Whereas if we were to count by 10, we actually started at zero. And we count by 10. So she started at one and then counted by 10. So it's really something that will get you tripped up if you're working with arrays. So keep that in mind. That's the only reason I'm keeping adding on some more story here so that you remember, arrays are special in how they handle things. And it may not always be what you think. So arrays start with zero. Just remember. OK, so functions. I promised we would get to functions. Functions are great because functions are a way to repeat code without having to write it over and over and over again. So we want this block of code to perform a specific action. And it can either return a value or just perform an operation without returning anything. PHP has over 700 built-in functions that you can use, that you don't have to write, that it will already be for you. And it's a good idea just to use the ones that are there and not have to write the function yourself. If it's already there, you don't have to write a function that counts an array, for instance. There are lots of things that you can write, and they're useful. But there are a lot of built-in functions that you can use as well. So here are just some of my favorite, the top-use functions out there. These are built-in functions with PHP, just to give you an idea of what sorts of things you can do. The substring returns a part of a string. You can do the string length. You can trim the white spaces. By default, it'll trim white spaces. You can trim anything you want. Like, for instance, you might want to trim the, a, and, words from the beginning of a string if you're trying to alphabetize books. That would be another thing. You can use trim to do that. You can left trim and right trim as well. Those functions are there for you. You can find a position of a string, string position. You can find the first occurrence, this starts again. If you're counting, again, to remember, just like arrays, if you're trying to find the first occurrence of a string, it'll tell you how many characters in. And guess what number it starts with? Good thinking. OK, so another thing, string to lower, that's used often, or string to upper, string replace. That's useful to know. You can do, you can do pattern matching as well. You can do arrays. You can match multiple things in one call, either multiple keys and multiple values replacements. Or you can say, I want to see all of these, all of these different things replaced with this one thing. So it's powerful. We also have a couple array functions here, like array merge or gt functions, checking if there's an item in the array or returning all the array keys instead of the values. OK, so we're going to write a little function here. We've got a little function that we ourselves have defined instead of using a built-in PHP function. One of the things in WordPress that you're going to want to get used to doing is namespacing by adding your plugin name or whatever in front. This is how we keep from conflicting the things. Because if you're trying to overwrite a function, sometimes it works, sometimes it gets errors, sometimes you replace a function that shouldn't have been replaced, most of the time you're not trying to overwrite a function. If you are trying to overwrite a function, there are ways to do that too, and that's fine. Just make sure you're doing what you want to do. You can overwrite a function or you can create a new function. If you're creating a new function, it's good to give it a name that's going to be unique. WordPress suggests that you name your functions as you're building your plugin or your theme and so that they don't have a complex. So I'm using sketchings here and then my quotes. And my functions usually accept some sort of data. They can accept as much or as little data as you want. We're just having it pass right here the category that I want. I want quotes in specific categories. I'm going to have two arrays. I didn't write them all out here. There are two arrays, the motivation that we saw back here. And then we have another array of encouragement. So two different types of quotes we're going to use here. We're going to check the category. Again, we're going to do... I can nest different function calls. It's going to work its way out, so it's going to start with a category and it's going to trim any white spaces off of it. And then it's going to convert it to lowercase so we don't have to worry about somebody typed it weird. And then we're going to check if it's in an array. And the array we're going to use is right here. We could have either set it above or put it right here. Here's our array. If the category is motivation, motivated, motivate, we could go on and on with whatever words we want for our motivation stuff. We want to return the motivation array. That's the array we're going to use. If we said we want to look for some sort of motivation, if we said we wanted to look for some sort of encouragement, we're going to return the encouragement array. And if not, either of those, we're going to merge those two arrays so we have all of the quotes to work with. Okay, next we're going to talk about conditionals. We do conditionals all the time. How many of you woke up this morning and consciously thought, should I go back to sleep or should I get up now and take a shower before I go to work? Okay, that was a conditional. That was a conditional that we use every day in our life. We make conditionals if this, then that. If it's raining outside, I might want to put on a rain jacket. I might not want to wear the skirt that rubs in the mud. Certain things that we look at and we consider what happens. If this, then that. There might be multiple considerations like we're going to talk about here. I wrap this in a function so you're not going to be surprised as we use this function later in our script. Both. So we've got this, again, sketchings and then our random quote. So we're going to do a couple conditions here. We're going to check if the category is not empty, this little, the exclamation point beforehand means not. So if I want to say not, I use an exclamation point. So if it's not empty, then I'm going to pass it to the category and get the quotes from that category. We're going to add another conditional. So to do a second conditional or the 5,000th conditional, which I would not recommend, you can always use an else if. So else if the hour is greater than four because I don't want you waking up before then. There are crazy people that like to get up at 4.30 like my CEO. And then we're going to say we want you to be done by five. So we're going to call this 17 hours. So if it's between those hours, this is going to be our working time and we're going to give you motivation for working. These are our work hours. We're going to give you motivation. Else, we're going to give you encouragement. We're just going to encourage you. You're done with your day. You need some encouragement. So then once we have our quote that we're going to work with or that we're going to work with all of them or that we're going to work with our motivations or encouragement, we now have our array that we're going to work with. And we're going to use another array function called array ran. And we're going to, it gives us a random key for that, from that array. And then we say, I'm going to use my quotes with that index. Whatever that random number was that you gave me, it'll get one of those numbers. You don't have to do a count between this zero and the count of the array. You can just say array ran. So again, another array function that can be useful. And we're going to return that single quote that we got after all of that and our conditions. Okay, so loops. Again, these are, we had our functions, our conditionals, and now our loops. Things that we're going to do to our variables. We're going to be working with variables. We're going to be outputting data. We work with data. Variables are just data. So there are different types of loops. Here are some, here are the different types of loops. Depending on what you're going to do, you can use any one of these that you want. There's lots of great documentation on php.net, which I have two at the end of the slides. We're going to talk about one here, one of my favorites, which is for each, a for each loop because it's really great. You can grab not only the key, the values, but the keys in the array, or just the key, the values, whatever you want to do, but you can loop through each item in the array. This works great if you want each item in the array. If you don't want each item in the array, you should not use for each, okay? But in this case, I want each item in the array. So what I'm going to do here is I'm going to replace text in my post. I'm going to replace some text and give it a little tool tip, quote above that text to encourage or motivate me. So what we're going to do here is for each search. Here are my search. I'm going to look for these words. There's a lot more words. That's what I did in this ellipse here. There's a lot more words I didn't put here. Put all of the types of motivation and encouragement. I want these to be words. I want them to have a space in front of them whether that'll allow it, if it starts a sentence or whatever, but it doesn't have that as part of the word. There are different ways you can do this. This is an easy way, mostly catches it. So I'm going to say, I want just the word motivation. This would also catch motivations, but it wouldn't. Then it would ignore that mass at the end and then our script would be a little bit weird. So if you're doing, if you wanted both of those, you probably want to put your D or your S, your longer one first, it'll find that first, and you're good. So there are a lot of little things that you can run into here, but I'm just going to give you a few little tips here. Play around with it, you'll have this script. So we're looking for these different words. So that's our search. So for each word, we're going to start with motivation, okay? So our first time through, our word is going to be motivation, and it's going to take our content, which we passed into this function, and it's going to, this I replace, you saw a string replace, I replace means that it's case insensitive. You don't have to worry about the case. So I don't care what the case is, but if you find a word with the space in front of it, then we're going to replace that word with this span, and we're still going to have the word there, but we want to put a span tag around it. So we have our span tag, the class of our tooltip, and then we trim the word so that we didn't put in a double space there. Or when we pass this to here, actually, that actually, I think it's supposed to be here, because we don't want, no, that's fine. That is right. So we find our word, we replace it here, that doesn't need a trim. We had the trim inside this one. So we replace it, and here's our span, there's our word, and now here's where we're putting our quote. So within this span here, we're going to generate a random quote. So it's going to be hidden. This class, the tooltips text is hidden until we scroll over this one, and then this is going to be shown. So it puts our word here, gives us this quote, that's what we're going to do, and we're looking in our content once again. We're looking in our content and replacing our content with our replacement here. And then we're going to return our content. That's how this function is going to work. We have our for each here that does all of our work for us, and we return our content. We pass our content, we did what we wanted, and we returned our content. So this is the basics of PHP, and you just wrote your whole plugin right there. We just went through everything we're going to do in our plugin. So now you've seen all of the code that we're going to use for our plugin. We're just going to work on writing our plugin now and seeing how we tie that in to WordPress. Look at that, I'm making great time to you, just perfect. Think I had planned this. It's great, and it's really easy to make a plugin. Your minimum one file. I make one file has the name of my plugin in it. You have to have this within a folder. I can upload this file, and that will be my plugin. It does nothing, but it is a plugin, and you can activate this plugin. That is how easy it is. Please do not put your functionality into the functions.php file in your theme. Fix your theme or change your theme when you've put your functionality into your functions.php file. Put it in a plugin. It's really easy to do. I just generated a plugin right there. That is all it takes from the start of this down because that's the name of my file. That one, two, three, four lines of code is all it takes to create a plugin. You have your first plugin right there. Okay, now we actually want our plugin to do something that's not really a plugin that doesn't do anything. Our plugin, as we went through php, we're going to be interacting with WordPress now because we want our plugin to do something to our WordPress app. We had our functionality, but we need to interact with WordPress. So how do we do that? Believe it, you understand that WordPress has a few things. One is called themes. The theme is how it looks. It should not be how it acts. Okay, WordPress does a lot of the actions for you, but if you want to perform more functionality, you would do that in a plugin. You add functionality in a plugin. You have to look and feel in the theme. And to the core functionality, using actions and filters. An action can add or remove functionality, whereas the filter changes the data. But both of those are done with hooks. So we're going to look at all of that and we can hook into the themes. We can hook into the plugins. Our themes and our plugins usually use hooks to hook into the core of what WordPress is already doing. Because WordPress is already doing a lot for us, we just want to add something. So we're going to hook into WordPress. We're going to do with our plugin. So we're going to take what we learned and apply it to WordPress. First we're going to do an action. Skip over this right now. First we need to have our function and then we can call it. But to make our hook into WordPress, we would do add action. What about giving us fence most of the time? So we want to add an action do. Add to our function. Add functionality. So we're going to be adding functionality here, which is we're going to be adding style sheets. This is something very common that you will most likely do, especially if you create a theme. But you can do this for your plugin too. If you have specific styles, in our case we want a tooltip that pops up. So that's a lot of styling. So we need to add our styles. Well, we don't need to put that into, again, we don't need to put that into our theme because it's not really attached to our theme. It has to do with our plugin. So we can have it in our plugin or we can do the same sorts of things when we create a theme. And what we're going to do is we're going to add an action and we want this action to happen when, when the WordPress head is called. Right here. So in the head, what we're going to do is we're going to call this function. Once we, once we're in the head, once WordPress is trying to do head stuff, it's going to call this function and it's going to go here. Oh, I want to register. And here's where I named it. And then here I've named my style, registered my style. I'm going to name my style, my quote style. And then again, here is a nice function that allows us to pull the plugin directory URL of this file. These are magic constant with the underscores before them. It's a constant because it's all uppercase, but it's a magic constant that PHP automatically does for us. That's why it's magic because it does it already. So the file that we're in, the plugin directory, and then we're going to go into CSS, styles.css. And then we're telling it to actually use it. So first we have to register it and then we got to tell it to use it. So we have those two steps. So we've registered it, we told it to use it. And this is where we told it to tie it in. Registered, used, and then where? Registered, used, where? Okay, that's what it takes when we add an action. Okay, so now we want to do our function. This should look very familiar because we already wrote this function when we were learning about our or each loop. So here it is. We have our function content. We're replacing our content and we're returning our content. That's what we did. Now we're going to use an add filter. What does the filter do? Hi, I'm not switching out this slide. It changes data. So we could add or remove functionality or we can change data. So we want to change data because we want to change the post on the page, right? So we're changing data. So we want to add a filter. Now we're going to filter what? The content. We're filtering the content. We're gonna call this function right here. Got James Coulton. Okay, a nice thing to know. You notice I'm not actually passing the variable here. That's kind of weird. Like, well, how did this work? Well, when you do your hook, WordPress automatically passes certain things depending on what the hook is. And WordPress, again, has great documentation of exactly how this works and exactly what gets passed. So you do not have to memorize any of this, what's being passed or what filters you can use or where exactly you can put things. You can look in the documentation. Again, I will link to that. PHP has great documentation. WordPress has great documentation. Use it, it's a lot smarter than your brain. So we're tying into the content and when we tie into the content, it passes the content. So we can now work with the content and actually do something because we want to filter the content. So it passes the content so we can filter it. Filtering it, we're changing the data. We're doing our replace on the data. So now we've done our replace, called the content to do this functionality and that's how we tied it into WordPress. We had a function, we tied it into WordPress. So next, we now tied it in so that we can see it on the page and now we want to be able to put these quotes somewhere, right? It's the other thing we probably want to be able to do with these quotes. I want to randomly put this on either a post or maybe a widget. Work codes will allow you to do this. We have, so we're gonna, oops, wrong button again. So we're gonna, again, again, it passes something and we're going to return. It's gonna create this random quote. If we have this attribute, it's not required. That's gonna be our category that, again, was getting that quote from our, getting our random quote. So all this did is it tied into this random quote here and gave us either the category or a random quote from all of them. So we added our short code. We had the random quote. I said I need to return here. So here's our random, sketching's random quote, short code, we add a short code. Pretty nice naming there, huh? Like I said, WordPress is pretty good about naming things for us. So there we created our short code and now we can use it like we've done short codes before you probably use short codes and in, I guess you just put random quote where you want it. You can put it in your page or your post or your widget. Okay, so I'm going to demonstrate this really quick. I'm gonna show you that there are resources here that I promised you. I will put these slides up for you to get and here's how to find me. Let's pull up, go back here. Are you set, maybe? Oh, no. How do I get back to my window? Browser, cool, we can do it that way. Okay, let's do it this way. So I'm gonna show you here my website. Cool tips. Where I rolled in encouragement, my motivation and over here, you'll see here my random quote here. I added it to my content and I've also added it over here to my widget area. So you probably, let's log in here. Let's go to my widgets, easiest widgets and here was my short code right there. Random quote and to pass that attribute, I said category equals motivation, so that's what's passing. There it is. What you have and I will share, you will have the code on the GitHub recap and you'll have all of my links that I had here and you'll be able to activate this plug in yourself and play around with it, add your own quotes and do whatever you want with it. Okay, thank you very much.