插件开发文档

闭合短代码

💡 云策文档标注

概述

本文档介绍了 WordPress 中闭合短代码的使用场景、处理方式和注意事项,包括如何定义和处理包含内容的短代码,以及解析器的限制。

关键要点

  • 闭合短代码允许对包裹的内容进行操作,格式为 [$tag]content[/$tag],类似于 HTML 标签。
  • 处理闭合短代码时,回调函数接收 $atts 和 $content 参数,其中 $content 包含被包裹的内容,默认值为 null 以区分自闭合标签。
  • 短代码解析器仅对内容执行单次解析,因此嵌套短代码需要手动调用 do_shortcode() 来递归处理。
  • 解析器无法处理同一 [$tag] 的闭合和非闭合形式混合使用的情况,会导致解析错误。

代码示例

function wporg_shortcode( $atts = array(), $content = null ) {
    // do something to $content
    // run shortcode parser recursively
    $content = do_shortcode( $content );
    // always return
    return $content;
}
add_shortcode( 'wporg', 'wporg_shortcode' );

注意事项

  • 处理函数必须负责输出安全,并始终返回处理后的内容。
  • 使用 is_null() 函数可以检查 $content 是否为 null,以判断短代码是自闭合还是闭合形式。

📄 原文内容

The are two scenarios for using shortcodes:

  • The shortcode is a self-closing tag like we seen in the Basic Shortcodes section.
  • The shortcode is enclosing content.

Enclosing Content

Enclosing content with a shortcode allows manipulations on the enclosed content.

[wporg]content to manipulate[/wporg]

As seen above, all you need to do in order to enclose a section of content is add a beginning [$tag] and an end [/$tag], similar to HTML.

Processing Enclosed Content

Lets get back to our original [wporg] shortcode code:

function wporg_shortcode( $atts = array(), $content = null ) {
    // do something to $content
    // always return
    return $content;
}
add_shortcode( 'wporg', 'wporg_shortcode' );

Looking at the callback function we see that we chose to accept two parameters, $atts and $content. The $content parameter is going to hold our enclosed content. We will talk about $atts later.

The default value of $content is set to null so we can differentiate between a self-closing tag and enclosing tags by using PHP function is_null().

The shortcode [$tag], including its content and the end [/$tag] will be replaced with the return value of the handler function.

It is the responsibility of the handler function to secure the output.

Shortcode-ception

The shortcode parser performs a single pass on the content of the post.

This means that if the $content parameter of a shortcode handler contains another shortcode, it won’t be parsed. In this example, [shortcode] will not be processed:

[wporg]another [shortcode] is included[/wporg]

Using shortcodes inside other shortcodes is possible by calling do_shortcode() on the final return value of the handler function.

<pre class="wp-block-syntaxhighlighter-code">function wporg_shortcode( $atts = array(), $content = null ) {
	// do something to $content
	// run shortcode parser recursively
	$content = do_shortcode( $content );
	// always return
	return $content;
}
add_shortcode( 'wporg', 'wporg_shortcode' );</pre>

Limitations

The shortcode parser is unable to handle mixing of enclosing and non-enclosing forms of the same [$tag].

[wporg] non-enclosed content [wporg]enclosed content[/wporg]

Instead of being treated as two shortcodes separated by the text “non-enclosed content“, the parser treats this as a single shortcode enclosing “non-enclosed content [wporg]enclosed content“.