Skip Navigation

[Resolved] Return an array (or multiple values) with custom shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem:

I need to return a custom shortcode as an array of values. Specifically, I want to pass a set of 3 pairs of values, with each pair having a Ratings and a Review Count

Solution:

You can output the PHP array as json format:
https://www.php.net/manual/en/function.json-encode.php

And in front-end, setup JS code to turn Json data to JS array:
https://api.jquery.com/jQuery.parseJSON/

Relevant Documentation:

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by ericE-4 5 years ago.

Assisted by: Luo Yang.

Author
Posts
#1429169

I am familiar with creating a custom shortcode to process a value passed to it and return a different value. But I need to return an array of values. Specifically, I want to pass a set of 3 pairs of values, with each pair having a Ratings and a Review Count, as follows:

TripAdvisor Rating
TripAdvisor Review Count

Facebook Rating
Facebook Review Count

Google Rating
Google Review Count

The custom function will then find the highest of the three ratings (in the case of a tie, select the winner based on the order above) and return both the highest rating and the accompanying review count.

For example, if a passed the following values to the function:

TripAdvisor Rating: 4
TripAdvisor Review Count: 83

Facebook Rating: 5
Facebook Review Count: 12

Google Rating: 4.5
Google Review Count: 78

The function would return 5 and 12 (the Facebook rating is highest)

I could not find any documentation on this topic.

#1430981

Hello,

I assume we are talking about custom PHP codes, and you are WordPress function add_shortcode() to add custom shortcodes, see WordPress document:
https://developer.wordpress.org/reference/functions/do_shortcode/
Return #Return
(string) Content with shortcodes filtered out.
It will return result in text format, it can not return PHP array.

For example, you output the PHP array as json format:
hidden link

And in front-end, setup JS code to turn Json data to JS array:
hidden link

#1435027

My issue is resolved now. Thank you!