Return (and call) shortcode from php function
-
Hi there,
on my website I want to call different shortcodes depending on the value of some variables.Here is a simple version of my problem:
1) user fills out a form which will result in some variables that are attached to a URL, e.g. URL?var=18
2) a php function embedded in a shortcode ([simple]) reads out the variable ‘var’
3) depending on the value of ‘var’ the simple-shortcode should return a new shortcode that should be called with different arguments -> either [shortcode]foo[/shortcode] or [shortcode]bar[/shortcode]Here is a minimal version of my simple-shortcode (php function) reading out the URL and returning the new shortcode:
//[simple] function simple_shortcode() { $var = $_GET['var']; if ($var > 12){ return '[shortcode]foo[/shortcode]'; } else { return '[shortcode]bar[/shortcode]'; } } add_shortcode('simple', 'simple_shortcode');
This doesn’t result in the shortcode being called with either ‘foo’ or ‘bar’ as an argument but just in a return of ‘[shortcode]xxx[/shortcode]’ as a string.
—-
Adjusting [simple] to just return either ‘foo’ or ‘bar’ and using
[shortcode][simple][/shortcode]
on the website doesn’t work either.—-
I hope I made clear what I want to achieve.
Are there any ideas how to do it (differently)?
(I’m quite new at php and it’s possible that the above code isn’t the prettiest version possible but it works in principle.)
Thanks,
Patrickedit: the shortcode [shortcode]xxx[/shortcode] I want to call is from a plugin and not a shortcode of myself.
- The topic ‘Return (and call) shortcode from php function’ is closed to new replies.