Use of the short code function in WordPress _ php instance

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.