Adding an array parameter to a Wordpress shortcode / PHP script

Blair Benjamin
Blair Benjamin used Ask the Experts™
on
Hello,  I have a custom shortcode that calls an API.  Works great, but I'd like to add a parameter to it in order to tweak the output a bit.  We already have one paramter, but the second one I'd like to add would be more along the lines of an array, so I need some tips.   The shortcode calls an API that pulls events from a calendar database.   Below are the lines of code related to this (not the full script of course):
function cal_events_shortcode($params = array()) 
{
extract(shortcode_atts(array("limit" => 5), $params));
$eventTypes = array (3,7);
...
}

Open in new window


This shortcode is called by inserting  [cal_events limit="4"] into a Wordpress page.   If no value is passed for "limit", it defaults to 5.  It then pulls events with the event types of 3 and 7, based on the hard-coded value for "eventTypes".

What I want to do is accept the values of "3, 7" as variables that are passed by the shortcode call and then inserted into the $eventTypes variable.    So, what would the "extract(shortcode_atts..." line of code look like if also accepting the values that need to go into $eventTypes array?

I imagine this is pretty simple, but I don't call myself a programmer.

Thanks for any tips!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
1. don't use extract - it looks like a nice function but it has some nasty side effects

2. Simply pass your values in a comma separated list and then explode them

Assume the following shortcode
[cal_events limit="4" list="1,2,3,4"]

Open in new window


function cal_events_limit_shortcode($atts)
{
  $parameters = shortcode_atts (
    array(
      'limit' => 0,
      'list' => ''
    ),
    $atts
  );
  $list_array = explode(',', $parameters['list']);
  // $list_array now is an array of [1,2,3,4]
}
add_shortcode('cal_events', 'cal_events_shortcode');

Open in new window

Blair BenjaminSr. Systems Administrator

Author

Commented:
Thanks.  I think we're on track with what I'm trying to work out here.   I think I just need to make a couple tweaks to get it working right on my end.   (Still working on it.)   However, shouldn't the last part of line 13 correspond to line 1 and be "cal_events_limit_shortcode" rather than just "cal_events_shortcode"?
Blair BenjaminSr. Systems Administrator

Author

Commented:
So far I haven't been able to make this work.  Any chance you could update this code to dump the contents of the variables on a page where the short code call is inserted?  For example, if I put [cal_events limit="4" list="1,2,3,4"] on a Wordpress page, it would render something like:
The value for limit is 4.
The values for list are 1, 2, 3 and 4.

If I have that much working, I should be able to tweak it for the primary function it will be a part of.

Thanks in advance!
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
I see the problem -
add_shortcode('cal_events', 'cal_events_shortcode');

Open in new window

Should be
add_shortcode('cal_events', 'cal_events_limit_shortcode');

Open in new window

This works for me
function cal_events_limit_shortcode($atts)
{
  $parameters = shortcode_atts (
    array(
      'limit' => 0,
      'list' => ''
    ),
    $atts
  );
  $list_array = explode(',', $parameters['list']);
  // $list_array now is an array of [1,2,3,4]
  echo "The value of limit is {$parameters['limit']}<br>";
  echo "<pre>" . print_r($list_array, true) . "</pre>";
}
add_shortcode('cal_events', 'cal_events_limit_shortcode');

Open in new window

Blair BenjaminSr. Systems Administrator

Author

Commented:
Thank you!  Exactly what I needed!
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome. Do you require further assistance - if so post back here, otherwise if you could close the question at your earliest convenience it would be appreciated.
Blair BenjaminSr. Systems Administrator

Author

Commented:
Thanks for the help.  It was just what I needed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial