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中简码格式标签编写的基本方法》的一些看法。更多内容请查看本栏目更多内容!
1、修正文章页面模板single.php关上模版文件中的single.php,在其中搜寻在这行上面加上:复制代码代码如下:...
wordpress经过以后文章的ID获取文章的信息用的极多,在wordpress二次开发中,上篇文章说过,获取以后文章id...
single_cat_title()函数 single_cat_title()函数,日常中咱们很少会用到,但这个函数会给咱们处理很多成绩,...
本文实例总结了WordPress评论增加楼层显示的办法。分享给大家供大家参考。详细剖析如下: 第一步:要在评论...
七牛云存储是由七牛提供的在线存储效劳,经过云端接口向企业客户提供网上有限存储空间,和传统的云存储效劳...
get_post()(获取一篇文章) get_post() 函数能够依据 ID 查问一篇文章的信息,还能前往循环中的以后文章。...