There are 2 ways that would be the most simple.
-
Do it with a function call.
-
Make the variables global.
The first would be done like so:
function my_func() {
$styles="stuff";
second_func($styles);
}
function second_func($styles) {
// do something with $styles
var_dump($styles);
}
The second way would be done like so:
// Set global variable
$GLOBALS['styles'] = 'stuff';
// Call it anywhere else
global $styles;
For more on globals:
http://php.net/manual/en/language.variables.scope.php
EDIT: See below.
global $styles;
$styles = array(
array(
'slug'=>'body_bcolor',
'default' => "#F1F2F1",
'label' => __('Body Background Color', 'impressive'),
'css_class' => 'body',
'attribute' => 'background-color:',
'section' => 'body',
'type' => 'color'
),
array(
'slug'=>'max_container_width',
'default' => '1440',
'label' => __('Maximal Container Width', 'impressive'),
'css_class' => '.header',
'attribute' => 'max-width:',
'section' => 'body',
'type' => 'number'
)
);
function im_customize_register( $wp_customize ) {
global $styles;
//Custom Sections
$wp_customize->add_section(
'body',
array(
'title' => __('Body Settings', 'impressive'),
'priority' => 200
)
);
foreach( $GLOBALS['styles'] as $style ) {
$wp_customize->add_setting(
$style['slug'], array(
'default' => $style['default'],
'type' => 'option',
'capability' =>
'edit_theme_options'
)
);
if ( $style['type'] == 'color' ) {
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
$style['slug'],
array('label' => $style['label'],
'section' => $style['section'],
'settings' => $style['slug'],
'type' => $style['type'])
)
);
} elseif ( $style['type'] == 'image' && $style['section'] == 'header_image' ) {
$wp_customize->add_control(
new WP_Customize_Header_Image_Control(
$wp_customize,
$style['slug'],
array('section' => $style['section'],
'settings' => $style['slug'])
)
);
} else {
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
$style['slug'],
array('label' => $style['label'],
'section' => $style['section'],
'settings' => $style['slug'],
'type' => $style['type'])
)
);
}
}
}
add_action( 'customize_register', 'im_customize_register' );
function im_custom_style_create() {
global $styles;
wp_enqueue_style( 'im_custom_style', get_template_directory_uri() . '/css/im_custom_style.css' );
foreach( $styles as $style ) {
if ( $style['attribute'] == $max_width ) { $measurement_unit="px"; }
if ( get_option( $style['slug'] ) == true ) { $custom_css="" . $style['css_class'] . ' { ' . $style['attribute'] . ' ' . get_option( $style['slug'] ) . $measurement_unit . '; } '; };
wp_add_inline_style ('im_custom_style', $custom_css);
}
}
add_action( 'wp_enqueue_scripts', 'im_custom_style_create' );