The implementation of short code in the development of WordPress and the use of related functions, wordpress use tips _php Tutorial

Source: Internet
Author: User

The implementation of short code in WordPress development and the use of related functions, WordPress use Tips


In fact, the implementation of short code is very simple, we only need to use the WordPress inside a function can be done short code, plus a small function of their own, you can make the short code implementation of the easy and enjoyable.

Short Code Implementation principle
Just like a few moves to WP add hooks and filter functions,
The short code is just a encapsulated filter for the output of the article,
It's not as shocking and profound as some of the theme features say.
Here's a simple example:

function MyName () {//short code to process functions return "My name ' s Xiangzi!";} Mount the short code//XZ to the short code name//That is, when you edit the article input [XZ] will execute the MyName function add_shortcode (' XZ ', ' myName ');

Then we'll enter [XZ] in the article and we'll get

My name ' s Xiangzi!

Short Code Transfer parameter
A bit more advanced, I'll talk about it in a later article,
Just to speak today, the short code of the transfer mechanism
An example of an advanced point

function MyName ($array, $content) {var_dump ($array); Var_dump ($content);} add_shortcode (' XZ ', ' myName ');

When editing an article we enter:

[XZ a= "1" b= "2" c= "3"] here are three parameters oh [/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 oh ';

Shortcode_atts
Not because of the short code plugin, I don't use this function,
The Shortcode_atts function is primarily used to set the initial value of the intercepted variable in the short code.
This is a very useful function, in fact, this function is really useful in the array,
Because the parameters we intercept from the short code are in the form of arrays.

Shortcode_atts function Explanation
Do not be confused by the name of the function, in WordPress is mainly used to set the default value of short code parameters,
If we extract the code and use it elsewhere, the function can help us set a default value for both groups.

The Shortcode_atts function uses
This function is simple to use.

Shortcode_atts (Array ("url" = ' Http://PangBu.Com '), $url)

The above code means that
Set the member default value of the $url array key value to URL to ' Http://PangBu.Com ',
Other places do not seem to use much, but for some super lazy people, sometimes to forget or lazy to set the value of the array, this function is very useful.

Shortcode_atts function Declaration

/** * Combine user attributes with known attributes and fill in defaults when needed. * The pairs should is considered to being all of the attributes which is * supported by the caller and given as a list. The returned attributes would * only contain the attributes in the $pairs list. * If The $atts list has unsupported attributes, then they'll 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;}

Articles you may be interested in:

    • Use of PHP functions to get recent articles in WordPress development
    • Introduction to the use of PHP functions for custom menus in WordPress development
    • The PHP function used in WordPress to get the search form is parsed
    • Use the Wp_count_posts function in WordPress to count the number of articles
    • PHP functions for invoking comment templates and looping out comments in WordPress
    • Simple steps to add Google search to WordPress
    • Get_post and get_posts function usage in WordPress development
    • Analysis of Post_class and Get_post_class functions in WordPress
    • Get_post_custom () function usage analysis in WordPress development
    • Installing Hana FLV player in WordPress with video player plugin
    • A detailed description of the use of classification function wp_list_categories in WordPress

http://www.bkjia.com/PHPjc/1088769.html www.bkjia.com true http://www.bkjia.com/PHPjc/1088769.html techarticle the implementation of short code in the development of WordPress and the use of related functions, WordPress Use of skills in fact, the implementation of short code is very simple, we only need to use the WordPress inside a function can ...

  • 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.