Automate tooltips via functions.php?
-
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 .
- This topic was modified 5 years, 3 months ago by .
- This topic was modified 5 years, 3 months ago by .
- This topic was modified 5 years, 3 months ago by . Reason: Trying to get the image to show
- This topic was modified 5 years, 3 months ago by .
- This topic was modified 5 years, 3 months ago by .
- This topic was modified 5 years, 3 months ago by .
- The topic ‘Automate tooltips via functions.php?’ is closed to new replies.