麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

詳解WordPress中簡(jiǎn)碼格式標(biāo)簽編寫(xiě)的基本方法

2024-05-04 23:41:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了詳解WordPress中簡(jiǎn)碼格式標(biāo)簽編寫(xiě)的基本方法,文中講到了添加和移除簡(jiǎn)碼等的一些PHP函數(shù)的用法,需要的朋友可以參考下
 

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 文件里,有能力的朋友可以閱讀一下,了解原理。



注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费观看亚洲视频 | 91网址在线播放 | 一区二区免费 | 播色网| 看91视频 | 欧美成人性生活片 | 黄色片视频免费观看 | 欧美日韩国产一区二区三区在线观看 | 午夜国产小视频 | 欧美性生交zzzzzxxxxx | 国产视频在线观看一区二区三区 | 小情侣嗯啊哦视频www | 一区二区精品视频 | jizzyouxxxx| 欧美日韩在线播放 | 最新一级毛片 | 午夜精品一区二区三区免费 | 久久国产精品99久久人人澡 | 亚洲成人第一页 | 国产视频在线播放 | 国产午夜三级一区二区三桃花影视 | 欧洲成人综合网 | 久久精品中文字幕一区二区 | av免费av| 中文字幕爱爱视频 | 黄色av网站在线观看 | xxxxxx性| 久久精品久久精品久久精品 | 鲁久久 | 伊人99re | 国产精品午夜一区 | 久久艹艹艹 | 美女黄色毛片免费看 | 爱爱视频天天干 | 一级空姐毛片 | 久久久久久久久浪潮精品 | 中文字幕亚洲视频 | 亚洲片在线观看 | 国产精品久久久久久婷婷天堂 | 一分钟免费观看完整版电影 | 深夜激情视频 |