WordPress 簡(jiǎn)碼是一種類似于論壇標(biāo)簽的東西,格式類似于把尖括號(hào)換成中括號(hào)的 Html 標(biāo)簽。簡(jiǎn)碼很多人叫做短代碼,但官方的翻譯應(yīng)該是簡(jiǎn)碼,在這里糾正一下。
簡(jiǎn)碼的開(kāi)發(fā)的邏輯比較簡(jiǎn)單,主要就是添加、刪除和判斷,會(huì)在本文全部介紹。
簡(jiǎn)碼格式
簡(jiǎn)碼的格式非常靈活,可以是有屬性、無(wú)屬性、閉合、非閉合等等:
[example]
[example]內(nèi)容[/example]
[example attr="屬性" attr-hide="1"]內(nèi)容[/example]
[example "屬性"]
添加簡(jiǎn)碼
添加簡(jiǎn)碼需要使用 add_shortcode() 函數(shù),兩個(gè)屬性,第一個(gè)為簡(jiǎn)碼名,第二個(gè)是簡(jiǎn)碼的回調(diào)函數(shù)。
add_shortcode( $tag, $func );
例如添加名為 test 的簡(jiǎn)碼,回調(diào) Bing_shortcode_test() 函數(shù):
function Bing_shortcode_test( $attr, $content ){ return 'Hello World!';}add_shortcode( 'test', 'Bing_shortcode_test' );
在文章中添加 [test] 就會(huì)輸出 “Hello World!”。
從上邊的例子可以看到,簡(jiǎn)碼的回調(diào)函數(shù)需要接收兩個(gè)參數(shù)。第一個(gè)是簡(jiǎn)碼所有的屬性,通過(guò)數(shù)組儲(chǔ)存;第二個(gè)是簡(jiǎn)碼的內(nèi)容(閉合簡(jiǎn)碼中的內(nèi)容)。
移除簡(jiǎn)碼
remove_shortcode() 函數(shù)可以移除一個(gè)簡(jiǎn)碼,只需要指定簡(jiǎn)碼的名稱即可移除。
remove_shortcode( 'test' );
remove_all_shortcodes() 函數(shù)用來(lái)移除當(dāng)前添加的所有簡(jiǎn)碼。
remove_all_shortcodes();
判斷簡(jiǎn)碼
關(guān)于判斷簡(jiǎn)碼,有兩個(gè)函數(shù),shortcode_exists() 函數(shù)判斷簡(jiǎn)碼是否存在。
remove_all_shortcodes();if( shortcode_exists( 'test' ) ) echo '簡(jiǎn)碼 test 存在';//Falseadd_shortcode( 'test', 'Bing_shortcode_test' );if( shortcode_exists( 'test' ) ) echo '簡(jiǎn)碼 test 存在';//True
還有一個(gè) has_shortcode() 函數(shù),判斷字符串中是否出現(xiàn)某某簡(jiǎn)碼。
$content = '測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試測(cè)試';if( has_shortcode( $content, 'test' ) ) echo '字符串中有 test 簡(jiǎn)碼';//False$content = '測(cè)試測(cè)試測(cè)試測(cè)[test]測(cè)試[/test]試測(cè)試測(cè)試測(cè)試測(cè)試';if( has_shortcode( $content, 'test' ) ) echo '字符串中有 test 簡(jiǎn)碼';//True
執(zhí)行簡(jiǎn)碼
do_shortcode() 函數(shù)用來(lái)在字符串中查找簡(jiǎn)碼,并在簡(jiǎn)碼處調(diào)用之前添加的回調(diào)函數(shù),把簡(jiǎn)碼執(zhí)行成需要的內(nèi)容。
WordPress 添加的鉤子:
add_filter( 'the_content', 'do_shortcode', 11 );
例子:
function Bing_shortcode_test( $attr, $content ){ return 'Hello World!';}add_shortcode( 'test', 'Bing_shortcode_test' );$content = '測(cè)試測(cè)試測(cè)試測(cè)[test]試測(cè)試測(cè)試測(cè)試測(cè)試';echo do_shortcode( $content );//測(cè)試測(cè)試測(cè)試測(cè)Hello World!試測(cè)試測(cè)試測(cè)試測(cè)試
簡(jiǎn)碼屬性
簡(jiǎn)碼支持各種格式的屬性,接受給簡(jiǎn)碼回調(diào)函數(shù)的第一個(gè)參數(shù)。如果你要給參數(shù)設(shè)置默認(rèn)值,可以使用 shortcode_atts() 函數(shù):
function Bing_shortcode_test( $attr, $content ){ extract( shortcode_atts( array( 'url' => 'http://www.bgbk.org', 'hide' => false, 'text' => '點(diǎn)擊隱藏 / 顯示' ), $attr ) ); $hide = $hide ? ' style="display:none;"' : ''; return '<a href="' . $url . '"' . $hide . '>' . $text . '</a>';}add_shortcode( 'test', 'Bing_shortcode_test' );
只有頁(yè)面中使用了簡(jiǎn)碼的時(shí)候才加載腳本
而在開(kāi)發(fā)的過(guò)程中,有時(shí)會(huì)遇到這種問(wèn)題:簡(jiǎn)碼模塊需要加載 JS 或者 CSS 腳本,而當(dāng)頁(yè)面沒(méi)有使用簡(jiǎn)碼的時(shí)候就會(huì)造成資源浪費(fèi)。
比如下邊的這個(gè) Google 地圖插件:
//添加簡(jiǎn)碼function Bing_add_google_map( $atts, $content ){ //content...}add_shortcode( 'google_map', 'Bing_add_google_map'); //掛載腳本function Bing_add_javascript(){ wp_enqueue_script( 'map_scripts' );}add_action( 'wp_enqueue_scripts', 'Bing_add_javascript' );
只有在頁(yè)面中使用了 [google_map] 簡(jiǎn)碼的時(shí)候才需要加載腳本,這怎么做到呢?
其實(shí)很簡(jiǎn)單,只需要在簡(jiǎn)碼函數(shù)觸發(fā)的時(shí)候在頁(yè)腳掛載腳本即可。
//添加簡(jiǎn)碼function Bing_add_google_map( $atts, $content ){ $GLOBALS['google_map_shortcode'] = true; return '地圖的代碼';}add_shortcode( 'google_map', 'Bing_add_google_map'); //掛載腳本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' );
總結(jié)
簡(jiǎn)碼是個(gè)非常強(qiáng)大的功能,對(duì)文章內(nèi)容是一種很好的擴(kuò)展,利用好可以讓添加某些東西變的方便快捷。
關(guān)于簡(jiǎn)碼的函數(shù)都在:wp-includes/shortcode.php 文件里,有能力的朋友可以閱讀一下,了解原理。