Languages: English β’ Italiano β’ API ζ₯ζ¬θͺ PortuguΓͺs do Brasil β’ Π ΡΡΡΠΊΠΈΠΉ β’ (Add your language)
ΠΠ²Π΅Π΄Π΅Π½Π½ΡΠΉ Π² WordPress 2.5, Shortcode API ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΏΡΠΎΡΡΡΠΌ Π½Π°Π±ΠΎΡΠΎΠΌ ΡΡΠ½ΠΊΡΠΈΠΉ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΠΌΠ°ΠΊΡΠΎ-ΠΊΠΎΠ΄ΠΎΠ² ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌΡΡ Π² ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠΌ Π·Π°ΠΏΠΈΡΠ΅ΠΉ. ΠΠ»Ρ ΠΏΡΠΈΠΌΠ΅ΡΠ°, ΡΠ»Π΅Π΄ΡΡΡΠΈΠΉ ΠΊΠΎΡΠΎΡΠΊΠΈΠΉ ΠΊΠΎΠ΄(shortcode) (Π² ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠΌ Π·Π°ΠΏΠΈΡΠΈ/ΡΡΡΠ°Π½ΠΈΡΡ) Π΄ΠΎΠ±Π°Π²ΠΈΠ» Π±Ρ ΡΠΎΡΠΎΠ³Π°Π»Π΅ΡΠ΅Ρ Π½Π° ΡΡΡΠ°Π½ΠΈΡΠ΅:
[gallery]
ΠΡΠΎ ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ°ΠΌ ΠΏΠ»Π°Π³ΠΈΠ½ΠΎΠ² ΡΠΎΠ·Π΄Π°Π²Π°ΡΡ ΡΠΏΠ΅ΡΠΈΠ°Π»ΡΠ½ΡΠ΅ Π²ΠΈΠ΄Ρ ΠΊΠΎΠ½ΡΠ΅Π½ΡΠ° (Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΡΠΎΡΠΌΡ, Π³Π΅Π½Π΅ΡΠ°ΡΠΎΡΡ ΠΊΠΎΠ½ΡΠ΅Π½ΡΠ°) ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΠΈ ΠΌΠΎΠ³ΡΡ ΠΏΡΠΈΠΊΡΠ΅ΠΏΠΈΡΡ ΠΊ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ½Π½ΡΠΌ ΡΡΡΠ°Π½ΠΈΡΠ°ΠΌ Π΄ΠΎΠ±Π°Π²ΠΈΠ² ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠΈΠΉ ΠΊΠΎΡΠΎΡΠΊΠΈΠΉ ΠΊΠΎΠ΄ Π² ΡΠ΅ΠΊΡΡΠ΅ ΡΡΡΠ°Π½ΠΈΡΡ.
Shortcode API ΡΠΏΡΠΎΡΠ°Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΊΠΎΡΠΎΡΠΊΠΈΡ ΠΊΠΎΠ΄ΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°ΡΡ Π°ΡΡΠΈΠ±ΡΡΡ Π²ΡΠΎΠ΄Π΅ ΡΡΠΈΡ :
[gallery id="123" size="medium"]
API ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ Π²Π΅ΡΡ ΡΠ»ΠΎΠΆΠ½ΡΠΉ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡΠ΅ΡΠΊΠΈΠΉ Π°Π½Π°Π»ΠΈΠ·, ΠΈΠ·Π±Π°Π²Π»ΡΡ ΠΎΡ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΡΡΠΈ Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΈΡ ΡΠ΅Π³ΡΠ»ΡΡΠ½ΡΡ Π²ΡΡΠ°ΠΆΠ΅Π½ΠΈΠΉ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π°. ΠΡΠΏΠΎΠΌΠΎΠ³Π°ΡΠ΅Π»ΡΠ½ΡΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ Π²ΠΊΠ»ΡΡΠ΅Π½Ρ Π΄Π»Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ ΠΈ ΠΈΠ·Π²Π»Π΅ΡΠ΅Π½ΠΈΡ Π°ΡΡΠΈΠ±ΡΡΠΎΠ² ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ. API ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ ΠΊΠ°ΠΊ ΡΠ°ΠΌΠΎΡΡΠΎΡΡΠ΅Π»ΡΠ½ΠΎ Π·Π°ΠΊΡΡΠ²Π°ΡΡΠΈΠ΅ΡΡ, ΡΠ°ΠΊ ΠΈ Π½Π΅ Π·Π°ΠΊΡΡΡΡΠ΅ ΠΊΠΎΡΠΎΡΠΊΠΈΠ΅ ΠΊΠΎΠ΄Ρ.
Π ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ Π±ΡΡΡΡΠΎΠ³ΠΎ ΡΡΠ°ΡΡΠ° Π΄Π»Ρ ΡΠ΅Ρ , ΠΊΡΠΎ ΡΠΏΠ΅ΡΠΈΡ, Π²ΠΎΡ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ ΠΏΡΠΈΠΌΠ΅Ρ PHP ΠΊΠΎΠ΄Π°, Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΠ³ΠΎ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π°:
//[foobar]
function foobar_func( $atts ){
return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
ΠΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ ΠΊΠΎΡΠΎΡΠΊΠΈΠΉ ΠΊΠΎΠ΄ "[foobar]" ΠΊΠΎΡΠΎΡΡΠΉ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΠΊΠ°ΠΊ: foo and bar
Π‘ Π°ΡΡΠΈΠ±ΡΡΠ°ΠΌΠΈ:
// [bartag foo="foo-value"]
function bartag_func( $atts ) {
extract( shortcode_atts( array(
'foo' => 'ΡΡΠΎ-ΡΠΎ',
'bar' => 'ΡΡΠΎ-ΡΠΎ Π΅ΡΡ',
), $atts ) );
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
ΠΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ ΠΊΠΎΡΠΎΡΠΊΠΈΠΉ ΠΊΠΎΠ΄ "[bartag]" ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ Π΄Π²Π° Π°ΡΡΠΈΠ±ΡΡΠ°: ["foo" ΠΈ "bar"]. ΠΠ±Π° Π°ΡΡΠΈΠ±ΡΡΠ° ΡΠ²Π»ΡΡΡΡΡ Π½Π΅ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΡΠΌΠΈ ΠΈ ΠΏΡΠΈΠΌΡΡ ΡΡΠ°Π½Π΄Π°ΡΡΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ [foo="ΡΡΠΎ-ΡΠΎ" bar="ΡΡΠΎ-ΡΠΎ Π΅ΡΡ"] Π΅ΡΠ»ΠΈ ΠΎΠ½ΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½Ρ. ΠΠΎΡΠΎΡΠΊΠΈΠΉ ΠΊΠΎΠ΄ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΠΊΠ°ΠΊ foo = {Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π°ΡΡΠΈΠ±ΡΡΠ° foo}
Shortcodes are written by providing a handler function. Shortcode handlers are broadly similar to WordPress filters: they accept parameters (attributes) and return a result (the shortcode output).
Shortcode names should be all lowercase and use all letters, but numbers and underscores should work fine too. Be wary of using hyphens (dashes), you'll be better off not using them.
The `add_shortcode()` function is used to register a shortcode handler. It takes two parameters: the shortcode name (the string used in a post body), and the callback function name.
Three parameters are passed to the shortcode callback function. You can choose to use any number of them including none of them.
The API call to register the shortcode handler would look something like this:
add_shortcode( 'myshortcode', 'my_shortcode_handler' );
When the_content is displayed, the shortcode API will parse any registered shortcodes such as "[myshortcode]", separate and parse the attributes and content, if any, and pass them the corresponding shortcode handler function. Any string returned (not echoed) by the shortcode handler will be inserted into the post body in place of the shortcode itself.
Shortcode attributes are entered like this:
[myshortcode foo="bar" bar="bing"]
These attributes will be converted into an associative array like the following, passed to the handler function as its $atts parameter:
array( 'foo' => 'bar', 'bar' => 'bing' )
The array keys are the attribute names; array values are the corresponding attribute values. In addition, the zeroeth entry ($atts[0]) will hold the string that matched the shortcode regex, but ONLY IF that is different from the callback name. See the discussion of attributes, below.
The raw $atts array may include any arbitrary attributes that are specified by the user. (In addition, the zeroeth entry of the array may contain the string that was recognized by the regex; see the note below.)
In order to help set default values for missing attributes, and eliminate any attributes that are not recognized by your shortcode, the API provides a shortcode_atts() function.
shortcode_atts() resembles the wp_parse_args() function, but has some important differences. Its parameters are:
shortcode_atts( $defaults_array, $atts );
Both parameters are required. $defaults_array is an associative array that specifies the recognized attribute names and their default values. $atts is the raw attributes array as passed into your shortcode handler. shortcode_atts() will return a normalized array containing all of the keys from the $defaults_array, filled in with values from the $atts array if present. For example:
$a = shortcode_atts( array( 'title' => 'My Title', 'foo' => 123, ), $atts );
If $atts were to contain array( 'foo' => 456, 'bar' => 'something' ), the resulting $a would be array( 'title' => 'My Title', 'foo' => 456 ). The value of $atts['foo'] overrides the default of 123. $atts['title'] is not set, so the default 'My Title' is used. And there is no 'bar' item in the defaults array, so it is not included in the result.
Attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched. [myshortcode FOO="BAR"] produces $atts = array( 'foo' => 'BAR' ).
A suggested code idiom for declaring defaults and parsing attributes in a shortcode handler is as follows:
function my_shortcode_handler( $atts, $content = null ) {
extract( shortcode_atts( array(
'attr_1' => 'attribute 1 default',
'attr_2' => 'attribute 2 default',
// ...etc
), $atts ) );
}
This will parse the attributes, set default values, eliminate any unsupported attributes, and (using extract()) store the results in local variables named for the attribute keys - $attr_1, $attr_2 and so on. In other words, the array of defaults approximates a list of local variable declarations. (extract() is safe to use in this context without special flags to handle collisions because shortcode_atts() will strip out any keys not included in the defaults array).
add_shortcode('foo','foo'); // two shortcodes referencing the same callback
add_shortcode('bar','foo');
produces this behavior:
[foo a='b'] ==> callback to: foo(array('a'=>'b'),NULL,"foo");
[bar a='c'] ==> callback to: foo(array(0 => 'bar', 'a'=>'c'),NULL,"");
This is confusing and perhaps reflects an underlying bug, but an overloaded callback routine can correctly determine what shortcode was used to call it, by checking BOTH the third argument to the callback and the zeroeth attribute. (It is NOT an error to have two shortcodes reference the same callback routine, which allows for common code.)
ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ° ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π° Π²ΡΡΠ°Π²Π»ΡΠ΅ΡΡΡ Π² Π²ΡΠ²ΠΎΠ΄ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ³ΠΎ Π·Π°ΠΏΠΈΡΠΈ Π½Π° ΠΌΠ΅ΡΡΠ΅ ΠΌΠ°ΠΊΡΠΎΡΠ° ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π°. ΠΡΠΏΠΎΠ»ΡΠ·ΡΠΉΡΠ΅ 'return', Π° Π½Π΅ 'echo' - ΠΈΠ½Π°ΡΠ΅ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠ²ΠΎΠ΄ΠΈΡΡΡ Π² Π±ΡΠ°ΡΠ·Π΅Ρ, Π½ΠΎ Π½Π΅ Π±ΡΠ΄Π΅Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°ΡΡΡΡ Π² Π½ΡΠΆΠ½ΠΎΠΌ ΠΌΠ΅ΡΡΠ΅ Π½Π° ΡΡΡΠ°Π½ΠΈΡΠ΅.
ΠΠΎΡΠΎΡΠΊΠΈΠ΅ ΠΊΠΎΠ΄Ρ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ ΡΠΎΠ³ΠΎ ΠΊΠ°ΠΊ wpautop ΠΈ wptexturize ΡΠΎΡΠΌΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ Π±ΡΠ»ΠΎ ΠΏΡΠΈΠΌΠ΅Π½Π΅Π½ΠΎ (Π½ΠΎ ΡΠΌΠΎΡΡΠΈΡΠ΅ ΠΏΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅ Π½ΠΈΠΆΠ΅ ΠΎ ΡΠ°Π·Π»ΠΈΡΠΈΡΡ 2.5.0 ΠΈ 2.5.1). ΠΡΠΎ ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΊ HTML Π²ΡΠ²ΠΎΠ΄Ρ Π²Π°ΡΠ΅Π³ΠΎ ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π° Π½Π΅ Π±ΡΠ΄ΡΡ Π°Π²ΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΏΡΠΈΠΌΠ΅Π½ΡΡΡΡΡ ΠΊΠ°Π²ΡΡΠΊΠΈ, Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ ΡΠ΅Π³ΠΈ p ΠΈ br, ΠΈ ΡΠΎΠΌΡ ΠΏΠΎΠ΄ΠΎΠ±Π½ΠΎΠ΅. ΠΡΠ»ΠΈ Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ ΡΡΠΎΠ±Ρ Π²ΡΠ²ΠΎΠ΄ Π²Π°ΡΠ΅Π³ΠΎ ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π° ΡΠΎΡΠΌΠ°ΡΠΈΡΠΎΠ²Π°Π»ΡΡ, Π²Ρ Π΄ΠΎΠ»ΠΆΠ½Ρ Π²ΡΠ·Π²Π°ΡΡ wpautop() ΠΈΠ»ΠΈ wptexturize() Π½Π°ΠΏΡΡΠΌΡΡ ΠΊΠΎΠ³Π΄Π° Π²Ρ Π΄Π΅Π»Π°Π΅ΡΠ΅ Π²ΡΠ²ΠΎΠ΄ ΠΈΠ· ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ° Π²Π°ΡΠ΅Π³ΠΎ ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π°.
wpautop ΡΠ°ΡΠΏΠΎΠ·Π½Π°ΡΡ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡ ΠΊΠΎΡΠΎΡΠΊΠΈΡ ΠΊΠΎΠ΄ΠΎΠ² ΠΈ Π½Π΅ Π±ΡΠ΄Π΅Ρ ΠΏΡΡΠ°ΡΡΡΡ Π·Π°ΠΊΠ»ΡΡΠΈΡΡ Π² p ΠΈΠ»ΠΈ br ΡΠ΅Π³ΠΈ ΠΎΠ΄ΠΈΠ½ΠΎΡΠ½ΡΠ΅ ΠΊΠΎΡΠΎΡΠΊΠΈΠ΅ ΠΊΠΎΠ΄Ρ ΡΡΠΎΡΡΠΈΠ΅ Π½Π° ΡΡΡΠΎΠΊΠ΅ ΡΠ°ΠΌΠΈ ΠΏΠΎ ΡΠ΅Π±Π΅. ΠΠΎΡΠΎΡΠΊΠΈΠ΅ ΠΊΠΎΠ΄Ρ, ΠΏΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½Π½ΡΠ΅ Π΄Π»Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ ΡΠ°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, Π΄ΠΎΠ»ΠΆΠ½Ρ Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°ΡΡ, ΡΡΠΎ Π²ΡΠ²ΠΎΠ΄ Π·Π°ΠΊΠ»ΡΡΡΠ½ Π² ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠΈΠΉ Π±Π»ΠΎΠΊ ΡΠ΅Π³ΠΎΠ², ΡΠ°ΠΊΠΈΡ ΠΊΠ°ΠΊ <p> ΠΈΠ»ΠΈ <div>.
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅: Π² WordPress 2.5.0 ΠΊΠΎΡΠΎΡΠΊΠΈΠ΅ ΠΊΠΎΠ΄Ρ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π»ΠΈΡΡ ΠΏΠΎΡΠ»Π΅ ΠΏΡΠΈΠΌΠ΅Π½Π΅Π½ΠΈΡ ΠΊ Π½ΠΈΠΌ ΡΠΎΡΠΌΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ, ΡΠ°ΠΊ ΡΡΠΎ ΠΈΠ½ΠΎΠ³Π΄Π° HTML Π²ΡΠ²ΠΎΠ΄ ΠΊΠΎΡΠΎΡΠΊΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π° Π·Π°ΠΊΠ»ΡΡΠ°Π»ΡΡ Π² ΡΠ΅Π³ΠΈ p ΠΈΠ»ΠΈ br. ΠΡΠΎ Π±ΡΠ»ΠΎ Π½Π΅ΠΊΠΎΡΡΠ΅ΠΊΡΠ½ΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ»ΠΎ ΠΈΡΠΏΡΠ°Π²Π»Π΅Π½ΠΎ Π² Π²Π΅ΡΡΠΈΠΈ 2.5.1.
ΠΡΠ»ΠΈ ΠΊΠΎΡΠΎΡΠΊΠΈΠΉ ΠΊΠΎΠ΄ ΠΎΠ±ΡΠ°Π·ΡΠ΅Ρ ΠΌΠ½ΠΎΠ³ΠΎ HTML ΠΊΠΎΠ΄Π°, ΡΠΎ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ob_start() Π΄Π»Ρ Π·Π°ΠΏΠΈΡΠΈ Π²ΡΡ ΠΎΠ΄Π½ΡΡ Π΄Π°Π½Π½ΡΡ ΠΈ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΡ ΠΈΡ Π² ΡΡΡΠΎΠΊΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ:-
function myShortCode() {
ob_start();
?> <HTML> <here> ... <?PHP
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}
The examples above show self-closing shortcode macros such as [myshortcode]. The API also supports enclosing shortcodes such as [myshortcode]content[/myshortcode].
If a shortcode macro is used to enclose content, its handler function will receive a second parameter containing that content. Users might write shortcodes in either form, so it is necessary to allow for either case by providing a default value for the second parameter to your handler function:
function my_shortcode_handler( $atts, $content = null )
is_null( $content ) can be used to distinguish between the self-closing and enclosing cases.
When content is enclosed, the complete shortcode macro including its content will be replaced with the function output. It is the responsibility of the handler function to provide any necessary escaping or encoding of the raw content string, and include it in the output.
Here's a trivial example of an enclosing shortcode:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
When used like this:
[caption]My Caption[/caption]
The output would be:
<span class="caption">My Caption</span>
Since $content is included in the return value without any escaping or encoding, the user can include raw HTML:
[caption]<a href="http://example.com/">My Caption</a>[/caption]
Which would produce:
<span class="caption"><a href="http://example.com/">My Caption</a></span>
This may or may not be intended behaviour - if the shortcode should not permit raw HTML in its output, it should use an escaping or filtering function to deal with it before returning the result.
The shortcode parser uses a single pass on the post content. This means that if the $content parameter of a shortcode handler contains another shortcode, it won't be parsed:
[caption]Caption: [myshortcode][/caption]
This would produce:
<span class="caption">Caption: [myshortcode]</span>
If the enclosing shortcode is intended to permit other shortcodes in its output, the handler function can call do_shortcode() recursively:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . do_shortcode($content) . '</span>';
}
In the previous example, this would ensure the "[myshortcode]" macro in the enclosed content is parsed, and its output enclosed by the caption span:
<span class="caption">Caption: The result of myshortcode's handler function</span>
The parser does not handle mixing of enclosing and non-enclosing forms of the same shortcode as you would want it to. For example, if you have:
[myshortcode example='non-enclosing' /] non-enclosed content [myshortcode] enclosed content [/myshortcode]
Instead of being treated as two shortcodes separated by the text " non-enclosed content ", the parser treats this as a single shortcode enclosing " non-enclosed content [myshortcode] enclosed content".
Enclosing shortcodes support attributes in the same way as self-closing shortcodes. Here's an example of the caption_shortcode() improved to support a 'class' attribute:
function caption_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'class' => 'caption',
), $atts ) );
return '<span class="' . esc_attr($class) . '">' . $content . '</span>';
}
[caption class="headline"]My Caption[/caption]
<span class="headline">My Caption</span>
The following Shortcode API functions are available:
function add_shortcode($tag, $func)
Registers a new shortcode handler function. $tag is the shortcode string as written by the user (without braces), such as "myshortcode". $func is the handler function name.
Only one handler function may exist for a given shortcode. Calling add_shortcode() again with the same $tag name will overwrite the previous handler.
function remove_shortcode($tag)
Deregisters an existing shortcode. $tag is the shortcode name as used in add_shortcode().
function remove_all_shortcodes()
Deregisters all shortcodes.
function shortcode_atts($pairs, $atts)
Process a raw array of attributes $atts against the set of defaults specified in $pairs. Returns an array. The result will contain every key from $pairs, merged with values from $atts. Any keys in $atts that do not exist in $pairs are ignored.
function do_shortcode($content)
Parse any known shortcode macros in the $content string. Returns a string containing the original content with shortcode macros replaced by their handler functions' output.
do_shortcode() is registered as a default filter on 'the_content' with a priority of 11.
The shortcode parser correctly deals with nested shortcode macros, provided their handler functions support it by recursively calling do_shortcode():
[tag-a]
[tab-b]
[tag-c]
[/tag-b]
[/tag-a]
However the parser will fail if a shortcode macro is used to enclose another macro of the same name:
[tag-a] [tag-a] [/tag-a] [/tag-a]
This is a limitation of the context-free regexp parser used by do_shortcode() - it is very fast but does not count levels of nesting, so it can't match each opening tag with its correct closing tag in these cases.
In certain cases the shortcode parser cannot correctly deal with the use of both closed and unclosed shortcodes. For instance in this case the parser will only correctly identify the second instance of the shortcode:
[tag] [tag] CONTENT [/tag]
However in this case the parser will identify both:
[tag] CONTENT [/tag] [tag]
Note: The behavior described below involving shortcodes with hyphens ('-') still applies in WordPress 3+. This could be due to a bug in do_shortcode() or get_shortcode_regex().
Take caution when using hyphens in the name of your shortcodes. In the following instance WordPress may see the second opening shortcode as equivalent to the first (basically WordPress sees the first part before the hyphen):
[tag] [tag-a]
It all depends on which shortcode is defined first. If you are going to use hyphens then define the shortest shortcode first.
To avoid this, use an underscore or simply no separator:
[tag] [tag_a] [tag] [taga]
If the first part of the shortcode is different from one another, you can get away with using hyphens:
[tag] [tagfoo-a]
Important: Using hyphens can have implications that you may not be aware of; such as if other installed shortcodes also are use hyphens, the use of generic words with hyphens may cause collisions (if shortcodes are used together within the same request):
// plugin-A [is-logged-in] // plugin-B [is-admin]
The shortcode parser does not accept square brackets within attributes. Thus the following will fail:
[tag attribute="[Some value]"]
Note: these limitations may change in future versions of WordPress, you should test to be absolutely sure.