• Resolved dedepress

    (@dedepress)


    I’m trying to do something with $_GET vars on customize.php page, but It is quite difficult than I expected.

    //======== Example

    WP Version: 4.0-beta4
    Also tested with 3.9.2, so I think it’s not a wp beta version bug.

    URL:

    https://localhost/wp/wp-admin/customize.php?var=google

    Code:

    add_action('customize_register', 'testing');
    
    function testing($wp_customize) {
    
        // this can't get vars and throw a undefined index error,
        // but if I check source code of the page, I can see it's here.
        print_r($_GET[var]); 
    
        // Notice: the stuff below inside will render properly,
        // in other words, I got the var once,
        // but it cause another fatal problem:
        // the setting which added inside here will not be saved.
        if(!empty($_GET[var]) {
            $wp_customize->add_setting(...);
            $wp_customize->add_control(...);
        }
    
    }

    if I hook the testing() function to ‘customize_controls_init’, I’m able to get $_GET vars and no errors, but it also cause the same problem: the settings will not be saved.

    seems it’s caused by iframe and refresh, but I tried a variety of ways and still can’t make it working well.

    Anyone have an point? Thanks in advance.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Dion

    (@diondesigns)

    The query term must be quoted in $_GET, such as $_GET['var'].

    You may also have a problem since var is a reserved word in PHP, but that may not apply to superglobals. If you still have a problem after fixing the quotes, then try a different query term.

    Thread Starter dedepress

    (@dedepress)

    Hey, Dion, Thanks for your reply. just example, my real code no quote error.

    What is the value that gets printed out for $_GET ['var']?

    Just so you’re aware, empty() checks a whole lot of things depending on the type of data that’s passed in, and if any one of them is true it returns to say yes it’s empty. All of ”, 0, false, array() are all considered empty and will return true from that function.

    Thread Starter dedepress

    (@dedepress)

    Hey, gus, I’m a WordPress developer, not a php beginner.

    I‘m talking about get $_GET vars inside ‘customize_register‘ hook in /wp-admin/customize.php page.

    In fact, I tried to get $_GET vars via so many hooks on this page, but no surprises.

    For example:

    add_action(‘wp_loaded’, ‘testing’);
    add_action(‘customize_register’, ‘testing’);
    add_action(‘after_setup_theme’, ‘testing’);

    function testing(){
    print_r($_GET);
    }

    if you check source code of customize.php, we got $_GET vars. but if you view the page in browser, the $_GET args is an empty array().

    in your original function that you posted here you have…

    function testing($wp_customize) {
        print_r($_GET[var]); 
    
        if(!empty($_GET[var]) {
            $wp_customize->add_setting(...);
            $wp_customize->add_control(...);
        }
    
    }

    What is given by the print_r there? You said that it’s an empty array, so that’s why it’s failing.

    I have to say that I’m not sure of how you’d get the values when you’re checking the source code??? I’m sure that I’m missing something in the translation, but as far as I am aware running it in the browser is what you’d be doing to check, not just looking at the codebase??

    There’s nothing inside that function that would cause any of the $_GET vars to dissappear for any reason, so if they aren’t showing up there I’d start working my way backwards from that function outwards until I find where the values are being removed or changed.

    Thread Starter dedepress

    (@dedepress)

    Thanks, I figured it out.

    Can I ask what the problem was? Mainly so anyone else looking for the same thing can find an answer.

    I got the same problem I came to this thread yet couldn’t find a solutions. However on digging further I found that we need to add to customizer url get string
    url=your_wp_site_url.com/?var=value&var=value

    For instance
    your_wp_site_url.com/wp-admin/customize.php?url=your_wp_site_url.com/?var=value&var=value&return=%2F7%2Fwp-admin%2Fthemes.php%3Fpage%3Dcover-pages
    Where var and value correspond $_GET var you wanna use and your_wp_site_url.com/ is url to your site

    Hope this helps somebody ; )

    Cheers

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to get $_GET var on customize.php?’ is closed to new replies.