wordpress shortCode API
wordpress2.5增加了一个新的简码(shortCode)API,是一个简单的函数集,用于创建发布内容中的宏代码。简码形式如下:[play] or [codebox]...[/codebox]
有了简码API,创建支持下列属性的简码就变得容易了。简码API能够处理所有形式的解析工作,不再需要为每个简码编写相应的正则表达式。Helper函数也包含进API了,用于设置和获取默认属性。对于自动关闭和被动关闭的简码,API都提供支持。
概述
简码是用来提供处理函数的。简码处理器在很大程序上类似于wordpress的过滤器filter:它们都接受参数(属性)并返回结果(简码输出)。函数add_shortcode()用于注册简码处理器。它有两个参数:简码名(用于文章本身的字符串)和处理器函数名。简码处理器函数应该接受一到两个参数:$atts(属性的数组)和$content(其中的内容)(如果简码用于包含其中的表单)。
例如:function my_shortcode_handler($atts, $content=null) {}用于注册简码处理器的API调用,
形式如下:add_shortcode('my-shortcode', 'my_shortcode_handler');
当the_content显示的时候,简码API就会解析所有的简码,比如"[my-shortcode]",如果有属性和内容,就会把它们分离并解析出来,然后传递给相应的简码处理器函数。任何由处理器返回的字符串都会被插入到文章本身,替换掉简码的位置。简码属性如下:[my-shortcode foo="bar" baz="bing"]code content........[/my-shortcode]它们会被转化为关联数组(如下),作为$atts参数传递给处理器函数;内容作为$content传递。
array( 'foo' => 'bar', 'baz' => 'bing')关联数组的键是属性名,而值就是相应的属性值。
例子
paypal-shortcuts:This plugin allow to insert Paypal buttons in your posts or pages, with a shortcode like [paypal type="add|view"].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | define("PAYPAL_MAIL","your-email-here"); // your Paypal email address define("CURRENCY","EUR"); // your Paypal currency (EUR, USD) define("ALT_ADD","Add to cart (Paypal)"); // alternate text for "Add to cart" image define("ALT_VIEW","View Paypal cart"); // alternate text for "View cart" image // [paypal type="add" name="Item Name" amount="12.99"] // [paypal type="view"] function eft_paypal_shortcode($atts) { switch($atts['type']): case "add": $code = ' <form name="_xclick" target="paypal" action="https://www.paypal.com" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="business" value="'.PAYPAL_MAIL.'"> <input type="hidden" name="currency_code" value="'.CURRENCY.'"> <input type="hidden" name="item_name" value="'.$atts['name'].'"> <input type="hidden" name="amount" value="'.$atts['amount'].'"> <input type="image" src="http://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="'.ALT_ADD.'"> <input type="hidden" name="add" value="1"> </form>'; break; case "view": $code = ' <form name="_xclick" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="business" value="'.PAYPAL_MAIL.'"> <input type="image" src="https://www.paypal.com/en_US/i/btn/view_cart_new.gif" border="0" name="submit" alt="'.ALT_VIEW.'"> <input type="hidden" name="display" value="1"> </form> '; break; endswitch; echo $code; } add_shortcode('paypal', 'eft_paypal_shortcode'); |
要让文中的某个位置加入Flash动画,在短码出来之前,通常的做法是,给文章来这么一段
1 2 3 4 5 6 | <object width="400" height="300"> <param name="movie" value="index.swf"></param> <param name="allowScriptAccess" value="always"></param> <param name="wmode" value="transparent"></param> <embed src="index.swf" type="application/x-shockwave-flash" width="400" height="300" allowFullScreen="true" wmode="transparent" allowScriptAccess="always"></embed> </object> |
而现在只要[flash width=400 height=300]index.swf[/flash]或者更短一些[flash]index.swf[/flash]
前提是,将以下内容添加到当前模板文件的functions.php,让短码认得[flash]这个标签
1 2 3 4 5 6 7 8 9 10 | function show_flash( $atts, $content = null ) { extract( shortcode_atts( array('height' => '300','width' => '400'), $atts ) ); return '<object width="'.$width.'" height="'.$height.'"> <param name="movie" value="'.$content.'"></param> <param name="allowScriptAccess" value="always"></param> <param name="wmode" value="transparent"></param> <embed src="'.$content.'" type="application/x-shockwave-flash" width="'.$width.'" height="'.$height.'" allowFullScreen="true" wmode="transparent" allowScriptAccess="always"></embed> </object>' ; } add_shortcode('flash', 'show_flash'); |
同理,您也可以将音频、视频或者其它一些应用,甚至是游戏等插进去。
官方使用说明请看这里。
目前WordPress的官方版本默认支持[footag][bartag][baztag]这几个“短码”。
| -欢迎为本文评级 |
相关日志 |
本文读者也关心以下内容:
|















































Leave a reply