In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The purpose of this article is to share with you the content of a sample analysis of simple code format tags in WordPress. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
WordPress short codes are similar to forum tags in a format similar to Html tags that replace angle brackets with square brackets. Simple code is called short code by many people, but the official translation should be short code. Correct it here.
Simple code format
The format of the simple code is very flexible, which can be attribute, non-attribute, closed, non-closed, and so on:
[example]
[example] content [/ example]
[example attr= "attribute" attr-hide= "1"] content [/ example]
[example "Properties"]
Add simple code
To add a simple code, you need to use the add_shortcode () function, with two attributes, the first is the name of the simple code, and the second is the callback function of the simple code.
Add_shortcode ($tag, $func)
For example, add a simple code named test and call back the Bing_shortcode_test () function:
Function Bing_shortcode_test ($attr, $content) {return 'Hello Worldwaters;} add_shortcode (' test', 'Bing_shortcode_test')
Adding [test] to the article will output "Hello World!".
As you can see from the example above, the callback function of the simple code needs to receive two parameters. The first is all the attributes of the simple code, which are stored in an array; the second is the content of the simple code (closing the contents of the simple code).
Remove simple code
The remove_shortcode () function removes a simple code by specifying the name of the simple code.
Remove_shortcode ('test')
The remove_all_shortcodes () function is used to remove all currently added short codes.
Remove_all_shortcodes ()
Judge simple code
For judging a simple code, there are two functions, and the shortcode_exists () function determines whether a simple code exists or not.
Remove_all_shortcodes (); if (shortcode_exists ('test')) echo' short code test exists'; / / Falseadd_shortcode ('test',' Bing_shortcode_test'); if (shortcode_exists ('test')) echo' simplified code test exists'; / / True
There is also a has_shortcode () function to determine whether a so-and-so brief code appears in the string.
$content = 'test test'; if (has_shortcode ($content, 'test')) echo' string has test short code'; / / False$content = 'test [test] test [/ test] test'; if (has_shortcode ($content, 'test')) echo' has test short code in string'; / / True
Executive simple code
The do_shortcode () function is used to find the simple code in the string and call the callback function added before the short code to execute the short code into the desired content.
Hooks added by WordPress:
Add_filter ('the_content',' do_shortcode', 11)
Example:
Function Bing_shortcode_test ($attr, $content) {return 'Hello Worldwide testing;} add_shortcode (' test', 'Bing_shortcode_test'); $content =' Test [test] trial Test'; echo do_shortcode ($content); / / Test Hello World! Trial test
Simple code attribute
The simple code supports attributes in various formats and accepts the first parameter to the callback function of the simple code. If you want to set default values for parameters, you can use the shortcode_atts () function:
Function Bing_shortcode_test ($attr, $content) {extract (array ('url' = >' http://www.bgbk.org', 'hide' = > false,' text' = > 'Click hide / Show'), $attr); $hide = $hide?':'; return''. $text. '';} add_shortcode ('test',' Bing_shortcode_test')
The script is loaded only when a simple code is used on the page
In the process of development, we sometimes encounter this problem: the simplified code module needs to load JS or CSS scripts, and when the page does not use the simplified code, it will cause a waste of resources.
For example, the following Google map plug-in:
/ / add the short 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 needs to be loaded only if the [google_map] simplified code is used on the page. How do you do that?
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 short 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 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'); Thank you for reading! This is the end of this article on "sample Analysis of simple Code format tags in WordPress". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.