Friday , 17 November 2017
Home / Programming / WordPress / Plugins / Shortcode Attributes – WordPress Plugin Tutorial
Shortcode Attributes
Shortcode Attributes

Shortcode Attributes – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

Let’s take it up a step and make our shortcode more flexible. What if we want the user to define their own link? Well, it’s possible if we create shortcode attributes. Let’s learn how to do so. We’ll be using the same plugin we made in the previous tutorial. We’ll just be adding onto it.

Update the post you created earlier for our previous shortcode example and update the shortcode to this.

As you can see, inside our shortcode, we create an attribute and set it’s value to a link. You can define any name for your attribute. This will passed into our shortcode function. It’s just like HTML. Let’s update our shortcode function. Here’s what our new shortcode function looks like.

Shortcode Attributes

The $atts variable will contain an array of all attributes defined by our shortcode. In this case, we created an attribute named link and used that attribute to define the link we want. We can then grab that attribute from our array $atts. The name of the attribute is the name of the key. We can also take it a step further and define multiple attributes. Shortcodes can take an unlimited amount of attributes.

Update your shortcode in your post to this.

To define multiple attributes, you simply separate them with spaces. WordPress will take of identifying each attribute.

Check your post and you should see the changes made.  Do you see the power of shortcodes? You can provide your users with ultimate flexibility by using shortcode attributes. We can provide users the ability to change your plugin to work the way they want it to. Of course, you have to assume sometimes your users won’t define every single attribute, so doing some checks to see if the attributes are empty or not is probably a good idea.

Shortcode Attributes
Shortcode Attributes

Feel free to mess with this function. You could create a shortcode that takes in an ID of a youtube video and then returns the video to them. More information about shortcode attributes can be found here.

About Jasko Koyn

Check Also

WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

WordPress meta boxes are used to display certain information or options in a page. It’s …

2 comments

  1. I had to remove the comma between the attributes in the shortcode, no comma needed.

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link