Shortcode Short code function in WordPress, wordpressshortcode_php tutorial

Source: Internet
Author: User
Tags bbcode closing tag

WordPress in the Shortcode Short code function of the use of detailed, Wordpressshortcode


WordPress starting from the 2.5 version, the addition of a shortcode (short code) API, similar to the BBCode on BBS, Shortcode can also be very convenient for the article or page to add functionality, and shortcode than BBCode more Plus flexibility and power. The following kayo for you to introduce Shortcode.

I. Shortcode INTRODUCTION
Shortcode can allow developers to create content by creating macro content as a function, which may seem a bit blurry, but in fact it is a very simple and useful function, as long as you write basic PHP functions, you can use Shortcode, and the following is a practical example to illustrate How to use Shortcode.

Two. Shortcode form
Shortcode supports closed tags and self-closing (auto-enclosing) tags, and supports the use of parameters within the tag, as to what the Shortcode is, which determines how the developer writes the Shortcode.

[Myshortcode] Some Content[/myshortcode]//closed tag [myshortcode]//self-closing tag [Myshortcode title= "Example"]//self-closing label with one parameter [Myshortcode]

Content

[/myshortcode]//tags can be filled in text or html[myshortcode]content [Myshortcodesecond] more Content[/myshortcodesecond]// You can also nest using tags

Three. Shortcode Example
Before using Shortcode, you must first define Shortcode in the subject's functions.php file, for example:

function Myshortcode_function ($atts, $content = null) {//$atts represents the individual parameters of the Shortcode, $content the contents of the tag  extract ( Shortcode_atts (Array (///Use the Extract function to resolve the parameters within the label "title" =//"title"//give the parameter a default value, the following directly call the value of the parameter name output parameters), $atts)); Returns the content return '    

'. $title. '

'. $content. '

';} Add_shortcode ("MSC", "Myshortcode_function"); Register the Shortcode and call it later using the [MSC] tag Shortcode

Adding the above code to the 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 the independent blog Kayo's Melody, welcome to this blog [/msc]

Enter the above call in the article or page content, you can output a welcome statement at the corresponding location, define the corresponding CSS in Style.css, and give the style to the short code.

Kayo briefly introduces the short code (shortcode) function of WordPress, mainly introduces the main concepts and methods of Shortcode. In this article, Kayo will give a more detailed introduction to the more important API 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, referring to the example above:

function Myshortcode_function ($atts, $content = null) {//$atts represents the individual parameters of the Shortcode, $content the contents of the tag  extract ( Shortcode_atts (Array (///Use the Extract function to resolve the parameters within the label "title" =//"title"//give the parameter a default value, the following directly call the value of the parameter name output parameters), $atts)); Returns the content return '    

'. $title. '

'. $content. '

';} Add_shortcode ("MSC", "Myshortcode_function"); Register the Shortcode and call it later using the [MSC] tag Shortcode

MSC is a short code name that can be used to invoke the short code directly using the [MSC][/MSC] tag when writing an article or page, while "Myshortcode_function" is the name of the short code handler function in the example. The following focuses on analyzing short code processing functions.

Five. Short Code processing function

The Shortcode handler is the core of a shortcode, and the Shortcode handler function is similar to Flickr (WordPress filter), which accepts certain parameters and returns certain results. The Shortcode processor accepts two parameters, $attr and $content, $attr each attribute parameter representing the Shortcode, which is essentially an associative array, and $content represents the contents of the Shortcode tag.

As the above example, if you make a call within the article, output a welcome statement:

[MSC Title= "Welcome"] This is the 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 attribute parameters and content, WordPress will separate them out and parse, and then pass to the Shortcode Short code processing function, after processing The result of processing the output of the function instead of the original content of the short code is displayed within the article.

The attribute argument is then parsed to correlate the array and passed to the $attr, as in the example above $attr the value is an associative array of the following:

Array (' title ' = ' Welcome ')

When you output the result, you can output it directly using the parameter name, as in the case of the $title output the property value.

Six. Shortcode_atts

Shortcode_atts is a useful function that sets default values for the property parameters you need, and removes some unwanted parameters.

Shortcode_atts () contains two parameters $defaults _array and $atts, $attr is a collection of attribute parameters, $defaults _array is the default value for the property that needs to be set, for example:

$result = Shortcode_atts (Array (' title ' = ' new title ', ' description ' = ' description content '), $atts); $attr remains an array (' title ' = ' = ') Welcome ')

At this point $result result is

Array (' title ' = ' new title ', ' description ' + ' description title ')

Because the ' title ' has different values in the $defaults _array, the ' title ' is updated with this new value, and the value of ' description ' is added. It is important to note that Shortcode_atts () filters properties that are not in the $defaults _array, and if there is a property of ' Ohter ' in the $attr, the result of $result is still the result above, because $defaults _array There is no such attribute as ' other '. Of course, the value mentioned here is only the default value of the property, and the true output value is the value that is filled in when the shortcode is called.

Seven. Further parsing properties and setting property defaults

The extract () function is used to further parse the property and set the default value of the property, one of which is to save the values of each attribute parameter to a variable with a shape such as the "$ parameter name" (as in the example $title), which is convenient to call, use the function with Shortcode_atts () You can safely output the results. The specific use of this point can be found in the 1th "I. Function Add_shortcode" example in this article.

In addition, uppercase letters in attribute names are converted to lowercase letters before they are passed to the handler, so it is recommended that you use lowercase letters directly when writing property names.

http://www.bkjia.com/PHPjc/1127865.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127865.html techarticle WordPress in the Shortcode Short code function use in detail, Wordpressshortcode WordPress from the 2.5 version, the addition of a shortcode (short code) API, similar to the BBCode on the BBS. ..

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.