filters - How to wrap pattern-matches from the_content in [wiki]-shortcode tags

I'm working on a function to wrap word-matches* in [wiki]-shortcode tags. I suspect the best method is to use add_f

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745283391a4620413.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信