0

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

7
  • your above array argument should work. What do you need? can you explain a bit
    – WPTC-Troop
    Commented Jan 1, 2016 at 16:02
  • Yeah, I think there might be something funny going on, although I don't know what. I elaborated a bit more.
    – Rokit
    Commented Jan 1, 2016 at 16:08
  • It looks basically the same. There is absolutely no good reason I can think of why this would be an issue. Sorry
    – jgraup
    Commented Jan 1, 2016 at 16:12
  • @Rokit What you are looking for is not wordpress specific. These are ways to pass arguments in php function. You can prefer your own preference
    – WPTC-Troop
    Commented Jan 1, 2016 at 16:23
  • @WPTC-Troop I'm not so sure it's not WordPress specific. I use the same method of passing arguments on other pages. The only difference is this bit of code resides in Functions.php. Also, argument passing is only one issue. The other issue is my initial question, which does not work either. The argument passing issue is something I found later.
    – Rokit
    Commented Jan 1, 2016 at 16:25

2 Answers 2

0

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 );
1
  • To respond, I edited my question since I can't format a comment.
    – Rokit
    Commented Jan 1, 2016 at 16:07
0

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
}
0

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.