admin管理员组文章数量:1418038
I'm working on a function to wrap word-matches* in [wiki]-shortcode tags. I suspect the best method is to use add_filter() on the_content, and parse using RegEx in preg_replace_callback_array(), but that's beyond me.
Here's a sample of post content:
This <i>Gymnopus dryophilus</i>, itself saprotrophic rather than mycorrhizal, is infected with <i>Syzygospora mycetophila</i>. mycorrhizal, mycorrhiza,'mycorrhizae','mycorrhizas', Mycorrhizal, Mycorrhiza,Mycorrhizae, Mycorrhizas
Here's a sample array of terms to be matched:
$arr = array('mycorrhizal','mycorrhiza','mycorrhizae','mycorrhizas','Mycorrhizal','Mycorrhiza','Mycorrhizae','Mycorrhizas','Gymnopus dryophilus');
Here's the code I have for trying to do it:
function add_wikitips($content) {
if( is_single() && in_the_loop() && is_main_query() ) {
$arr = array('mycorrhizal','mycorrhiza','mycorrhizae','mycorrhizas','Mycorrhizal','Mycorrhiza','Mycorrhizae','Mycorrhizas','Gymnopus dryophilus');
for ($i = 0; $i < count($arr); $i++) {
$content = str_replace($arr[$i], '[wiki]'.$arr[$i].'[/wiki]', $content);
}
}
return $content;
}
add_filter('the_content', 'add_wikitips');
Here's the garbley output:
* I need RegEx matching to exclude match-instances where there are already manually added shortcodes, e.g., [wiki title="Mycorrhiza"]Mycorrhizality[/wiki]. If that counted as a match, the filtered content would be
[wiki title="Mycorrhiza"][wiki]Mycorrhiza[/wiki]lity[/wiki]
Which is no good (I think something like that might be happening to generate the garbled output above).
Thanks for any help!
本文标签: filtersHow to wrap patternmatches from thecontent in wikishortcode tags
版权声明:本文标题:filters - How to wrap pattern-matches from the_content in [wiki]-shortcode tags 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745283391a2651558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论