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 存在';//False add_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' );
总结
简码是个非常强大的功能,对文章内容是一种很好的扩展,利用好可以让添加某些东西变的方便快捷。
更多相关教程请访问 php编程从入门到精通全套视频教程

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具