1

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.

1 Answer 1

1

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.

require_once( get_stylesheet_directory() . '/array.php' );

OR

$array = array('key' => 'value');

Then inside the function just write global $array; to access the variables declared outside the function.

function someCode($opts) {
    global $array;

    return $array[$opts[1]];

}
add_shortcode('myshortCode', 'someCode');

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.

$opts 

is an array of your unnamed attributes so you can choose $opts[0], $opts[1] and so on..

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for trying, but it's not working. All paths are correct and no tags (i.e. <?php) are incomplete. There is something in the syntax I'm not getting. Maybe I need to clarify. I'm going to be using the shortcode on several places on the same page where the attributes should decide what content to get (i.e. I can't be inserting [$opt[0]]). Might not have clarified my question better. I need to get different content from the array, based on the returned attribute in the shortcode (e.g. 'ashortcode key1' returns the value from key1 in the array. I believe my return has to change.
So if you will use [myshortCode "key1"] then it should display "a string respons..." and if you use [myshortCode "key2"] you will get "...from external API" return. (You will use $opts[0]). This functionality works perfectly fine on my side.
Sorry just checked and if the shortcode is used twice then it break. Let me check further.
OK, here is a solution: use require instead of require_once
The case is that if the shortcode is ran twice then when using require once the file will get fetched only once and second time it will not be accessed. Maybe it should be a better way to declare $array outside of the functions and then inside the function call global $array; and then use that array. This way you would call it once and then use it many times as a global variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.