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 '<a href="' . $url . '"' . $hide . '>' . $text . '</a>'; } 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中简码格式标签编写的基本方法》的一些看法。更多内容请查看本栏目更多内容!
wpslugtranslate插件引见: 经过该插件,能够让wordpress中文博客的博主应用/%postname%.html方式的固定链接...
而后你犯晕了吧。次要缘由是某些主题没有增加WP-postviews这款插件的支持代码,凑巧笔者的Green Park 2这款...
WordPress 默许有 “订阅者”、“投稿者”、“作者”、“编辑” 和 “治理员” 五个用户角色,权限由低到高...
明天吃完午饭,劳动劳动,最近搞了一下google adsense,不过最终的审核没经过,我想会不会是界面不难看呢,...
让WordPress登录页的明码框显示最初一个输出的字符 在通常状况下,网页的设计者们会将明码输出框中的一切字...
这个烦恼的成绩是有处理计划的,并且十分简略。 1、找到主标题录下的Header.php文件 2、应用FTP或Dreamweav...