WordPress starting from the 2.5 version, added a shortcode (short code) API, similar to the BBS on the BBCode, Shortcode can also be very convenient for the article or page to add functionality, and shortcode than BBCode more Plus flexibility and strength. Below Kayo for you to introduce Shortcode.
I. Shortcode INTRODUCTION
Shortcode can let developers create content by creating content in the form of functions, which may seem a bit blurry, but in fact it is a very simple and practical function, as long as you write basic PHP functions, you can use Shortcode, The following is a practical example of how shortcode is used.
Two. Shortcode form
Shortcode supports closed and closed (auto close) tags, and supports the use of parameters within the label, as to Shortcode specific form, which determines how developers write this shortcode.
[Myshortcode] Some Content[/myshortcode]//Closed label
[Myshortcode]//self-closing label
[Myshortcode title= "Example"]///with a parameter of a closed label
[ Myshortcode]<p><a href= "#" ><span> content </span></a></p>[/myshortcode]// Tags can be filled in text or HTML
[myshortcode]content [Myshortcodesecond] more Content[/myshortcodesecond]//can be nested using labels
Three. Shortcode Example
before using Shortcode, you must first define Shortcode in the functions.php file of the topic, for example:
function Myshortcode_function ($atts, $content = null) {//$atts represents each parameter of the Shortcode, $content as the content within the label
extract (
Back to content return
' <div class= ' Myshortcode ' >
Adding the above code to functions.php, a simple shortcode is created and we can invoke the Shortcode through the [msc][/msc] tag, such as:
[MSC Title= "Welcome"] This is an independent blog Kayo ' s melody, welcome to this blog [/msc]
Input the above call in the article or the content of the page, can output a section of welcome statement in the corresponding position, define the corresponding CSS in the STYLE.CSS, can give the style for the short code.
Kayo briefly introduces the short code (shortcode) function of WordPress, mainly introduces the main concepts and use methods of Shortcode. In this article, Kayo will give a more detailed introduction to the more important APIs in Shortcode, hoping to help you develop more complex shortcode.
Four. Function Add_shortcode
This function is used to register a shortcode, which has two parameters: the short code name and the Shortcode handler function name, citing the example above:
function Myshortcode_function ($atts, $content = null) {//$atts represents each parameter of the Shortcode, $content as the content within the label
extract (
Back to content return
' <div class= ' Myshortcode ' >
MSC is a short code name, you can use the [Msc][/msc] tag to invoke the short code later when writing an article or page, and "Myshortcode_function" is the name of the Short code processing function in the example. The following focuses on the short code processing functions.
Five. Short Code processing function
The Shortcode handler function is the core of a shortcode, and the Shortcode handler function is similar to Flickr (WordPress filter), which accepts certain parameters and returns a certain result. The Shortcode processor accepts two parameters, $attr and $content, $attr represent the individual property parameters of the Shortcode, essentially an associative array, and the $content represents the contents of the Shortcode tag.
As in the example above, if you make a call in the article, you output a welcome statement:
[MSC Title= "Welcome"] This is an independent blog Kayo ' s melody, welcome to this blog [/msc]
When the article is displayed, WordPress will register all the shortcode, such as the above [MSC], if the Shortcode has attribute parameters and content, WordPress will separate them out and parse, and then pass to the shortcode of the Short code processing functions, after processing The output of the processing function instead of the short code original content is displayed in the article.
The attribute argument then resolves the associative array and passes it to the $attr, as in the example above $attr the value is an associative array as follows:
Array (' title ' => ' Welcome ')
When you output the result, you can output it directly using the form of the $ parameter name, as in the case where the property value is output $title.
Six. Shortcode_atts
Shortcode_atts is a useful function that sets the default values for the property parameters you need and deletes some unwanted parameters.
Shortcode_atts () contains two parameters $defaults _array and $atts, $attr is a collection of property parameters, $defaults _array is the default value for the property that needs to be set, for example:
$result = Shortcode_atts (
' title ' => ' new title ',
' description ' => ' descriptive content '
), $atts);
$attr still for
Array (' title ' => ' Welcome ')
The result of this $result is
Array (' title ' => ' new title ', ' description ' => ' description title ')
' title ' because there are different values in the $defaults _array, so the ' title ' is updated with this new value, and the value of ' description ' is also added. It is worth noting that Shortcode_atts () will filter the attributes that are not in the $defaults _array, if there is a ' ohter ' attribute in the $attr, then the result of the $result is still the result, because $defaults _array There is no such attribute as ' other '. Of course, the value here is just the default value of the property, and the true output value is the value that was filled in when the shortcode called.
Seven. Further resolve the properties and set the default value of the property
The extract () function is used to further parse the property and set the default value of the property, one of which is to assign the values of each property parameter to a variable such as "$ Parameter name" ($title in the example) for easy invocation and use of the function in conjunction with Shortcode_atts () It can be very safe to output the results. The specific use of this point can be found in the 1th "a. Function Add_shortcode" example.
In addition, uppercase letters in property names are converted to lowercase letters before being passed to the handler, so it is recommended that you use lowercase letters directly when you write the property names.