0

I have a function that is designed to do two things: 1) create color picker controls/ settings in the customizer and 2) create a multi-dimensional array $customizeData to be outputted in header styles and used to localize variables for customizer.js.

The first objective is successful. The second one has driven me here. I added a var_dump for $customizerData to index.php the array is null on the front end, but is populated as expected when I load it inside the customizer.

Here is the function in question:

if (! function_exists('_sf_customzier_color_loop') ) :
function _sf_customzier_color_loop($colors, $countStart = 10, $section) {
//Not sure why I have to do this first thing
global $wp_customize;
//declare $customizerData as a global variable
global $customizerData;
//start the counter at 10 or whatever was set.
$count = $countStart;
foreach ($colors as $things) {
    $slug = $things['slug'];
    $id = "_sf[{$slug}]";
    //If current array has a priority set, use it, if not use the counter.
    if (! isset($things['priority']) ) {
        $priority = $count;
    }
    else {
        $priority = $things['priority'];
    }
    $wp_customize->add_setting( $id, array(
        'type'              => 'option', 
        'transport'     => 'postMessage',
        'capability'     => 'edit_theme_options',
        'default'       => $things['default'],
    ) );

    $control = 
    new WP_Customize_Color_Control(
            $wp_customize, $slug, 
        array(
        'label'         => __( $things['label'], '_sf' ),
        'section'       => $section,
        'priority'      => $priority,
        'settings'      => $id
        ) 
    );
    $wp_customize->add_control($control);
    //create array to be used for the outputting styles to wp_head and customizer.js
    $customizerData [] = array(
        'id' => $id,
        'slug' => $slug, 
        'selector' => $things['selector'],
        'property' => $things['property'],
    );

    //advance priority counter
    $count++;
}
}
endif;
// ! _sf_customzier_color_loop exists 

You can see an example of what I am feeding to this at: https://github.com/Shelob9/_sf_lib/blob/1.1-fancy-data/customizer/customizer-sidebar.php

What do I need to do so that that the array is populated on the front end?

1 Answer 1

2

Couple points:

  • The customize_register action hook actually passes your function the $wp_customize variable, you don't need to declare it as the global. Just put it as the first argument in your function declaration.

  • The reason your code doesn't run on the front end of the site is that the customize_register action hook only runs when the customizer is being used. If you want to declare data somewhere that it will always be available, don't put it in a function hooked to customize_register.

3
  • Ah, makes sense. Sucks, because I'm trying to do three things with this array. The first of which (add controls/settings to customizers has to be hooked to customize_register. Right now I'm saving $customizerData as an option so I can get that in my css and js functions. That solution sucks as well, but its working for outputting the css in header, which is awesome.
    – JPollock
    Commented Jul 27, 2013 at 23:24
  • Why not make a function that returns this array of data, then call that function from both places where you need the data?
    – Otto
    Commented Jul 28, 2013 at 16:43
  • That makes sense of course. Thing is that function creates the settings/controls. Not sure how to do what your suggesting with out screwing that up. You can see the function in question at: github.com/Shelob9/simple_customizer_framework/blob/master/…
    – JPollock
    Commented Jul 29, 2013 at 0:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.