记录一些常用的函数和瞎折腾的过程……

wordpress自定义短代码

提醒:本文最后更新于 2377 天前,文中所描述的信息可能已发生改变,请谨慎使用。

有时候为了方便输入,可以使用wordpress的短代码功能,实现自己一些想要的功能。我要用短代码转义一个下载选项,可以这样实现:

在后台内容页面中,我输入:

[download]http://www.test.com/download.rar[/download]

在前台显示的时候,要想显示为正常的html代码,必须经过短代码转义:

// 下载按钮
function button_download($atts, $content = null) {
    return '<div id="down"><a title="下载链接" href="'. $content.'" rel="nofollow"><i class="download"></i>下载地址</a></div>';
}
add_shortcode("download", "button_download");

为了更加方便,可以后台编辑器中注册一个自定义按钮:

<?php
add_action('after_wp_tiny_mce', 'my_bottom');
function my_bottom($mce_settings) {
    if (wp_script_is('quicktags')){
?>
    <script type="text/javascript">
        QTags.addButton( 'mydownload', '下载按钮', "[download]", "[/download]" );
    </script>
<?php
    }
}

这样就基本完成了,将自定义按钮和短代码充分的利用,使写作更加简便。我这里没有加CSS,为了美观,可以自行增加CSS哦。