WordPress : Comment passer des variables d’une fonction à une autre ou combiner des fonctions
J’ai deux fonctions :
- 1er – Prend en charge toutes les options de personnalisation
- 2nd – Génère un style en ligne à partir de ces options
Dans la première fonction, je définis des variables à partir desquelles je crée des paramètres de personnalisation et des contrôles comme ceci :
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' );
Dans la deuxième fonction, je dois taper les mêmes variables pour que le script génère du CSS en ligne :
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' );
Ma question est la suivante: existe-t-il un moyen de passer $styles
de la 1ère fonction à la 2ème ou s’il n’y en a pas, je peux combiner ces deux fonctions. J’ai essayé de les combiner, mais je n’ai pas pu faire en sorte que la magie opère.
Solution n°1 trouvée
Il y a 2 façons qui seraient les plus simples.
-
Faites-le avec un appel de fonction.
-
Rendre les variables globales.
Le premier se ferait ainsi :
function my_func() {
$styles = 'stuff';
second_func($styles);
}
function second_func($styles) {
// do something with $styles
var_dump($styles);
}
La deuxième façon serait faite comme ceci:
// Set global variable
$GLOBALS['styles'] = 'stuff';
// Call it anywhere else
global $styles;
Pour en savoir plus sur les variables globales : http://php.net/manual/en/language.variables.scope.php
EDIT : Voir ci-dessous.
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' );
0 commentaire