在页面内容中呈现 wordpress 简码
Render wordpress shortcode within page content
我写了一个wordpress插件。它是一组短代码,可从 API 中获取、解析和呈现数据。
我现在正在写一个主题来支持这个插件。我注意到 Wordpress 将短代码内容与编辑器内容分开并单独呈现它们。
这是我的问题的说明:
在管理面板中编辑页面或 Post:
<div class="row">
<div class="col-lg-12">
<p>HERE</p>
[PluginShortcode_1]
</div>
</div>
假设 PluginShortcode_1 生成以下 html:
<h1>This is the output of PluginShortcode_1</h1>
<p>Got Here!</p>
我希望输出为:
<div class="row">
<div class="col-lg-12">
<p>HERE</p>
<h1>This is the output of PluginShortcode_1</h1>
<p>Got Here!</p>
</div>
</div>
而是将以下内容发送到浏览器:
<h1>This is the output of PluginShortcode_1</h1>
<p>Got Here!</p>
<div class="row">
<div class="col-lg-12">
<p>HERE</p>
</div>
</div>
显然 wordpress 执行以下操作:
- 解析并呈现简码内容
- 渲染post内容
我看到了对 do_shortcode() 的引用,但我的插件定义了许多短代码,并且不会在 template.php 中明确知道页面上有哪些短代码,而无需事先解析内容,选择所有短代码和应用过滤器。
更新:
短代码函数在一组处理所有渲染的 classes 中。大多数短代码内容单独存储在呈现内容的 "view" 脚本中。
以上示例将按以下方式调用:
shortcodes.ini - 简码列表及其相关函数:
[shortcode_values]
PluginShortcode_1 = shortcodeOne
AnotherShortcode = anotherShortcode
Shortcodes.php - 短代码函数和调用的容器:
public function __construct() {
$ini_array = parse_ini_file(__DIR__. '/shortcodes.ini', true);
$this->codeLib = $ini_array['shortcode_values'];
foreach ($this->codeLib as $codeTag => $codeMethod) {
add_shortcode($codeTag, array(&$this, $codeMethod));
}
}
public function shortcodeOne() {
require_once(__DIR__ . 'views/shortcodeOneView.php');
}
views/shortcodeOneView.php
<?php
?>
<h1>This is the output of PluginShortcode_1</h1>
<p>Got here!</p>
简码函数实际上负责获取数据,设置将暴露给视图的变量。
更新
更复杂的是,由于此插件发出 API 请求,我将其限制为仅在 post 内容实际包含短代码时才被调用。
在插件初始化脚本中,我有以下内容:
class myPlugin.php:
public function __construct() {
. . .
add_filter('the_posts', array(&$this, 'isShortcodeRequest'));
. . .
}
public function isShortcodeRequest($posts) {
if (empty($posts)) { return $posts; }
foreach ($posts as $post) {
if (stripos($post->post_content, '[PluginShortcode') !== false) {
$shortcodes = new Shortcodes();
break;
}
}
return $posts;
}
我担心的是这个过滤器可能负责劫持输出。 . .(?)
问题是短代码回调,你是"echo"而不是return,你应该用这个替换shortcodeOne()
:
public function shortcodeOne() {
return "<h1>This is the output of PluginShortcode_1</h1><p>Got here!</p>";
}
WordPress 在打印它之前解析它的输出,在你的情况下,当它解析 PluginShortcode_1 以获得它的内容时,你打印它并且 WordPress 在return.
嗯。在 wp-includes 函数 do_shortocode() 第 199 - 200 行:
$pattern = get_shortcode_regex();
return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
preg_replace 正在对字符串进行操作。该函数被调用。所述函数的输出用内容进行插值。
所以,输出缓冲来拯救!
public function shortcodeOne() {
ob_start();
require_once(__DIR__ . 'views/shortcodeOneView.php');
$buffer = ob_get_clean();
return $buffer;
}
产生预期的输出!呼。 (顺便说一句,我所有的短代码函数都调用了一个需要该文件的渲染方法。让我免于进行大量更新。谢谢@all!
}
我写了一个wordpress插件。它是一组短代码,可从 API 中获取、解析和呈现数据。 我现在正在写一个主题来支持这个插件。我注意到 Wordpress 将短代码内容与编辑器内容分开并单独呈现它们。 这是我的问题的说明: 在管理面板中编辑页面或 Post:
<div class="row">
<div class="col-lg-12">
<p>HERE</p>
[PluginShortcode_1]
</div>
</div>
假设 PluginShortcode_1 生成以下 html:
<h1>This is the output of PluginShortcode_1</h1>
<p>Got Here!</p>
我希望输出为:
<div class="row">
<div class="col-lg-12">
<p>HERE</p>
<h1>This is the output of PluginShortcode_1</h1>
<p>Got Here!</p>
</div>
</div>
而是将以下内容发送到浏览器:
<h1>This is the output of PluginShortcode_1</h1>
<p>Got Here!</p>
<div class="row">
<div class="col-lg-12">
<p>HERE</p>
</div>
</div>
显然 wordpress 执行以下操作:
- 解析并呈现简码内容
- 渲染post内容
我看到了对 do_shortcode() 的引用,但我的插件定义了许多短代码,并且不会在 template.php 中明确知道页面上有哪些短代码,而无需事先解析内容,选择所有短代码和应用过滤器。
更新:
短代码函数在一组处理所有渲染的 classes 中。大多数短代码内容单独存储在呈现内容的 "view" 脚本中。
以上示例将按以下方式调用:
shortcodes.ini - 简码列表及其相关函数:
[shortcode_values]
PluginShortcode_1 = shortcodeOne
AnotherShortcode = anotherShortcode
Shortcodes.php - 短代码函数和调用的容器:
public function __construct() {
$ini_array = parse_ini_file(__DIR__. '/shortcodes.ini', true);
$this->codeLib = $ini_array['shortcode_values'];
foreach ($this->codeLib as $codeTag => $codeMethod) {
add_shortcode($codeTag, array(&$this, $codeMethod));
}
}
public function shortcodeOne() {
require_once(__DIR__ . 'views/shortcodeOneView.php');
}
views/shortcodeOneView.php
<?php
?>
<h1>This is the output of PluginShortcode_1</h1>
<p>Got here!</p>
简码函数实际上负责获取数据,设置将暴露给视图的变量。
更新 更复杂的是,由于此插件发出 API 请求,我将其限制为仅在 post 内容实际包含短代码时才被调用。
在插件初始化脚本中,我有以下内容:
class myPlugin.php:
public function __construct() {
. . .
add_filter('the_posts', array(&$this, 'isShortcodeRequest'));
. . .
}
public function isShortcodeRequest($posts) {
if (empty($posts)) { return $posts; }
foreach ($posts as $post) {
if (stripos($post->post_content, '[PluginShortcode') !== false) {
$shortcodes = new Shortcodes();
break;
}
}
return $posts;
}
我担心的是这个过滤器可能负责劫持输出。 . .(?)
问题是短代码回调,你是"echo"而不是return,你应该用这个替换shortcodeOne()
:
public function shortcodeOne() {
return "<h1>This is the output of PluginShortcode_1</h1><p>Got here!</p>";
}
WordPress 在打印它之前解析它的输出,在你的情况下,当它解析 PluginShortcode_1 以获得它的内容时,你打印它并且 WordPress 在return.
嗯。在 wp-includes 函数 do_shortocode() 第 199 - 200 行:
$pattern = get_shortcode_regex();
return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
preg_replace 正在对字符串进行操作。该函数被调用。所述函数的输出用内容进行插值。
所以,输出缓冲来拯救!
public function shortcodeOne() {
ob_start();
require_once(__DIR__ . 'views/shortcodeOneView.php');
$buffer = ob_get_clean();
return $buffer;
}
产生预期的输出!呼。 (顺便说一句,我所有的短代码函数都调用了一个需要该文件的渲染方法。让我免于进行大量更新。谢谢@all! }