将包含 id 的 WordPress 简码替换为使用 id 的 html 标记
我正在 WordPress 中构建一个过滤器插件,并用一些 html 替换一些插件特定的标签。
示例:[VIDEO ID=12]
将在此函数中通过 preg_replaced 替换
function display_video($text){
$pattern = '/\[VIDEO ID\=\d+\]/';
$text=preg_replace($pattern,get_video_block($id),$text);
return $text;
}
我不确定如何确保提供正确的 ($id
) 参数我的 get_video_block()
函数用于每次替换发生。
除了 preg_replace()
函数内部之外,没有真正的循环发生,那么我将如何提供该值?
有关更多上下文,请使用 get_video_block()
函数:
function get_video_block($id){
global $wpdb;
$wpdb->show_errors();
$table_name = $wpdb->prefix . "video_manager";
$query = "SELECT * FROM " . $table_name . " WHERE `index` = '$id'";
$results = $wpdb->get_results($query, ARRAY_A);
$results = $results[0];
$returnString = '<div class="vidBlock">';
$returnString .= $results['embed_code'];
$returnString .= '<div class="voteBar">';
$returnString .= $results['vote_text'];
$returnString .= '<input type="button" value="YES" class="voteButton">';
$returnString .= '<input type="button" value="NO" class="voteButton">';
$returnString .= '</div>';
$returnString .= $results['title'] . '<br>';
$returnString .= $results['description'] . '<br>';
$returnString .= '</div>';
return $returnString;
}
I'm building a filter plugin in Wordpress and I'm replacing some plugin specific tags with bits of html.
Example: [VIDEO ID=12]
will be replaced via preg_replaced in this function
function display_video($text){
$pattern = '/\[VIDEO ID\=\d+\]/';
$text=preg_replace($pattern,get_video_block($id),$text);
return $text;
}
I'm not exactly sure how to make sure I supply the correct ($id
) param to my get_video_block()
function for each replacement occurrence.
There's no real loop going on other than inside the preg_replace()
function so, how would I supply that value?
For more context, get_video_block()
function:
function get_video_block($id){
global $wpdb;
$wpdb->show_errors();
$table_name = $wpdb->prefix . "video_manager";
$query = "SELECT * FROM " . $table_name . " WHERE `index` = '$id'";
$results = $wpdb->get_results($query, ARRAY_A);
$results = $results[0];
$returnString = '<div class="vidBlock">';
$returnString .= $results['embed_code'];
$returnString .= '<div class="voteBar">';
$returnString .= $results['vote_text'];
$returnString .= '<input type="button" value="YES" class="voteButton">';
$returnString .= '<input type="button" value="NO" class="voteButton">';
$returnString .= '</div>';
$returnString .= $results['title'] . '<br>';
$returnString .= $results['description'] . '<br>';
$returnString .= '</div>';
return $returnString;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
preg_replace_callback()
为此目的。您还需要将\d+
括在(
括号)
中,以便可以在回调函数中捕获并使用它。请注意,使用
$matches[1]
是因为$matches[0]
包含与正则表达式匹配的整个字符串。Erwin 的评论可能对您有用 - WordPress 有一个 shortcode API 可以为您管理短代码的解析,这样您就可以专注于处理您想要使用短代码属性执行的操作。
You can use
preg_replace_callback()
for that purpose. You'll also need to wrap\d+
in(
parentheses)
so it can be captured and used in the callback function.Note that
$matches[1]
is used because$matches[0]
contains the entire string matched by the regular expression.Erwin's comment may be of use to you — WordPress has a shortcode API that manages parsing of shortcodes for you, so you can concentrate on dealing with what you want to do with the shortcode attributes.
@BoltClock 的答案是正确的,但是 create_function() 现在有点过时了。
通过
preg_replace_callback()
内部的匿名函数将捕获的 id 传递给辅助函数。辅助函数返回的字符串将用于替换整个匹配的短代码。
由于没有为
preg_replace_callback()
声明任何限制,因此它可能会进行多次替换。代码:(Demo)
输出:
Ps 正如 @azureru 在评论中提到的,这似乎是一个很好的候选者实现 Wordpress 的短代码 api。 http://codex.wordpress.org/Shortcode_API
@BoltClock's answer is correct, but
create_function()
is a little old fashioned now.Pass the captured id to the helper function via an anonymous function inside of
preg_replace_callback()
.The helper function's returned string will be used to replace the entire matched shortcode.
Since no limit was declared for
preg_replace_callback()
, it will make potentially multiple replacements.Code: (Demo)
Output:
P.s. As @azureru mentioned in a comment, this does seem like a good candidate for implementing Wordpress's shortcode api. http://codex.wordpress.org/Shortcode_API