Adding an array parameter to a Wordpress shortcode / PHP script

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!
benjaminfamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

0
benjaminfamAuthor 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"?
0
benjaminfamAuthor 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!
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Julian HansenCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
benjaminfamAuthor Commented:
Thank you!  Exactly what I needed!
0
Julian HansenCommented:
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.
0
benjaminfamAuthor Commented:
Thanks for the help.  It was just what I needed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.