Use of the short code function in WordPress

Source: Internet
Author: User
Tags bbcode closing tag code tag
The short code in WordPress can generate content through the combination of simple function sets and macro code to facilitate function calling. let's take a look at the usage of the short code function in WordPress. wordPress has added a short code API starting with version 2.5. similar to BBCode on BBS, the short code can also easily add features for articles or pages, in addition, the response code is more flexible and powerful than the BBCode. The following section describes the deployment code.

I. INTRODUCTION to postal code
Coding code allows developers to create macro content in the form of functions to generate content. this concept may seem a bit vague, but it is actually a very simple and practical function, as long as you write basic PHP functions, you can use the response code. The following describes how to use the response code using the actual example.

II. response code
The response code supports both closed and self-closed tags, and supports using parameters in the tag. the specific form of response code depends on how developers compile the response code.

[Mydomaincode] Some Content [/mydomaincode] // The closed tag [mydomaincode] // The self-closing tag [mydomaincode title = "example"] // The self-closing tag with a parameter [mydomaincode]

Content

[/Mydomaincode] // you can enter text or HTML [mydomaincode] Content [mydomaincodesecond] more content [/mydomaincodesecond] in the tag. // You can also use tags for nesting.

III. example of postal code
Before using the response code, you must first define the response code in the functions. php file of the topic. for example:

Function my‑code_function ($ atts, $ content = null) {// $ atts represents the parameters of your code, $ content is the content of the tag extract (inclucode_atts (array (// use the extract function to parse the parameter "title" => 'title' in the tag. // assign the default value to the parameter, directly call $ add parameter name output parameter value), $ atts); // return content return'

'. $ Title .'

'. $ Content .'

';} Add_response code ("msc", "my‑code_function"); // register the response code and use the [msc] label to call the response code.

Add the above code to functions. php, and a simple deployment code is created. we can call the deployment code through the [msc] [/msc] tag, for example:

[Msc title = "welcome"] this is an 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 position and define the corresponding CSS in style.css to assign a style to the short code.

Kayo briefly introduces the short code function of WordPress, mainly introduces the main concepts and usage of the short code. In this article, Kayo will introduce the more important APIs in the coding code in more detail, hoping to help you develop more complex coding.

IV. function add_response code

This function is used to register a response code. It has two parameters: short code name and response code processing function name. for example:

Function my‑code_function ($ atts, $ content = null) {// $ atts represents the parameters of your code, $ content is the content of the tag extract (inclucode_atts (array (// use the extract function to parse the parameter "title" => 'title' in the tag. // assign the default value to the parameter, directly call $ add parameter name output parameter value), $ atts); // return content return'

'. $ Title .'

'. $ Content .'

';} Add_response code ("msc", "my‑code_function"); // register the response code and use the [msc] label to call the response code.

Msc is the short code name. you can use the [msc] [/msc] label to call the short code when writing an article or on the page, "myshortcode_function" is the name of the short code processing function in the example. The following describes short code processing functions.

5. short code processing functions

The primary code processing function is the core of the primary code. The primary code processing function is similar to the WordPress filter. all of them accept specific parameters and return certain results. The encoding code processor accepts two parameters: $ attr and $ content. $ attr represents each attribute parameter of the encoding code, which is essentially an associated array, while $ content represents the content in the encoding code tag.

In the preceding example, if a call is made in the article, a welcome statement is output:

[Msc title = "welcome"] this is an independent blog Kayo's Melody. welcome to this blog [/msc]
When the article is displayed, WordPress registers all the response code, as shown in [msc] Above. if the response code contains the attribute parameters and content, WordPress will separate them and parse them, the short code handler passed to the response code. after processing, the original content of the short code is displayed in the article with the result of the processing function output.

At this time, the attribute parameters are parsed and associated with the array and passed to $ attr. in the preceding example, the value of $ attr is an associated array as follows:

Array ('title' => 'Welcome ')

You can directly use the $ parameter name to output the result. In this example, the $ title attribute value is output.

6. shortcode_atts

Shortcode_atts is a very practical function that can set default values for the property parameters you need and delete unnecessary parameters.

Shortcode_atts () contains two parameters: $ defaults_array and $ atts. $ attr is the set of attribute parameters. $ defaults_array represents the default attribute values to be set. for example:

$ Result = inclucode_atts (array ('title' => 'newtitle', 'description' => 'Description'), $ atts ); $ attr is still array ('title' => 'Welcome ')

The result of $ result is

Array ('title' => 'New title', 'description' => 'Description ')

'Title' because there are different values in $ defaults_array, the 'title' is updated based on the new value, and the 'description' value is also added. It is worth noting that the shortcode_atts () filter will filter the attributes not in $ defaults_array. if $ attr has an 'ohter 'attribute, the result of $ result is still the above result, because $ defaults_array does not have the 'other' attribute. Of course, the value here is only the default value of the attribute. the actual output value is also the value filled in when the response code is called.

VII. further parsing attributes and setting default values of attributes

The extract () function is used to parse attributes and set the default values of attributes, one function is to save the attribute parameter values to a variable in the form of "$ parameter name" (for example, $ title in this example) for convenient calling, you can use this function with shortcode_atts () to output results safely. For specific use of this point, see the example of "1. function add_code code" in the first part of this article.

In addition, the upper-case letters in the attribute name are converted to lower-case letters before being passed to the handler. Therefore, we recommend that you use lower-case letters directly when writing the attribute name.

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.