Search code examples
phparraysjsonwordpresswordpress-gutenberg

Organizing arrays within arrays


I'm having trouble finding a clue to this.

I have a function that takes a list of JSON data and forms it into a php multidimensional array. I'm trying to plug this array into an add_theme_support function for Wordpress to add some color options.

For some reason, the arrays I'm creating won't be accepted into the add_theme_support function.

$json_data = file_get_contents();  // url here
$decodey = json_decode($json_data, true);
$new_palette = array(); 
$palette_colors = array_values($new_palette);

 //loop through JSON to get php arrays
for ($i = 0; $i < sizeof($decodey['list']['colors'][0]['colors']); $i++){

 $colorName = $decodey['list']['colors'][0]['colors'][$i]['name'];
 $colorName = trim($colorName);
 $pattern = '/!/';
 $replacement = '';

 $colorName = preg_replace($pattern, $replacement, $colorName);
 //echo $colorName;

 $colorSlug = str_replace(' ', '-', strtolower($colorName));
//echo $colorSlug;

$colorCode = $decodey['list']['colors'][0]['colors'][$i]['value'];
//echo $colorCode;

//create new array for each color
$newColorItem = array( 'name' => $colorName,
  'slug' => $colorSlug,
  'color' => $colorCode,
);
//push color arrays to empty array
$new_palette[] = array( 'name' =>  $colorName,
'slug' => $colorSlug,
'color' => $colorCode,
);
} //end loop

function addNewColors(){
add_theme_support('editor-color-palette', 
    array( 
        //this doesn't work
        $new_palette
    )
 );
}
add_action( 'after_setup_theme', 'addNewColors');

Solution

  • The problem came from the addNewColors() function. It wasn't needed and the options in Gutenberg appear fine now. Pushing to a variable was un-needed.

    add_theme_support('editor-color-palette', $new_palette);