• Resolved doclalor

    (@doclalor)


    I would like to automatically generate tooltip shortcode for an array of words, using 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 what I have so far.

    A sample of post content (the text visible in the admin-side post editor):

    This [wiki base=”WI”]Gymnopus dryophilus[/wiki], itself saprotrophic rather than mycorrhizal, is infected with <i>Syzygospora mycetophila</i>. mycorrhizal, [wiki base=”WO”]mycorrhiza[/wiki],’mycorrhizae’,’mycorrhizas’, Mycorrhizal, Mycorrhiza,Mycorrhizae, Mycorrhizas,

    [wiki base=”WO”]-trophic[/wiki]

    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');

    But the output’s garbled, perhaps due to an unwanted (recursive) double-application of the filters to the_content?:

    https://pasteboard.co/Iqp0oHY.png

    * 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 come out as

    [wiki title=”Mycorrhiza”][wiki]Mycorrhiza[/wiki]lity[/wiki]

    – which would be rendered in an undesirable, garbled way by the WP Wiki Tooltip plugin. (I think something like that might be happening to generate the garbled output above.)

    Thanks for any help!

    • This topic was modified 5 years, 3 months ago by doclalor.
    • This topic was modified 5 years, 3 months ago by doclalor.
    • This topic was modified 5 years, 3 months ago by doclalor.
    • This topic was modified 5 years, 3 months ago by doclalor. Reason: Trying to get the image to show
    • This topic was modified 5 years, 3 months ago by doclalor.
    • This topic was modified 5 years, 3 months ago by doclalor.
    • This topic was modified 5 years, 3 months ago by doclalor.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Nico Danneberg

    (@nida78)

    Just with a quick review I would say that you solution is quiet nice BUT your function is called late after all shortcodes are rendered.

    So, just call do_shortcode() function right before returning the content and it should work.

    Looking forward to your answer!

    Thread Starter doclalor

    (@doclalor)

    Once I removed my redundant matches (duh: ‘mycorrhiza’ matches inside ‘mycorrhizae’,’mycorrhizas’, not to mention with ‘mycorrhiza’ itself!) and added your call, it works!

    Thanks for your help!

    Plugin Author Nico Danneberg

    (@nida78)

    Nice that it works for you!

    I will take this as a new feature request. I think it could be useful for more users ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Automate tooltips via functions.php?’ is closed to new replies.