Detailed analysis of WordPress simple code format tag writing basic methods _php skills

Source: Internet
Author: User

WordPress simple code is something similar to a forum label, formatted like an Html tag that replaces the angle bracket with a bracket. Simple code Many people called Short code, but the official translation should be simple code, here to correct.

Simple code development Logic is simpler, the main is to add, delete and judge, will be introduced in this article.

Simple code format

Simple code format is very flexible, can be attributes, no attributes, closed, not closed, and so on:

[Example]

[Example] content [/example]

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

[Example Properties]

Add simple code

Add simple code need to use the Add_shortcode () function, two attributes, the first is a simple code name, the second is a simple code callback function.

Add_shortcode ($tag, $func);

For example, to add a simple code named Test, callback the Bing_shortcode_test () function:

function Bing_shortcode_test ($attr, $content) {return
  ' Hello world! ';
}
Add_shortcode (' Test ', ' bing_shortcode_test ');

adding [test] to the article will output "Hello world!".

As you can see from the above example, the callback function of the simple code needs to receive two parameters. The first is the attribute of the simple code, which is stored through the array, and the second is the content of the simple code (the contents of the closed code).

Remove Simple code

The Remove_shortcode () function can remove a simple code by simply specifying the name of the simple code.

Remove_shortcode (' Test ');

The Remove_all_shortcodes () function is used to remove all the code that is currently being added.

Remove_all_shortcodes ();

Judge Simple Code

There are two functions to judge the simple code, and the Shortcode_exists () function determines whether the simple code exists.

Remove_all_shortcodes ();
if (shortcode_exists (' Test ')) echo ' Simple code test exists ';//false
add_shortcode (' Test ', ' bing_shortcode_test ');
if (shortcode_exists (' Test ')) echo ' Simple code test exists ';//true

There is also a has_shortcode () function that determines whether a certain code is present in the string.

$content = ' test test testing test test ';
if (Has_shortcode ($content, ' Test ')) the Echo ' string has a test simple code ';//false $content = ' test test [test
] testing [/test] test test '; 
   if (Has_shortcode ($content, ' Test ')) the Echo ' string has a test Jane code ';//true

Execute Simple code

The Do_shortcode () function is used to find a simple code in a string, and a callback function added before the code is invoked to execute the code into the desired content.

WordPress Added Hooks:

Add_filter (' the_content ', ' Do_shortcode ', 11);

Example:

function Bing_shortcode_test ($attr, $content) {return
  ' Hello world! ';
}
Add_shortcode (' Test ', ' bing_shortcode_test ');
$content = ' test test testing test test ';
echo Do_shortcode ($content);///test test Test world! test test test

Simple code attribute

The simple code supports the properties of various formats and accepts the first argument to the simple code callback function. If you want to set a default value for the parameter, you can use the Shortcode_atts () function:

function Bing_shortcode_test ($attr, $content) {
  extract (Shortcode_atts) (Array (
    ' url ' => ' http:// Www.bgbk.org ',
    ' hide ' => false,
    ' text ' => ' click hide/Show ')
  , $attr));
  $hide = $hide? ' style= ' Display:none; "': ';
  Return ' <a href= '. $url. '"' . $hide. ' > '. $text. ' </a> ';
}
Add_shortcode (' Test ', ' bing_shortcode_test ');


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

For example, this Google map plugin below:


Add simple code
function Bing_add_google_map ($atts, $content) {
  //content ...
}
Add_shortcode (' Google_map ', ' bing_add_google_map ');
 
Mount script
function bing_add_javascript () {
  wp_enqueue_script (' map_scripts ');
}
Add_action (' wp_enqueue_scripts ', ' bing_add_javascript ');

How do you do this only if you need to load the script when you use [Google_map] code in the page?

In fact, it's simple to mount the script in the footer when the simple code function is triggered.

Add simple code
function Bing_add_google_map ($atts, $content) {
  $GLOBALS [' google_map_shortcode '] = true;
  Return ' code for map ';
}
Add_shortcode (' Google_map ', ' bing_add_google_map ');
 
Mount 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 ');

Summarize

Simple code is a very powerful function, the content of the article is a good extension, the use of good to add something to become convenient and quick.

On the simple code functions are: wp-includes/shortcode.php file, the ability of friends can read, understand the principle.

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.