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中简码格式标签编写的基本方法》的一些看法。更多内容请查看本栏目更多内容!
很多采纳WordPress顺序搭建的博客都相当注重与读者之间的互动,以评论为例,为了进步读者的体验品质,有的博...
关于日拜访量还不错的WordPress网站来说,一定都会存在很多待审核的渣滓评论,假如要经过WP后盾删除,那几乎...
esc_attr()(过滤属性) 普通在写 Html 代码的标签属性的时分会是下边的格局: <input type="text" name="...
明天在装置wordpress3.0.1的时分,写入数据库阶段时呈现了一大片谬误提醒信息: 复制代码代码如下:WordPres...
在没有本人的网站的时分很想领有一个属于本人的网站,能够说是本人的一个欲望吧。然而当我真正的运转起来了...
query posts是一个十分好用的调用文章函数,能够做到同页面内显示多种特定范畴的文章,例如能够调用某分类、...