我有两个功能:
第一个-处理所有自定义程序选项第二个-从这些选项生成内联样式在第一个函数中,我设置变量,从中创建自定义程序设置和控件,如下所示:
function im_customize_register( $wp_customize ) {
//Custom Sections
$wp_customize->add_section(
\'body\',
array(
\'title\' => __(\'Body Settings\', \'impressive\'),
\'priority\' => 200
)
);
//Styles
$bg_color = \'background-color:\';
$max_width = \'max-width:\';
$styles = array();
$styles[] = array(
\'slug\'=>\'body_bcolor\',
\'default\' => "#F1F2F1",
\'label\' => __(\'Body Background Color\', \'impressive\'),
\'css_class\' => \'body\',
\'attribute\' => $bg_color,
\'section\' => \'body\',
\'type\' => \'color\'
);
$styles[] = array(
\'slug\'=>\'max_container_width\',
\'default\' => \'1440\',
\'label\' => __(\'Maximal Container Width\', \'impressive\'),
\'css_class\' => \'.header\',
\'attribute\' => $max_width,
\'section\' => \'body\',
\'type\' => \'number\'
);
foreach( $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\' );
在第二个函数中,我需要键入相同的变量,以便脚本生成内联CSS:
function im_custom_style_create() {
wp_enqueue_style( \'im_custom_style\', get_template_directory_uri() . \'/css/im_custom_style.css\' );
$custom_css = \'\';
$bg_color = \'background-color:\';
$max_width = \'max-width:\';
$measurement_unit = \'\';
$styles = array();
$styles[] = array(
\'slug\'=>\'body_bcolor\',
\'default\' => "#F1F2F1",
\'label\' => __(\'Body Background Color\', \'impressive\'),
\'css_class\' => \'body\',
\'attribute\' => $bg_color,
\'section\' => \'body\',
\'type\' => \'color\'
);
$styles[] = array(
\'slug\'=>\'max_container_width\',
\'default\' => \'1440\',
\'label\' => __(\'Maximal Container Width\', \'impressive\'),
\'css_class\' => \'.header\',
\'attribute\' => $max_width,
\'section\' => \'body\',
\'type\' => \'number\'
);
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\' );
我的问题是:有没有办法通过
$styles
从第一个函数到第二个函数,或者没有一个函数可以将这两个函数组合起来。我试着把它们结合起来,但我没能让魔法发生。
最合适的回答,由SO网友:Steve 整理而成
最简单的方法有两种。
通过函数调用来实现。
使变量成为全局变量。
第一步是这样做的:
function my_func() {
$styles = \'stuff\';
second_func($styles);
}
function second_func($styles) {
// do something with $styles
var_dump($styles);
}
第二种方法如下:
// Set global variable
$GLOBALS[\'styles\'] = \'stuff\';
// Call it anywhere else
global $styles;
有关Global的更多信息:
http://php.net/manual/en/language.variables.scope.php编辑:见下文。
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\' );