Implementation of short code in WordPress development and related function usage skills

Source: Internet
Author: User
This article mainly introduces the implementation of short code and related function usage skills in WordPress development. it describes the usage of the add_shortcode function and the shortcode_atts function. if you need a friend, it is very easy to implement short code, we only need to use a function in WordPress to get short code and add a small function of our own, so that the implementation of short code is easy and pleasant.

Implementation principle of short code
Just like adding hooks and filter functions to some WP actions,
The short code is only a encapsulated filter for the output content of the article,
It is not as shocking and advanced as some theme functions are.
The following is a simple example:

Function myName () {// return "My name's XiangZi! ";}/// Mount the short code // xz indicates the short code name. // when you enter [xz] when editing the article, the myName function add_code code ('xz ', 'myname ');

Enter [xz] in the article to obtain

My name's XiangZi !

Passing parameters in short code
I will discuss more advanced exploitation in the following articles,
Today, I only want to talk about the passing mechanism of short code.
Advanced example

function myName($array,$content) {var_dump($array);var_dump($content);} add_shortcode('xz', 'myName');

When editing an article, enter:

[Xz a = "1" B = "2" c = "3"] here are three parameters. [/xz]

In the function, we will get:

// $ Array is an array. // The general structure is as follows $ array = array ('a' => '1', 'B' => '2 ', 'C' => '3'); // $ content is a string $ content = 'Here are three parameters ';

Shortcode_atts
This function is not used because of short code plug-ins,
The shortcode_atts function is mainly used to set the initial values of intercepted variables in short code.
This is a very practical function. In fact, this function actually works on arrays,
Because the parameters we intercept from the short code are in the form of arrays.

Shortcode_atts functions
Do not be confused by the function name. in WordPress, it is mainly used to set the default value of the short code parameter,
If we extract the code and use it elsewhere, this function can help us set a default value for an array.

Use the shortcode_atts function
This function is easy to use.

shortcode_atts(array("url" => 'http://PangBu.Com'), $url)

The code above indicates that,
Set the default value of the $ url array key to 'http: // PangBu. com ',
It does not seem to be useful in other places, but for some super-lazy people, sometimes this function is super easy to use when they forget or are too lazy to set the value of the array.

Shortcode_atts function declaration

/** * Combine user attributes with known attributes and fill in defaults when needed. * * The pairs should be considered to be all of the attributes which are * supported by the caller and given as a list. The returned attributes will * only contain the attributes in the $pairs list. * * If the $atts list has unsupported attributes, then they will be ignored and * removed from the final returned list. * * @since 2.5 * * @param array $pairs Entire list of supported attributes and their defaults. * @param array $atts User defined attributes in shortcode tag. * @return array Combined and filtered attribute list. */function shortcode_atts($pairs, $atts) { $atts = (array)$atts; $out = array(); foreach($pairs as $name => $default) { if ( array_key_exists($name, $atts) )  $out[$name] = $atts[$name]; else  $out[$name] = $default; } return $out;}

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.