Viewers: in countries Watching now:
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.
Shortcode is a way of allowing non- coding users of WordPress--since version 2.5-- to output some type of feature into their pages and posts. In this video, we'll discuss how you can create your own shortcode functions in your plugins. Let's go ahead and log in to my blog and have a look at one of my posts. If I go and look at the HTML, you can see I have this tag here with the square brace, caption id, and it has some attributes assigned to it. Then at the bottom, I have a closing tag.
This is a shortcode. What this does is this allows for the generation of this little tag around it and the placement of the caption underneath. So let's create our own shortcode. I have a function already written in the plugin, called smp_map_it. What this does currently is it's just a function, it can be used as a template tag that takes an address, and from that address it will then output Your map and then a 256x256 image using the Google Maps API.
If we want to turn this into a tag, for starters, we need to remove this attribute, and let's just set a static address. In order to use it then as a tag, we need to use the special function called add_shortcode. We then define what you want the shortcode tag to look like. In this case, I am just going to call 'map-it', and then you assign the function name that's going to be used to output your shortcode-- in this case, smp_map_it.
Now we'll log into our admin, and in our Plugins, we'll go ahead and activate our Map plugin. We then need to add shortcode to one of our Posts. We only have one, so it makes it simple. You need to be in the HTML View. So make sure you click the HTML tab. Underneath here, we'll just put map-it. We update it, and then load it in the front page of our site, and you can see it outputs our map with Your Map.
Notice however that it outputted above our content, which isn't necessarily what we want to do, and we don't really have any control. Maybe we want to have something other than the White House mapped out. In order to do that, we need to add some arguments to our function. The first one is going to take our attributes. The second one is going to take content. This is used in the case where you have an open and a closing tag. The content will represent the data in between that open and closing tag. To set up your attributes, there is a special function called shortcode_atts.
This allows you to pass an associative array of variable names that will set up default values for the attributes. So, in this case, I'll create a new array. I am going to create one value called title and set its default to Your Map. Then I'll create another value called address and set it to an empty string. Then I pass in my attributes variable. Now what I am going to do is instead of outputting the HTML directly, I am going to use a return keyword, because this function is actually going to return the stringed output where our shortcode tag is.
So, I am going to first add an
tag, and then I am going to concatenate on
the value from the attributes for title.
Then I am going to have all the same HTML code up to the base_map_url, which is
the location of the Google API.
So, I am going to close that with a single quote, concatenate it on, and then I
am going to change my address variable to now look at the attribute address, and
I can erase the rest of this PHP code.
So there is my string. So now it's returning essentially the same HTML that was output, but it's returning it as an HTML string. To use this now, I can go back into my Post, and I can pass in these attributes. So I can say title="Our Location:" and address='100 Main St. Santa Monica, CA'. We update the post and we refresh, and now you can see, it's mapped out our Santa Monica location in the correct place of our content.
If I did have something in between the open and closing code, I can output it at any point in this HTML using the content attribute. However, be careful, because in this instance, if you have any shortcode in this content, then it will be ignored. So if you want to make sure that it gets used, you want to use the do_shortcode command and wrap that around your content. This will allow you to have nested shortcode. The last couple things to mention: if you ever want to remove shortcode, there is our special remove_shortcode, and it takes the same arguments as the add_shortcode, and you can also do remove_all_shortcodes in any sample where you want to use that.
So, WordPress gives us the ability to add functions non-technical admins can use, using simple tags called shortcodes. We can create these as functions and allow for the setting of attributes in wrapping content. It's yet another tool at the disposal of WordPress plugin developers.
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.