New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Introducing shortcode

From: WordPress: Creating Custom Widgets and Plugins with PHP

Video: Introducing shortcode

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.

Introducing shortcode

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.

Show transcript

This video is part of

Image for WordPress: Creating Custom Widgets and Plugins with PHP
 
Expand all | Collapse all
  1. 1m 49s
    1. Welcome
      1m 16s
    2. Using the exercise files
      33s
  2. 23m 29s
    1. WordPress overview
      2m 32s
    2. Installing WPI for Windows
      3m 42s
    3. Installing MAMP for the Mac
      3m 25s
    4. Installing and configuring WordPress
      5m 51s
    5. Comparing WordPress 3.0 with previous versions
      2m 57s
    6. Setting up a PHP/WordPress development environment
      5m 2s
  3. 14m 47s
    1. Exploring WordPress plugins
      3m 42s
    2. Administering plugins from the WordPress admin
      5m 23s
    3. Exploring where plugins reside
      2m 51s
    4. Introduction to hooks
      2m 51s
  4. 39m 28s
    1. Creating the plugin PHP file(s)
      3m 12s
    2. More on hooks: Actions and filters
      3m 15s
    3. Installation and activation
      4m 6s
    4. Writing activation code
      3m 45s
    5. Writing an action
      5m 12s
    6. Writing a filter
      4m 15s
    7. About pluggable functions
      2m 1s
    8. Writing a pluggable function
      5m 30s
    9. Using template tags
      2m 46s
    10. Introducing shortcode
      5m 26s
  5. 26m 2s
    1. Widgets and the WordPress Widgets SubPanel
      2m 54s
    2. Comparing widgets and plugins
      1m 8s
    3. Using and customizing built-in widgets
      3m 18s
    4. Creating a new widget
      7m 21s
    5. Writing the constructor and registering widgets
      5m 20s
    6. Enabling configuration of widgets
      6m 1s
  6. 44m 59s
    1. Creating an admin interface
      5m 25s
    2. Saving data to the database
      5m 39s
    3. Securing form submission with nonces
      2m 25s
    4. Options editing post-WordPress 2.7
      4m 8s
    5. Integrating with the WordPress admin menus
      3m 34s
    6. WordPress admin dashboard API
      4m 5s
    7. Using existing options and option editing pages in WordPress
      5m 19s
    8. Using jQuery and AJAX for administration
      14m 24s
  7. 27m 13s
    1. Accessing the WordPress database
      5m 45s
    2. Using the built-in schema
      2m 21s
    3. Accessing data using $wpdb
      5m 15s
    4. Creating new tables
      7m 18s
    5. Inserting data
      6m 34s
  8. 26m 27s
    1. Introducing the Loop
      6m 22s
    2. Using WP_Query()
      3m 11s
    3. Custom filtering and sticky posts
      4m 58s
    4. Using jQuery and AJAX for posts and pages
      11m 56s
  9. 12m 9s
    1. Registering and promoting plugins
      2m 28s
    2. Creating an uninstall function
      5m 53s
    3. Backward compatibility issues
      3m 48s
  10. 15m 3s
    1. Understanding security issues
      11m 20s
    2. Internationalizing your plugin
      3m 43s
  11. 18s
    1. Goodbye
      18s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.