apply_shortcodes()
云策文档标注
概述
apply_shortcodes() 是 WordPress 5.4 引入的函数,用于在内容中搜索并处理短代码。它是 do_shortcode() 的别名,提供相同的功能。
关键要点
- apply_shortcodes() 是 do_shortcode() 的别名,两者功能完全一致。
- 函数接受两个参数:$content(必需,要搜索短代码的内容)和 $ignore_html(可选,默认为 false,当为 true 时跳过 HTML 元素内的短代码)。
- 返回处理后的内容字符串,短代码已被替换为实际输出。
- 从 WordPress 5.4.0 版本开始引入。
代码示例
// 旧方法使用 do_shortcode()
echo do_shortcode( '[myshrtcode]My Text[/myshrtcode]' );
// WordPress 5.4 引入的新方法使用 apply_shortcodes()
echo apply_shortcodes( '[myshrtcode]My Text[/myshrtcode]' );
// 显示短代码的处理结果
原文内容
Searches content for shortcodes and filter shortcodes through their hooks.
Description
This function is an alias for do_shortcode() .
See also
Parameters
$contentstringrequired-
Content to search for shortcodes.
$ignore_htmlbooloptional-
When true, shortcodes inside HTML elements will be skipped.
Default:
false
Source
function apply_shortcodes( $content, $ignore_html = false ) {
return do_shortcode( $content, $ignore_html );
}
Changelog
| Version | Description |
|---|---|
| 5.4.0 | Introduced. |
Skip to note 3 content
MakeWebBetter
Here is the implementation by current method –
echo do_shortcode( '[myshrtcode]My Text[/myshrtcode]' );And here is the usage which new WordPress 5.4 Introduces
echo apply_shortcodes( '[myshrtcode]My Text[/myshrtcode]' ); // Displays the result of the shortcodeSkip to note 4 content
Ataur R
Old method –
echo do_shortcode( '[wpdocs_my_shrtcode text="My Text"]', false ); echo do_shortcode( '[wpdocs_my_shrtcode]My Text[/wpdocs_my_shrtcode]', false );WordPress 5.4 Introduces apply_shortcodes()
echo apply_shortcodes( '[wpdocs_my_shrtcode text="My Text"]', false ); echo apply_shortcodes( '[wpdocs_my_shrtcode]My Text[/wpdocs_my_shrtcode]', false );// Displays the result of the shortcode