Avatar of Blair Benjamin
Blair Benjamin
Flag for United States of America

asked on 

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!

Avatar of undefined
Last Comment
Blair Benjamin

8/22/2022 - Mon