Using attributes with embeds makes them much more flexible, but it’s a bit of a pain when you have to remember to specify a value for all of them when you use the embed. Fortunately, you can avoid this problem by setting a default value for an attribute. Then, if you don’t set the attribute’s value when you use it, the embed will still work, since it will use the default value you set for the attribute.
Here’s a simple example of an embed without any attributes:
Name: big Value: <span style="font-size:20pt;">%content% - 20pt</span>
The font size is hard coded in two places, which is okay, but not very flexible:
Hello World - ’20pt’
Easy, but not very flexible, so we turn the font-size into an attribute:
Name: big Value: <span style="font-size:%size%;">%content% - %size%</span>
Used in a post:
[big size='20pt']Hello World[/big]
As we would expect, this gives us the same result as before:
Hello World - ’20pt’
But what if we leave the size attribute out of the shortcode?
It doesn’t work:
Hello World -
%size% is not recognized as an attribute because it doesn’t have a value. To avoid this problem, we can give %size% a default value in the embed’s definition.
To specify a default value, we simply insert the string =<value> between the end of the attribute name and the closing % sign:
Name: big Value: <span style="font-size:%size=20pt%;">%content% - %size%</span>
Notice how you only need to set the default value for the first occurrence of %size% in the embed. In fact, if you try to set a different default value for the second one, it will still take the first default it finds in the embed. (We could have added the =20pt to the second occurrence of %size% instead of the first, and it would have worked too, though it makes more sense to always set the default in the first occurrence of an attribute, for clarity).
Now, when we use the embed without any attributes, it picked up the default value we set:
And it works just fine:
Hello World - 20pt
The nice thing about this is that if you set the right defaults, you can use the embeds without having to set any attributes most of the time, but still use them when you want to override the default values:
[big size="30pt"]Hello World[/big]
Hello World - ”30pt”
One last point. If you need to use a % sign in the default value, you must precede it with a backslash () so that it is not confused with the end of the attribute:
Name: big Value: <span style="font-size:%size=200%%;">%content% - %size%</span>
Now, when you use the default:
The first % sign after the =200 is correctly considered to be part of the default value:
Hello World - ”200%”
(Note: since the backslash is an “escape” character, if you want it to appear in a default value, you need to use two of them together (\) to get one to show up.)
Default values can be used with both local and default embeds, but you can’t use a default with the special %content% attribute.
They can also be used in auto-embeds. In fact this is the only way you can use attributes in auto-embeds since you have no way to set them otherwise. This is useful if you want to use an ordinary embed with attributes as an auto-embed. All you have to do is make sure all the attributes have default values.