《PHP應(yīng)用:詳解WordPress中簡(jiǎn)碼格式標(biāo)簽編寫的基本方法》要點(diǎn):
本文介紹了PHP應(yīng)用:詳解WordPress中簡(jiǎn)碼格式標(biāo)簽編寫的基本方法,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP實(shí)戰(zhàn)WordPress 簡(jiǎn)碼是一種類似于論壇標(biāo)簽的東西,格式類似于把尖括號(hào)換成中括號(hào)的 Html 標(biāo)簽.簡(jiǎn)碼很多人叫做短代碼,但官方的翻譯應(yīng)該是簡(jiǎn)碼,在這里糾正一下.
PHP實(shí)戰(zhàn)簡(jiǎn)碼的開發(fā)的邏輯比擬簡(jiǎn)單,主要就是添加、刪除和判斷,會(huì)在本文全部介紹.
PHP實(shí)戰(zhàn)簡(jiǎn)碼格式
PHP實(shí)戰(zhàn)簡(jiǎn)碼的格式非常靈活,可以是有屬性、無屬性、閉合、非閉合等等:
PHP實(shí)戰(zhàn)[example]
PHP實(shí)戰(zhàn)[example]內(nèi)容[/example]
PHP實(shí)戰(zhàn)[example attr="屬性" attr-hide="1"]內(nèi)容[/example]
PHP實(shí)戰(zhàn)[example "屬性"]
PHP實(shí)戰(zhàn)添加簡(jiǎn)碼
PHP實(shí)戰(zhàn)添加簡(jiǎn)碼必要使用 add_shortcode() 函數(shù),兩個(gè)屬性,第一個(gè)為簡(jiǎn)碼名,第二個(gè)是簡(jiǎn)碼的回調(diào)函數(shù).
PHP實(shí)戰(zhàn)
add_shortcode( $tag, $func );
PHP實(shí)戰(zhàn)例如添加名為 test 的簡(jiǎn)碼,回調(diào) Bing_shortcode_test() 函數(shù):
PHP實(shí)戰(zhàn)
function Bing_shortcode_test( $attr, $content ){
return 'Hello World!';
}
add_shortcode( 'test', 'Bing_shortcode_test' );
PHP實(shí)戰(zhàn)在文章中添加 [test] 就會(huì)輸出 “Hello World!”.
PHP實(shí)戰(zhàn)從上邊的例子可以看到,簡(jiǎn)碼的回調(diào)函數(shù)必要接收兩個(gè)參數(shù).第一個(gè)是簡(jiǎn)碼所有的屬性,通過數(shù)組儲(chǔ)存;第二個(gè)是簡(jiǎn)碼的內(nèi)容(閉合簡(jiǎn)碼中的內(nèi)容).
PHP實(shí)戰(zhàn)移除簡(jiǎn)碼
PHP實(shí)戰(zhàn)remove_shortcode() 函數(shù)可以移除一個(gè)簡(jiǎn)碼,只必要指定簡(jiǎn)碼的名稱即可移除.
PHP實(shí)戰(zhàn)
remove_shortcode( 'test' );
PHP實(shí)戰(zhàn)remove_all_shortcodes() 函數(shù)用來移除當(dāng)前添加的所有簡(jiǎn)碼.
PHP實(shí)戰(zhàn)
remove_all_shortcodes();
PHP實(shí)戰(zhàn)判斷簡(jiǎn)碼
PHP實(shí)戰(zhàn)關(guān)于判斷簡(jiǎn)碼,有兩個(gè)函數(shù),shortcode_exists() 函數(shù)判斷簡(jiǎn)碼是否存在.
PHP實(shí)戰(zhàn)
remove_all_shortcodes();
if( shortcode_exists( 'test' ) ) echo '簡(jiǎn)碼 test 存在';//False
add_shortcode( 'test', 'Bing_shortcode_test' );
if( shortcode_exists( 'test' ) ) echo '簡(jiǎn)碼 test 存在';//True
PHP實(shí)戰(zhàn)還有一個(gè) has_shortcode() 函數(shù),判斷字符串中是否出現(xiàn)某某簡(jiǎn)碼.
PHP實(shí)戰(zhà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
PHP實(shí)戰(zhàn)執(zhí)行簡(jiǎn)碼
PHP實(shí)戰(zhàn)do_shortcode() 函數(shù)用來在字符串中查找簡(jiǎn)碼,并在簡(jiǎn)碼處調(diào)用之前添加的回調(diào)函數(shù),把簡(jiǎn)碼執(zhí)行成必要的內(nèi)容.
PHP實(shí)戰(zhàn)WordPress 添加的鉤子:
PHP實(shí)戰(zhàn)
add_filter( 'the_content', 'do_shortcode', 11 );
PHP實(shí)戰(zhàn)例子:
PHP實(shí)戰(zhàn)
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è)試
PHP實(shí)戰(zhàn)簡(jiǎn)碼屬性
PHP實(shí)戰(zhàn)簡(jiǎn)碼支持各種格式的屬性,接受給簡(jiǎn)碼回調(diào)函數(shù)的第一個(gè)參數(shù).如果你要給參數(shù)設(shè)置默認(rèn)值,可以使用 shortcode_atts() 函數(shù):
PHP實(shí)戰(zhàn)
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' );
PHP實(shí)戰(zhàn)
只有頁面中使用了簡(jiǎn)碼的時(shí)候才加載腳本
而在開發(fā)的過程中,有時(shí)會(huì)遇到這種問題:簡(jiǎn)碼模塊需要加載 JS 或者 CSS 腳本,而當(dāng)頁面沒有使用簡(jiǎn)碼的時(shí)候就會(huì)造成資源浪費(fèi).
PHP實(shí)戰(zhàn)好比下邊的這個(gè) Google 地圖插件:
PHP實(shí)戰(zhàn)
//添加簡(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' );
PHP實(shí)戰(zhàn)只有在頁面中使用了 [google_map] 簡(jiǎn)碼的時(shí)候才必要加載腳本,這怎么做到呢?
PHP實(shí)戰(zhàn)其實(shí)很簡(jiǎn)單,只必要在簡(jiǎn)碼函數(shù)觸發(fā)的時(shí)候在頁腳掛載腳本即可.
PHP實(shí)戰(zhàn)
//添加簡(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' );
PHP實(shí)戰(zhàn)總結(jié)
PHP實(shí)戰(zhàn)簡(jiǎn)碼是個(gè)非常強(qiáng)大的功能,對(duì)文章內(nèi)容是一種很好的擴(kuò)展,利用好可以讓添加某些東西變的便利快捷.
PHP實(shí)戰(zhàn)關(guān)于簡(jiǎn)碼的函數(shù)都在:wp-includes/shortcode.php 文件里,有才能的朋友可以閱讀一下,了解原理.
《PHP應(yīng)用:詳解WordPress中簡(jiǎn)碼格式標(biāo)簽編寫的基本方法》是否對(duì)您有啟發(fā),歡迎查看更多與《PHP應(yīng)用:詳解WordPress中簡(jiǎn)碼格式標(biāo)簽編寫的基本方法》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/8172.html