It seems we can populate a stat bar’s custom text with a shortcode but shortcodes (that produce a single numerical value) don’t seem to work for the length. Is there a special way to format them or is it just not supported at all?
Hello @co50,
Thanks for writing in!
Please make sure that your shortcode returns something. The Content field of the Statbar should be able to render a shortcode. Take this [x_icon type="adjust"]
icon shortcode as an example:
Best Regards.
The label field isn’t the problem - it’s the length field. Our short code returns “30%” but it seems the length field doesn’t convert shortcodes? Is there a workaround?
Thank you for the info. The Bar > Setup > Length field doesn’t support shortcodes by default, but you can apply the value using dynamic content. Have you tried using an ACF field instead of a custom shortcode? Why do you need to supply the value using shortcode?
The plugin we have uses shortcodes to allow us to display values where we want. We don’t know how to pull that value using dynamic content codes, otherwise we’d do that. And we don’t use ACF on this website because we don’t have a use for it.
Looks like you can do this using a custom Twig template. Please add this in the functions.php file:
function x_return_thirty_shortcode() {
return "30%";
}
add_shortcode('x_return_thirty', 'x_return_thirty_shortcode');
add_filter('cs_twig_functions', function($functions) {
$functions['x_render_thirty'] = [
'callable' => function() {
return do_shortcode('[x_return_thirty]');
}
];
return $functions;
});
Click the dynamic content icon in the Length fielld, search for Twig > Functions, then select x_render_thirty.
We wrapped the shortcode inside the custom Twig function so that the Length field can properly read the value returned by the shortcode. You can replace this with the plugin shortcode.
Let us know if you need more info.
I’m not sure I follow any of this…
-
Why is 30% hardcoded into the function? If it’s hardcoding 30% I could just enter 30% into the length field and forgo the shortcode - what would be the difference?
-
Am I supposed to replace both instances of [x_return_thirty] in your function with the actual shortcode?
-
This hack only applies to a single shortcode? So if the page has 20 stat bars showing various stats, I would need to create 20 separate functions like this example for them to work?
Hello @co50,
The code above given by @Ismael is just an example. You may need to create your own shortcode that will accept attribute. I would recommend that you check out this codex guide you in creating your own shortcode.
By the way, may we know which shortcode you are using so that we can test that shortcode in our local testing server as well?
Thanks,
RueNel
Huh? I’m not looking for info on how to create shortcodes. We already have the shortcodes we want to use. The problem is we can’t use them for the stat bar length and I’m looking for a way to do so.
Could you provide examples of the shortcodes and the actual values they return? You could add parameters to the Twig function to return different shortcodes.
Our shortcodes used the format of [billdata field=“prog_cons_score” bill=""] where the bill field is populated by the query string in the URL (though we can also specify the bill for testing purposes). This shortcode returns the score, in this case 30%. Works fine for the custom text but length doesn’t work.
But we’ve resolved this issue by sending an image of a pro stat bar example to our AI and asking it to replicate it and allow us to use via shortcode. Took 30 seconds, so we’re all set.
Alright. Glad to know that you’ve come up with an alternative solution. Feel free to open another thread if you have more questions. Have a nice day.