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: Output of filter on the_content:This Gymnopus dryophilus, itself saprotrophic rather than [wiki]mycorrhizal[/wiki], is infected with Syzygospora mycetophila. [wiki]mycorrhizal[/wiki], mycorrhiza,’mycorrhizae’,’mycorrhizas’, [wiki]Mycorrhizal[/wiki], Mycorrhiza,Mycorrhizae, Mycorrhizas

* 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!

  • What does the [wiki] shortcode do? This is probably related to the ordering of hooks, you don't specify a priority when you add your hooks. You can also simplify your for loop with a foreach( $fungii as $fungus ) { rather than a generic sounding $arr and a for loop that assumes numbered keys – Tom J Nowell Jul 30 at 16:01
  • @TomJNowell, thanks for the reply. The [wiki] code is to be rendered as tooltips by a plugin (wordpress.org/plugins/wp-wiki-tooltip). Some of those links in my output work just fine (but you see the garble there, too). A question arising from my ignorance: Might specifying a priority resolve my garble issue? – Brendan Jul 30 at 16:06
  • It's not garbled, it's just that your shortcodes are being added after shortcodes are processed, not before. If you don't specify a priority, it assumes 10 – Tom J Nowell Jul 30 at 21:51
  • Thanks, @TomJNowell. Two things: One, on priority, I tried 0, 2, and 6 (which didn't change anything) and 20 (which was too late in processing to be operated on by the plugin). Two, I eliminated re-duh!-ndancies in the array, so the elements are just $arr = array('mycorrhiza','Mycorrhiza','Gymnopus dryophilus');. My array was accidentally creating extra tagging - both for 'Mycorrhizal' and for 'Micorrhiza' in the word 'Micorrhizal', too. All that's left in my original question is how to RegEx the content that's already [wiki]-tagged out of the match results. – Brendan Jul 30 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.