Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
In WordPress: Creating Custom Widgets and Plugins with PHP, Drew Falkman teaches PHP developers how to create custom functionality for WordPress 2.0 through 3.0 using widgets and plugins. This course starts by installing and setting up WordPress 3.0 on both Mac and Windows, then provides an in-depth look at tasks related to these WordPress add-ons: installing and administering, building and customizing, creating editable options and database tables, working with posts and pages, and utilizing jQuery and AJAX. There are also tutorials dedicated to promoting a widget or plugin, adding security, and localizing the interface. Exercise files are included with the course.
Actions are the events of WordPress. That is to say when you want something of your own making to happen inside of WordPress, you are going to use an action. If you want something to happen when a menu is generated, you'll use an action. If you want something to happen when the users saves a comment, you use an action. So the process of writing plugins that do things is really a process of writing what you want done, then associating that with one of WordPress's many actions. To do that, we are going to use the add action method, which we will talk about in a second. But the first thing we need is a plugin. So I have created this file here, cc_ comment.php in my plugins directory.
You can see I have set up all the comment information so that WordPress administrator can read it and see it. Now I need to essentially write the function that's going to do what it is I want to be done. In this case, I am going to set it so that whenever someone submits a comment on the front-end of the web site, it's going to send a CC e-mail to some particular e-mail address. So we will create a new function. We'll call it cc_comment. And it's a good idea to create your functions with some special name. You may want to get more specific than this, so that it doesn't collide with other functions in the WordPress environment.
In the function itself, I want to have access to the _request scope. That way I can get the information that has been submitted from the form. In order to do that in my functions, I am going to declare it using the global keyword and then typing $_REQUEST. I now have access to the request scope, and I can use whatever variables I want to inside of it. The next that is to set up some variables that are going to store information that's going to be used by the e-mail. to, we can send this to firstname.lastname@example.org.
We will set a subject, and in the subject, we are going to say, "New comment posted @ yourblog," and of course we could use some other ways to access what their blog name is from the information, but for now we are just going to keep it simple. Then _REQUEST, and the request scope is all stored in associative array, and the name of this subject field is subject. Then I am going to set the actual message. I'm going to say, "Message from:" and note that I'm using the dot, which is just the concatenator in PHP.
So in a sense, you just append this on to my string, REQUEST, and then there's a name field which has the name of the user that they submitted. And we will say, 'at e-mail,' and then we can use a concatenator add REQUEST, e-mail. Then we can use a concatenator and add on the actual message. And I am going to add a new line, and that field is called comments.
So now I have got all my variables set up. I am going to actually do the mailing. I am going to keep it simple and use the built-in PHP mail function, and you can see the arguments it takes. The first three are the to address, the subject, and the message, and I don't need to send any additional headers or parameters. So I go ahead and just pass the fields that I have just created, to,$subject. So there we have it. That's my function. This however is never going to execute. If you remember, when I activate a plug- in, it will run any code that's not in this function, and currently, I don't have any code that's not in this function.
So what I want to do is outside of this function, I want to add this is an action. So I use the add_action method, I pass the name of the action--in this case it's called comment_post, and I know this from the WordPress Codex--and then the name of the function that I want to execute for that action: cc_comment. So that's all it takes. If I now go to my WordPress Plugins page, you can see my plugin right here. When I activate it, it will now set it up so that any future comments that are submitted will send an e-mail along with it.
If you should ever want to remove an action, you can use the remove_action, and it takes the same arguments. You specify the name of the action and then the name of the function that you want to remove. Of course, we don't want to do this because we've only just added it, but it gives you the flexibility to do that. So WordPress actions really allow us to get into WordPress and do all kinds of stuff in all kinds of places. We looked at one particular action, but there are tons of these. There are hundreds of them. The way I like to think of them is they're like power outlets around an empty house: all you need do is plug your appliances in wherever you want to, and then you can have that in that room.
There's one less thing to mention is there's a third argument here, which is priority. Priority allows you to determine what order you want these to execute in. In this case, it doesn't really matter, but in other cases it might. I recommend, to become familiar with this, is going through the Codex and looking at the plugins API, and in addition, you can check out Adam Brown's web site at adambrown.info.
Find answers to the most frequently asked questions about WordPress: Creating Custom Widgets and Plugins with PHP.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.