Describes the basic method for compiling simple code format labels in WordPress. _ PHP Tutorial

Source: Internet
Author: User
Describes the basic methods for compiling simple code format tags in WordPress ,. The following describes the basic method for compiling simple code format tags in WordPress. WordPress simple code is something similar to Forum tags. the format is similar to replacing angle brackets with Html tags in brackets. Briefly describe the basic method for compiling simple code format tags in WordPress,

WordPress is a simple code similar to a forum tag. the format is similar to an Html tag that replaces angle brackets with brackets. Many people call short codes, but the official translation should be short codes. correct them here.

The logic for simple code development is relatively simple. it mainly involves adding, deleting, and judging. This article will introduce it all.

Simple code format

The simple code format is very flexible. it can be attributes, no attributes, closed, non-closed, and so on:

[Example]

[Example] content [/example]

[Example attr = "attribute" attr-hide = "1"] content [/example]

[Example "attribute"]

Add a QR code

To add a QR code, you need to use the add_shortcode () function. There are two attributes: The first is the short code name, and the second is the short code callback function.

add_shortcode( $tag, $func );

For example, add a simple code named test and call back the bing_javascode_test () function:

function Bing_shortcode_test( $attr, $content ){  return 'Hello World!';}add_shortcode( 'test', 'Bing_shortcode_test' );

After you add [test] to the article, "Hello World!" will be output !".

The preceding example shows that the callback function of the simple code needs to receive two parameters. The first is all the attributes of the simplified code, which are stored through arrays; the second is the content of the simplified code (the content in the simplified code ).

Remove the QR code

The remove_shortcode () function can remove a simple code. you only need to specify the name of the simple code to remove it.

remove_shortcode( 'test' );

The remove_all_inclucodes () function is used to remove all the currently added shortcodes.

remove_all_shortcodes();

Judge the QR code

There are two functions for determining the short code. the shortcode_exists () function determines whether the short code exists.

Remove_all_inclucodes (); if (inclucode_exists ('test') echo 'simple code test exist'; // falseadd_inclucode ('test', 'Bing _ inclucode_test '); if (inclucode_exists ('test') echo 'simple code test exists'; // True

There is also a has_shortcode () function to determine whether a simple code is displayed in the string.

$ Content = 'test test test'; if (has_shortcode ($ content, 'test') echo 'The test shortcode is included in the string '; // False $ content = 'test test [test] test [/test] test test'; if (has_coding code ($ content, 'test ')) echo 'test short code' in the string; // True

Execution code

The do_shortcode () function is used to find the shortcode in the string, and call the previously added callback function in the shortcode to execute the shortcode as needed.

Hooks added by WordPress:

add_filter( 'the_content', 'do_shortcode', 11 );

Example:

Function bing_inclucode_test ($ attr, $ content) {return 'Hello World! ';} Add_shortcode ('test', 'Bing _ shortcode_test'); $ content = 'Test and test [test] test and test test '; echo do_domaincode ($ content); // test and test Hello World! Test

Simple code attributes

The simple code supports attributes in various formats and receives the first parameter of the simple code callback function. If you want to set the default value for the parameter, you can use the shortcode_atts () function:

Function bing_inclucode_test ($ attr, $ content) {extract (inclucode_atts (array ('URL' => 'http: // www.bgbk.org ', 'hide' => false, 'text' => 'Click to hide/display'), $ attr); $ hide = $ hide? 'Style = "display: none;" ': ''; return''. $ text. '';} add_coding code ('test', 'Bing _ javascode_test ');


The script is loaded only when the simple code is used in the page.
In the development process, this problem is sometimes encountered: the simple code module needs to load JS or CSS scripts, and when the page does not use the simple code, it will cause a waste of resources.

For example, the Google map plug-in below:


// Add the simple code function Bing_add_google_map ($ atts, $ content) {// content ...} add_shortcode ('Google _ map', 'Bing _ add_google_map '); // mount the script function Bing_add_javascript () {wp_enqueue_script ('map _ scripts ');} add_action ('WP _ enqueue_scripts ', 'Bing _ add_javascript ');

The script must be loaded only when the [google_map] simple code is used on the page. how can this be done?

In fact, it is very simple. you only need to mount the script in the footer when the simple code function is triggered.

// Add the simple code function Bing_add_google_map ($ atts, $ content) {$ GLOBALS ['Google _ map_shortcode'] = true; return 'Map code ';} add_shortcode ('Google _ map', 'Bing _ add_google_map '); // mount the script function Bing_add_javascript () {global $ google_map_shortcode; if (isset ($ google_map_shortcode) & $ google_map_shortcode) wp_enqueue_script ('map _ Scripts');} add_action ('WP _ footer ', 'Bing _ add_javascript ');

Summary

Simple code is a very powerful function. it is a great extension to the content of the article. it makes it easy and convenient to add something.

All simple code functions are in the wp-shortdes/shortcode. php file. if you have the ability, read and understand the principles.

Articles you may be interested in:
  • PHP code instance for page number navigation added to the article list page in a WordPress topic
  • Analysis of function hook functions in WordPress and its basic usage
  • Enable theme support gadgets and add plug-ins in WordPress

WordPress short code is something similar to a forum tag. the format is similar to an Html tag that replaces angle brackets with brackets. Simple...

Related Article

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.