0

I'm using a shortcode to render a count-up box on my page. To do so, I can add something like [ut_count_up to="1000"][/ut_count_up] in the editor for this particular page. That's all well as long as the number we're counting up to is static. Unfortunately, I need to make that number dynamic based on some visitor data. This "visitor data" is collected in a custom shortcode that returns the number I want to count up to. I can easily display this number on its own by adding [visitor-data] in the editor. The problem comes when I want to use that number as value of the "to" attribute of the ut_count_up shortcode. For example, I was expecting this to work:

[ut_count_up to='[visitor-data detail="population"]'][/ut_count_up]

It actually comes pretty close to working as expected in that it does successfully set the number returned by my custom shortcode as the value for the "to" attribute in the ut_count_up shortcode... and the page displays a box with a number that counts up to that value. The problem here is that a stray '] is displayed on the page right after the count-up number. It looks like the closing bracket from my custom shortcode closed the parent shortcode and the trailing characters were left behind to be displayed literally on the page.

What is the correct way to use pass a value generated by a shortcode to another shortcode as a value for a given attribute?

  • You should not and can not use shortcodes as attributes. The correct way is to find a different way to do whatever you need. – Mark Kaplun May 1 '17 at 16:29
  • If you could, there's no way to access a parents shortcode, shortcodes are self contained – Tom J Nowell May 1 '17 at 16:46
  • Maybe you could make use of an <ol> instead? – WebElaine May 1 '17 at 18:04
1

I decided to modify the custom shortcode to make it a wrapper for the parent shortcode. Instead of returning a value, it now returns a call (using do_shortcode()) to the parent with its "to" attribute dynamically set.


OLD VERSION

...
$val = get_some_value();
return $val;



NEW VERSION

...
$val = get_some_value();
return do_shortcode("[ut_count_up to=\"$val\"]");
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.