0

I have two functions:

  • 1st - Takes care of all customizer options
  • 2nd - Generates an inline style from those options

In first function I set variables from which I create customizer settings and controls like this:

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 second function I need to type same variables in order for the script to generate inline 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' );

My question is: Is there is a way to pass $styles from 1st function to 2nd or is there isn't one can I combine those two function. I tried combining them but I couldn't make the magic happen.

  • Just put both functions into the same class. That's what objects are for: combining state and behavior. – fuxia Oct 12 '16 at 16:39
  • @toscho It should be in a class, but it's not required to do what he's asking. What you have said isn't a solution. – Steve North Oct 12 '16 at 16:44
  • @SteveNorth It solves the problem in the most efficient and least fragile way. – fuxia Oct 12 '16 at 16:45
  • @toscho It solves the issue with bad practice, yeah. But, i'm not convinced he even needs to combine any functions. It looks to me like he's trying to do the same thing twice for 2 different reasons when i'm sure it could be combined into a single function. – Steve North Oct 12 '16 at 16:49
  • 1
    @SteveNorth Re-read what I have written here and in the duplicate. No one says, both functions should be "combined". They should act in the same scope, namely that of an object. you suggested to create a static dependency on elements from the global namespace. That is very wordpressy, but still bad practice. – fuxia Oct 12 '16 at 16:53
1

There are 2 ways that would be the most simple.

  1. Do it with a function call.

  2. 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' );
  • my_func() is supposed to be my first function right? – ERDFX Oct 12 '16 at 14:17
  • Yeah that's right. – Steve North Oct 12 '16 at 14:25
  • The two brackets at the end of a function name can be used to pass in arguments. The arguments can be pretty much any type. You may want to read up on PHP functions and how to do this for a better explanation. – Steve North Oct 12 '16 at 14:26
  • Thank you for the answer however neither way worked in the context of my functions. I am probably doing something wrong. – ERDFX Oct 12 '16 at 14:29
  • In my second function it gives me a FOREACH error for the first method. Second just doesn't work – ERDFX Oct 12 '16 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.