Wie man Variablen von einer function zur anderen leitet oder functionen kombiniert

Ich habe zwei functionen:

  • 1. – kümmert sich um alle Optionen des Customizers
  • 2. – Erzeugt einen Inline-Stil aus diesen Optionen

In der ersten function setze ich Variablen, von denen ich Customizing-Einstellungen und Kontrollen wie folgt erstellen:

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' ); 

In der zweiten function muss ich dieselben Variablen eingeben, damit das Skript Inline-CSS generiert:

 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' ); 

Meine Frage ist: Gibt es eine Möglichkeit, $styles von der ersten function an die zweite zu übergeben oder gibt es keine, kann ich diese beiden functionen kombinieren. Ich habe versucht, sie zu kombinieren, aber ich konnte die Magie nicht passieren lassen.

Solutions Collecting From Web of "Wie man Variablen von einer function zur anderen leitet oder functionen kombiniert"

Es gibt zwei Möglichkeiten, die am einfachsten wären.

  1. Mach es mit einem functionsaufruf.

  2. Machen Sie die Variablen global.

Der erste wäre so gemacht:

 function my_func() { $styles = 'stuff'; second_func($styles); } function second_func($styles) { // do something with $styles var_dump($styles); } 

Der zweite Weg wäre so gemacht:

 // Set global variable $GLOBALS['styles'] = 'stuff'; // Call it anywhere else global $styles; 

Für mehr auf Globals: http://php.net/manual/en/language.variables.scope.php

EDIT: Siehe unten.

 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' );