
类型:CMS系统
简介:一款开源的内容管理系统(CMS),用于构建和管理网站。
WordPress为保证核心代码的安全禁止任何人运行PHP代码,但从WordPress:2.5版本之后引入了“简码”这个概念,从此允许动态的添加内容。WordPress简码可以在文章中动态的添加内容代码,如创建相册、播放视频、表单插入等自定义操作。本文主要介绍在WordPress插件开发中简码的概念及用法。
一、WordPress简码的作用
简码是保持文章内容干净和语义化的方式之一,在WordPress中使用简码还是在不插入PHP代码的情况下把动态用户呈现给用户。简码的优势如下:
简码生成的标记和样式可以在随后很方便的修改和维护,不会添加HTML标记到文章内容中;
允许用户根据需要通过调整简码参数来修改简码内容的显示。
使用简码的几个建议:
- 总是返回!简码实际上是过滤器,所以创建 “副作用” 将导致意想不到的错误;
- 在简码前面添加自己的前缀,以避免与其他简码冲突;
- 消毒输入并转义输出;
- 向用户提供关于所有简码属性的明确说明文档。
二、WordPress内置简码
- caption:为图片或视频添加说明
- gallery:显示相册
- audio:嵌入和播放音频文件
- video:嵌入和播放视频文件
- playlist:显示音频或视频文件
- embed:显示嵌入式内容
三、WordPress简码开发从基础到进阶
开发自定义简码需借助WordPress的Shortcode API,核心是通过add_shortcode()函数注册简码,并绑定回调函数处理逻辑。下面分场景讲解具体实现方法。
1、添加基础简码
<?php add_shortcode( string $tag, callable $func );
在主题中注册简码
<?php
function wporg_shortcode($atts = [], $content = null) {
// do something to $content
// always return
return $content;
}
add_shortcode('wporg', 'wporg_shortcode');
在文章编辑器中输入[wporg]需要显示的内容[/wporg],前端会自动替换为回调函数返回的HTML。
在插件中注册简码:
<?php
function wporg_shortcodes_init() {
function wporg_shortcode($atts = [], $content = null) {
// do something to $content
// always return
return $content;
}
add_shortcode('wporg', 'wporg_shortcode');
}
add_action('init', 'wporg_shortcodes_init');
插件在WordPress加载流程中运行较早,若直接注册简码可能导致冲突。建议通过init钩子延迟注册,确保在WordPress初始化完成后执行:
2、简码的删除与检查
若某个简码不再需要,可使用remove_shortcode()函数删除:
<?php remove_shortcode( string $tag );
注意:删除操作需在简码注册之后执行,可通过设置钩子优先级确保执行顺序。
检查简码是否已注册
开发时若需判断某个简码是否存在(避免重复注册),可使用shortcode_exists()函数,返回布尔值(true存在,false不存在):
3、两种简码形式:自闭合与封闭
WordPress简码分为“自闭合”和“封闭”两种形式。
(1)自闭合简码
类似HTML 的<br>、<img>标签,无需结束标记,适用于无需包裹内容的场景。
(2)封闭简码
类似HTML的<div>、<p>标签,需成对使用,适用于需要处理包裹内容的场景。
使用封闭简码可以对简码包含的内容进行操作,
[wporg] content to manipulate [/wporg]
处理包含的内容
再看一下原来的 [wporg] 简码代码:
<?php
function wporg_shortcode($atts = [], $content = null) {
// do something to $content
// always return
return $content;
}
add_shortcode('wporg', 'wporg_shortcode');
4、简码嵌套的实现
WordPress默认的简码解析器仅对内容进行一次扫描,若封闭简码的$content中包含其他简码,嵌套的简码不会自动解析。需在回调函数中调用do_shortcode($content),强制递归解析嵌套的简码:
[wporg]another [shortcode] is included[/wporg]
<?php
function wporg_shortcode($atts = [], $content = null) {
// do something to $content
// run shortcode parser recursively
$content = do_shortcode($content);
// always return
return $content;
}
add_shortcode('wporg', 'wporg_shortcode');
限制:
简码解析器不能处理混合的闭合和自闭合简码,如下:
[wporg] non-enclosed content [wporg] enclosed content[/wporg]
其中non-enclosed content 解析器不把它当作由文本 “[wporg]” 分隔的两个短代码,而是把它当作一个包含“ non-enclosed content [wporg] enclosed content”的短代码。
5、带参数的简码
实际开发中,简码常需支持自定义参数需通过以下步骤处理参数:
简码的属性:
[wporg title="WordPress.org"] Having fun with WordPress.org shortcodes. [/wporg]
简码处理函数可以接受 3 个参数:
- $atts:数组 – [$tag] 的属性
- $content:字符串 – 简码包含的内容
- $tag:字符串 – [$tag] 的名称
function wporg_shortcode($atts = [], $content = null, $tag = '') {}
下面开发一个[wporg]简码,支持title参数,可包裹内容并生成带样式的文本框,同时兼容自闭合与封闭形式:
<?php
function wporg_shortcode($atts = [], $content = null, $tag = '') {
// normalize attribute keys, lowercase
$atts = array_change_key_case((array)$atts, CASE_LOWER);
// override default attributes with user attributes
$wporg_atts = shortcode_atts([
'title' => 'WordPress.org',
], $atts, $tag);
// start output
$o = '';
// start box
$o .= '<div class="wporg-box">';
// title
$o .= '<h2>' . esc_html__($wporg_atts['title'], 'wporg') . '</h2>';
// enclosing tags
if (!is_null($content)) {
// secure output by executing the_content filter hook on $content
$o .= apply_filters('the_content', $content);
// run shortcode parser recursively
$o .= do_shortcode($content);
}
// end box
$o .= '</div>';
// return output
return $o;
}
function wporg_shortcodes_init() {
add_shortcode('wporg', 'wporg_shortcode');
}
add_action('init', 'wporg_shortcodes_init');

