Can’t really get the hang of this. I’m trying to make a wordpress shortcode that gets values from an array in another file. What I’m trying to achieve is to make the shortcode ‘myshortCode valueFromKey’ work depending on the key.
This is my shortcode function in functions.php
function someCode($opts) {
require_once( get_stylesheet_directory() . '/assets/php/array.php' );
return $array[$opts[0]];
}add_shortcode('myshortCode', 'someCode');
And this is the array in array.php
$array = array(
'key1' => 'a string respons...',
'key2' => '...from external API'
);
But no matter what I do I can only get the first key value from the array. e.g.
‘myshortCode key1’
‘myshortCode key2’
only returns ‘myshortCode key1’
I plan on using this to display respons data from an external API. So all of these shortcodes will be in different sections on the same page.
I have just tested the code and it works fine on my side
[myshortCode “key1” “key2”] this is how I called the shortcode
and this is what I have included into my functions.php
Not to call the file many times just declare the array above the function or include the file once above the function.
OR
Then inside the function just write global $array; to access the variables declared outside the function.
Make sure you have
<?php
in the beginning of your array.php file and make sure path is correct. All the rest should be fine.is an array of your unnamed attributes so you can choose $opts[0], $opts[1] and so on..