I am trying to create a function with two variables. One variable is the text I want to hover over, the other variable is the text I want displayed when I hover over the first text mentioned. I’m using the following shortcode from Shortcodes Ultimate to create this function:
[su_tooltip style="yellow" position="top" shadow="no" rounded="no" size="default" title="" content="Tooltip text" behavior="hover" close="no" class=""]Hover me to open tooltip[/su_tooltip]
I have written the following function in functioins.php:
function hov($x, $y) { echo "[su_tooltip style= "yellow" position="top" shadow="no" rounded="no" size="default" title="" content =strval($y) behavior="hover" close="no" class=""]$x'[/su_tooltip)]'"; }
I added the backslash so the code ignores the quotes. I saved this function. I tried running this function in a text editor page as follows:
hov(arg1,arg2)
But it just published “hov(arg1,arg2)” as a literal string. Any ideas what I am doing wrong? Thanks in advance.
Advertisement
Answer
If you’re trying to type hov('foo', 'bar');
into your HTML
then it will output that as text.
To call a PHP
function from inside your template you need the opening and closing PHP
tags.
So in your templates, to call this function you would use the following code.
<?php hov('foo', 'bar'); ?>
If you’re trying to call for a shortcode
then you need to use echo do_shortcode(['...']);
<?php echo do_shortcode(hov('foo', 'bar')); ?>
Revised Answer
To run a PHP
function by calling it in the post editor you need to create a shortcode
Shortcodes are created by using the WordPress function add_shortcode
See: https://developer.wordpress.org/plugins/shortcodes/
add_shortcode('hov', 'hov'); // create ['hov'] which calls hov() function hov($args = []) { return do_shortcode(sprintf('[su_tooltip style="yellow" position="top" shadow="no" rounded="no" size="default" title="" content="%s" behavior="hover" close="no" class=""]%s[/su_tooltip]', $args['y'], $args['x'])); }
Example usage
[hov y="foo" x="bar"]
Where x
and y
are passed as items inside of $args