Log in

View Full Version : How Do You Make Keys From An Array() Global?



monaya
12-29-2012, 09:54 PM
I'm trying to access the variables in the array below outside of the function. I've tried everything :confused:

The variables I'm trying to access are bold and in red in the code below.

These variables are just not accessible outside the function.


function GenerateSitemap($params = array()) {
// default parameters
extract(shortcode_atts(array(
'title' => 'Site map',
'id' => 'sitemap',
'depth' => 2
), $params));
// create sitemap
$sitemap = wp_list_pages("title_li=&depth=$depth&sort_column=menu_order&echo=0");
if ($sitemap != '') {
$sitemap =
($title == '' ? '' : "<h2>$title</h2>") .
'<ul' . ($id == '' ? '' : " id=\"$id\"") . ">$sitemap</ul>";
}
return $sitemap;
}
add_shortcode('sitemap', 'GenerateSitemap');
function GenerateSitemap($params = array()) {
// default parameters
extract(shortcode_atts(array(
'title' => 'Site map',
'id' => 'sitemap',
'depth' => 2
), $params));
// create sitemap
$sitemap = wp_list_pages("title_li=&depth=$depth&sort_column=menu_order&echo=0");
if ($sitemap != '') {
$sitemap =
($title == '' ? '' : "<h2>$title</h2>") .
'<ul' . ($id == '' ? '' : " id=\"$id\"") . ">$sitemap</ul>";
}
return $sitemap;
}
add_shortcode('sitemap', 'GenerateSitemap');

Thanks!

djr33
12-29-2012, 11:33 PM
Hi monaya,

I edited your post to use [php] formatting, which is easier to read. But I left the old version there too so we can see the bold/red parts.


The code you posted is missing a lot (I think this is something in word press, right?), so it's hard to help.


I'm not sure exactly what you're trying to do (I don't see you accessing anything outside of the function, for example), but here are some general thoughts:
1. Array keys aren't ever global. The array itself would be global. You should be able to use the keyword global for that.
2. You must actually call that function to make that array exist. And each time it exists (each time it is run) it's a different array. That's why local scope is useful, not a bad thing. If you make it global, that might cause significant problems (although I don't really know).
3. It sounds to me like using Object Oriented Programming (classes, instead of functions) would be best here. But that's just a guess.


What exactly are you trying to do, and how are you trying to do it?

traq
12-30-2012, 01:56 AM
just so you know, "shortcodes (http://core.trac.wordpress.org/browser/tags/3.5/wp-includes/shortcodes.php#L0)" are not a real PHP thing. They're just something (of debatable merit) that WP invented. If you already understood that, I apologize; I mention it because I see a lot of confusion on this topic. There are many WP users who don't even realize that WordPress and PHP are *different things*, and it really complicates the process of helping them fix problems.

(On that note, you may be able to get better help on a WordPress forum, where there are more people who use these functions all the time.)

From reading through the shortcode functions, I don't think there's a "good way" to do what you want. You can hack it using the $_GLOBAL superglobal array, but it will confuse things and can cause unexpected problems (such as existing variables being overwritten (or not!) at various points in your script, not to mention no convenient way of knowing if the variables you wanted have been created/updated/ornot at any particular time).

Keeping in mind everything Daniel said...

I would not recommend trying this without better understanding how WP works in general.
I've tried to minimize the risk, but you still might break something important.

<?php

function GenerateSitemap($params = array()) {
// default parameters
extract(shortcode_atts(array(
'title' => 'Site map',
'id' => 'sitemap',
'depth' => 2
), $params));
// create sitemap
$sitemap = wp_list_pages("title_li=&depth=$depth&sort_column=menu_order&echo=0");
if ($sitemap != '') {
$sitemap =
($title == '' ? '' : "<h2>$title</h2>") .
'<ul' . ($id == '' ? '' : " id=\"$id\"") . ">$sitemap</ul>";
}

## HERE's THE NEW PART ##

$_GLOBALS['vars_from_GenerateSitemap'] = array( 'title'=>$title,'id'=>$id,'depth'=>$depth );

## all done ##

return $sitemap;
}


Later, after you've called this function (and assuming it ran successfully), you'd be able to access those values in the array $vars_from_GenerateSitemap (for example, $vars_from_GenerateSitemap['title']).