thelia / short-code-module
2.0.0
2022-08-09 08:12 UTC
Requires
- maiorano84/shortcodes: v2.0.0-beta
- thelia/installer: ~1.1
README
在 Thelia 中实现 WordPress 短代码语法(使用 https://github.com/maiorano84/shortcodes)。
此模块在搜索已注册的短代码(在数据库中)时扫描 Thelia 响应,如果找到短代码,则模块将分派相关事件。
安装
手动安装
- 将模块复制到
<thelia_root>/local/modules/
目录,并确保模块的名称为 ShortCode。 - 在 Thelia 管理面板中激活它
Composer
将其添加到主 Thelia composer.json 文件中
composer require thelia/shortcode-module:~1.0
使用方法
此模块 https://github.com/thelia-modules/ShortCodeMeta 是如何使用 ShortCode 的一个好例子。
1. 注册您的短代码
您可以在模块激活后执行此操作。最好的方法是使用模块方法:ShortCode::createNewShortCodeIfNotExist
,第一个参数是您将在模板中使用的短代码名称(如 `[my_shortcode_name]`),第二个参数是检测到短代码时分派的事件。
2. 将短代码添加到您的模板中
短代码语法如下
[shortcode] - No content, no attributes
[shortcode]My Content[/shortcode] - Short code with content
[shortcode attribute=value foo=bar] - Short code with attributes
[shortcode attribute=value foo=bar]My Content[/shortcode] - Short code with content and attributes
3. 监听与您的短代码相关的事件
当检测到响应中的短代码时,将分派一个带有创建时给定的事件名称的 ShortCodeEvent
。因此,如果您想用其他内容替换短代码,您必须监听此事件。在此事件中,您有 3 个属性
content
(字符串)您的标签之间的内容,对于上面的示例,它将是My Content
。attributes
(数组)传递给您的短代码的所有属性的数组['attribute'=>'value', 'foo'=>'bar']
,对于上面的示例。result
(字符串)您的短代码将在响应中被替换为这个值(默认等于内容)。