插件开发文档

带参数的短代码

💡 云策文档标注

概述

本文档介绍了如何在 WordPress 短代码中使用参数(属性),包括短代码标签的语法和处理器函数的参数定义。重点讲解了如何解析和标准化属性,以确保短代码的稳定性和安全性。

关键要点

  • 短代码标签可以接受参数(属性),例如 [wporg title="WordPress.org"]。
  • 短代码处理器函数接受三个参数:$atts(属性数组)、$content(短代码内容字符串)和 $tag(短代码标签名)。
  • 使用 array_change_key_case() 标准化属性键的大小写,确保一致性。
  • 通过 shortcode_atts() 函数解析属性,结合默认值和用户提供的属性。
  • 在输出前使用 esc_html() 等函数确保安全性,防止 XSS 攻击。
  • 完整示例展示了如何创建一个带标题的 [wporg] 短代码,支持自闭合和包围形式,并安全输出内容。

代码示例

function wporg_shortcode( $atts = [], $content = null, $tag = '' ) {
    $atts = array_change_key_case( (array) $atts, CASE_LOWER );
    $wporg_atts = shortcode_atts(
        array(
            'title' => 'WordPress.org',
        ), $atts, $tag
    );
    $o = '<div class="wporg-box">';
    $o .= '<h2>' . esc_html( $wporg_atts['title'] ) . '</h2>';
    if ( ! is_null( $content ) ) {
        $o .= apply_filters( 'the_content', $content );
    }
    $o .= '</div>';
    return $o;
}
function wporg_shortcodes_init() {
    add_shortcode( 'wporg', 'wporg_shortcode' );
}
add_action( 'init', 'wporg_shortcodes_init' );

注意事项

  • 用户可能提供任意属性,开发者无法强制属性使用策略,因此需通过默认值和解析来处理。
  • 短代码内容需根据支持情况安全处理,例如使用 apply_filters( 'the_content', $content ) 来应用标准内容过滤器。

📄 原文内容

Now that we know how to create a basic shortcode and how to use it as self-closing and enclosing, we will look at using parameters in shortcode [$tag] and handler function.

Shortcode [$tag] can accept parameters, known as attributes:

[wporg title="WordPress.org"]
Having fun with WordPress.org shortcodes.
[/wporg]

Shortcode handler function can accept 3 parameters:

  • $atts – array – [$tag] attributes
  • $content – string – The content inside your shortcode. In the example above, it will be “Having fun with WordPress.org shortcodes.”
  • $tag – string – the name of the [$tag] (i.e. the name of the shortcode)
function wporg_shortcode( $atts = array(), $content = null, $tag = '' ) {}

Parsing Attributes

For the user, shortcodes are just strings with square brackets inside the post content. The user have no idea which attributes are available and what happens behind the scenes.

For plugin developers, there is no way to enforce a policy on the use of attributes. The user may include one attribute, two or none at all.

To gain control of how the shortcodes are used:

Complete Example

Complete example using a basic shortcode structure, taking care of self-closing and enclosing scenarios and securing output.

A [wporg] shortcode that will accept a title and will display a box that we can style with CSS.

/**
 * The [wporg] shortcode.
 *
 * Accepts a title and will display a box.
 *
 * @param array  $atts    Shortcode attributes. Default empty.
 * @param string $content Shortcode content. Default null.
 * @param string $tag     Shortcode tag (name). Default empty.
 * @return string Shortcode output.
 */
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(
		array(
			'title' => 'WordPress.org',
		), $atts, $tag
	);

	// start box
	$o = '<div class="wporg-box">';

	// title
	$o .= '<h2>' . esc_html( $wporg_atts['title'] ) . '</h2>';

	// enclosing tags
	if ( ! is_null( $content ) ) {
		// $content here holds everything in between the opening and the closing tags of your shortcode. eg.g [my-shortcode]content[/my-shortcode].
        // Depending on what your shortcode supports, you will parse and append the content to your output in different ways.
		// In this example, we just secure output by executing the_content filter hook on $content.
		$o .= apply_filters( 'the_content', $content );
	}

	// end box
	$o .= '</div>';

	// return output
	return $o;
}

/**
 * Central location to create all shortcodes.
 */
function wporg_shortcodes_init() {
	add_shortcode( 'wporg', 'wporg_shortcode' );
}

add_action( 'init', 'wporg_shortcodes_init' );