詳解WordPress中簡碼格式標籤編寫的基本方法,
WordPress 簡碼是一種類似於論壇標籤的東西,格式類似於把角括弧換成中括弧的 Html 標籤。簡碼很多人叫做短代碼,但官方的翻譯應該是簡碼,在這裡糾正一下。
簡碼的開發的邏輯比較簡單,主要就是添加、刪除和判斷,會在本文全部介紹。
簡碼格式
簡碼的格式非常靈活,可以是有屬性、無屬性、閉合、非閉合等等:
[example]
[example]內容[/example]
[example attr="屬性" attr-hide="1"]內容[/example]
[example "屬性"]
添加簡碼
添加簡碼需要使用 add_shortcode() 函數,兩個屬性,第一個為簡碼名,第二個是簡碼的回呼函數。
add_shortcode( $tag, $func );
例如添加名為 test 的簡碼,回調 Bing_shortcode_test() 函數:
function Bing_shortcode_test( $attr, $content ){ return 'Hello World!';}add_shortcode( 'test', 'Bing_shortcode_test' );
在文章中添加 [test] 就會輸出 “Hello World!”。
從上邊的例子可以看到,簡碼的回呼函數需要接收兩個參數。第一個是簡碼所有的屬性,通過數組儲存;第二個是簡碼的內容(閉合簡碼中的內容)。
移除簡碼
remove_shortcode() 函數可以移除一個簡碼,只需要指定簡碼的名稱即可移除。
remove_shortcode( 'test' );
remove_all_shortcodes() 函數用來移除當前添加的所有簡碼。
remove_all_shortcodes();
判斷簡碼
關於判斷簡碼,有兩個函數,shortcode_exists() 函數判斷簡碼是否存在。
remove_all_shortcodes();if( shortcode_exists( 'test' ) ) echo '簡碼 test 存在';//Falseadd_shortcode( 'test', 'Bing_shortcode_test' );if( shortcode_exists( 'test' ) ) echo '簡碼 test 存在';//True
還有一個 has_shortcode() 函數,判斷字串中是否出現某某簡碼。
$content = '測試測試測試測試測試測試測試測試';if( has_shortcode( $content, 'test' ) ) echo '字串中有 test 簡碼';//False$content = '測試測試測試測[test]測試[/test]試測試測試測試測試';if( has_shortcode( $content, 'test' ) ) echo '字串中有 test 簡碼';//True
執行簡碼
do_shortcode() 函數用來在字串中尋找簡碼,並在簡碼處調用之前添加的回呼函數,把簡碼執行成需要的內容。
WordPress 添加的鉤子:
add_filter( 'the_content', 'do_shortcode', 11 );
例子:
function Bing_shortcode_test( $attr, $content ){ return 'Hello World!';}add_shortcode( 'test', 'Bing_shortcode_test' );$content = '測試測試測試測[test]試測試測試測試測試';echo do_shortcode( $content );//測試測試測試測Hello World!試測試測試測試測試
簡碼屬性
簡碼支援各種格式的屬性,接受給簡碼回呼函數的第一個參數。如果你要給參數設定預設值,可以使用 shortcode_atts() 函數:
function Bing_shortcode_test( $attr, $content ){ extract( shortcode_atts( array( 'url' => 'http://www.bgbk.org', 'hide' => false, 'text' => '點擊隱藏 / 顯示' ), $attr ) ); $hide = $hide ? ' style="display:none;"' : ''; return '' . $text . '';}add_shortcode( 'test', 'Bing_shortcode_test' );
只有頁面中使用了簡碼的時候才載入指令碼
而在開發的過程中,有時會遇到這種問題:簡碼模組需要載入 JS 或者 CSS 指令碼,而當頁面沒有使用簡碼的時候就會造成資源浪費。
比如下邊的這個 Google 地圖外掛程式:
//添加簡碼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' );
只有在頁面中使用了 [google_map] 簡碼的時候才需要載入指令碼,這怎麼做到呢?
其實很簡單,只需要在簡碼函數觸發的時候在頁尾掛載指令碼即可。
//添加簡碼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' );
總結
簡碼是個非常強大的功能,對文章內容是一種很好的擴充,利用好可以讓添加某些東西變的方便快捷。
關於簡碼的函數都在:wp-includes/shortcode.php 檔案裡,有能力的朋友可以閱讀一下,瞭解原理。
您可能感興趣的文章:
- WordPress主題中添加文章列表頁頁碼導航的PHP代碼執行個體
- 解析WordPress中函數鉤子hook的作用及基本用法
- WordPress中使主題支援小工具以及添加外掛程式啟用函數
http://www.bkjia.com/PHPjc/1084559.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1084559.htmlTechArticle詳解WordPress中簡碼格式標籤編寫的基本方法, WordPress 簡碼是一種類似於論壇標籤的東西,格式類似於把角括弧換成中括弧的 Html 標籤。簡...