Results 1 to 3 of 3

Thread: How Do You Make Keys From An Array() Global?

  1. #1
    Join Date
    Jan 2009
    Posts
    82
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation How Do You Make Keys From An Array() Global?

    I'm trying to access the variables in the array below outside of the function. I've tried everything

    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.

    PHP Code:
    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!
    Last edited by djr33; 12-29-2012 at 10:31 PM. Reason: update formatting

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    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?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    just so you know, "shortcodes" 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 Code:
    <?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']).

Similar Threads

  1. Replies: 0
    Last Post: 05-22-2012, 04:52 PM
  2. Validating that keys exist in an array
    By JasonDFR in forum PHP
    Replies: 4
    Last Post: 02-16-2009, 06:10 PM
  3. Keys?
    By Nile in forum JavaScript
    Replies: 7
    Last Post: 02-03-2008, 03:32 PM
  4. Replies: 5
    Last Post: 08-20-2007, 03:02 AM
  5. Can't make a sum of array
    By mgameulstee in forum JavaScript
    Replies: 6
    Last Post: 09-17-2006, 08:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •