How to pass a variable for key/value pairs in an argument?

For example, this works:

'default' = '#ffffff'

But this does not:

$white = '#ffffff';
'default' = $white

How can I pass variables to key/value pairs?

Here is a more complete example for some context:

$white = '#ffffff';
$transport = 'refresh';

$wp_customize-add_setting('mytheme_text_color', array(
    'default' = $white,
    'transport' = $transport
));

In reply to jgraup:

This code works:

$wp_customize-add_setting('themeone_primary_nav_background_color', array(
    'default' = '#181818',
    'transport' = 'refresh'
));

This code doesn't:

$args = array('default' = '#181818', 'transport' = 'refresh');
$wp_customize-add_setting('themeone_primary_nav_background_color', $args);

When I customize my page colors in the admin section, I should see a "default" color button. In the former code, I see this button. In the latter code, I do not.

I have uploaded the file to my google drive if anyone wants to take a look: https://drive.google.com/open?id=0B01XHUEqiziEcW14WE5NN0VYYlE

And here is a picture of the problem with example output: https://drive.google.com/file/d/0B01XHUEqiziEUDVyZWFRWGZ0SkE/view?usp=sharing

Topic wp-parse-args variables Wordpress

Category Web


Found the problem. My variables were out of scope. I was declaring them at the top of the page, then trying to use them in a function. I solved it by using the global keyword within the function:

$dark_grey = '#181818';

function whatever() {
     global $dark_grey;  // now I can use it
}

Many functions use Arrays as arguments so your question is really how do you construct an Array?

If you have a variable as a key then it's better to set it using square bracket syntax.

// key pairs in array constructor

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

// key pairs outside of array constructor

$key = "This is my key";
$value = "This is my value";

$args [ $key ] = $value;

// pass your array now

do_action ( 'xyz', $args );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.