themeum / kirki

Extending the customizer
https://kirki.org
MIT License
1.26k stars 328 forks source link

A big issue in add_field function #352

Closed Serpentsoft closed 9 years ago

Serpentsoft commented 9 years ago

Hi dude, Many thanks for your work .. Really it's awesome

I discover this issue because i want a list of registered sidebars so i try the following code and it doesn't work, BUT it works properly and all sidebars grabbed if using the default WordPress Customizer functions (add_control). I think it's an issue in add_action priority may be.

    global $wp_registered_sidebars;

    $arr_sidebars = array();

    $i = 0;
    $default_value = 0;

    foreach ($wp_registered_sidebars as $registered_sidebar) {
        if($i == 0){
            $default_value = $registered_sidebar['id'];
            $i++;
        }
        $arr_sidebars[$registered_sidebar['id']] = $registered_sidebar['name'];
    }

    Kirki::add_field( '$config_id', array(
        'type'        => 'select',
        'settings'     => '$setting_id',
        'label'       => '$label',
        'description' => '$desc',
        'help'        => '$help',
        'section'     => '$container_section',
        'default'     => $default_value,
        'priority'    => '$priority',
        'choices'     => $arr_sidebars,
    ) );
aristath commented 9 years ago

Is that wrapped inside a function? Can you please post the whole contents of your file? If it's too big for here, you can create a new gist for that file and post the link for it back here

Serpentsoft commented 9 years ago

No it's not inside a function BUT i test it inside a function but it's not work .. anyway ..

My Code

  // FILE - customizer.php */
  Kirki::add_config( 'srpcust_config', array(
      'capability'    => 'edit_theme_options',
     'option_type'   => 'theme_mod',
 ) );
 require_once( get_template_directory() . '/inc/customizer/panels/pnl-layout.php' );

  // FILE - pnl-layout.php */
  Kirki::add_panel( 'pnl_post', array(
       'priority'    => 50,
       'title'       => __( 'Post Settings', 'domain' ),
       'description' => '',
  ) );

   Kirki::add_section( 'sec_layout', array(
         'title'          => __( 'Layout Sidebar', 'domain' ),
         'description'    => '',
         'panel'          => 'pnl_post', // Not typically needed.
         'priority'       => 10,
         'capability'     => 'edit_theme_options',
         'theme_supports' => '',
  ) );

  /* CODE - Doesn't Work */
  // The Code that i had sent before with [Kirki::add_field] function

  /* CODE - The Following Work Properly */
  if( !function_exists('default_post_sidebar') ){
  function default_post_sidebar($wp_customize){

    global $wp_registered_sidebars;
    $arr_sidebars = array();
    $i = 0;
    $default_value = 0;

    foreach ($wp_registered_sidebars as $registered_sidebar) {
        if($i == 0){
             $default_value = $registered_sidebar['id'];
             $i++;
        }
        $arr_sidebars[$registered_sidebar['id']] = $registered_sidebar['name'];
    }

    $wp_customize->add_setting( 'sb_post_default', array(
        'type' => 'theme_mod',
        'capability' => 'edit_theme_options',
        'theme_supports' => '',
        'default' => $default_value,
        'transport' => 'refresh',
        'sanitize_callback' => '',
        'sanitize_js_callback' => '',
    ) );

    $wp_customize->add_control( 'sb_post_default', array(
        'settings' => 'sb_post_default',
        'type'     => 'select',
        'label'    => __('Sidebars', 'domain'),
        'priority' => 10,
        'section'  => 'sec_layout',
        'choices' => $arr_sidebars
    ) );
}
add_action( 'customize_register', 'default_post_sidebar' );
} // function_exists
Anydog commented 9 years ago

Perhaps wrapping your Kirki::add_panel and Kirki::add_section into function and hook it to add_action('init') will help (and perhaps other code too) ? Just a thought ...

aristath commented 9 years ago

@Serpentsoft hey there!

Can you please test if this works?

Kirki::add_config( 'srpcust_config', array(
    'capability'    => 'edit_theme_options',
    'option_type'   => 'theme_mod',
) );

Kirki::add_panel( 'pnl_post', array(
    'priority'    => 50,
    'title'       => __( 'Post Settings', 'domain' ),
    'description' => '',
) );

Kirki::add_section( 'sec_layout', array(
    'title'          => __( 'Layout Sidebar', 'domain' ),
    'description'    => '',
    'panel'          => 'pnl_post',
    'priority'       => 10,
    'capability'     => 'edit_theme_options',
    'theme_supports' => '',
) );

function my_custom_get_default_value() {
    global $wp_registered_sidebars;
    $default_value = 0;
    $i = 0;
    foreach ( $wp_registered_sidebars as $registered_sidebar ) {
        if ( 0 == $i ) {
            $default_value = $registered_sidebar['id'];
        } else {
            continue;
        }
    }
    return ( isset( $default_value ) ) ? $default_value : '';
}

function my_custom_post_sidebars() {
    global $wp_registered_sidebars;
    $arr_sidebars = array();
    $i = 0;
    $default_value = 0;

    foreach ( $wp_registered_sidebars as $registered_sidebar ) {
        $arr_sidebars[ $registered_sidebar['id'] ] = $registered_sidebar['name'];
    }

    return $arr_sidebars;
}

Kirki::add_field( 'srpcust_config', array(
    'default'  => my_custom_get_default_value(),
    'settings' => 'sb_post_default',
    'type'     => 'select',
    'label'    => __( 'Sidebars', 'domain' ),
    'priority' => 10,
    'section'  => 'sec_layout',
    'choices'  => my_custom_post_sidebars()
) );
Serpentsoft commented 9 years ago

Hey @aristath, sorry for delay

No it's not working, I think it's a priority issue.

perhaps add_field function called before sidebars initialization.

aristath commented 9 years ago

@Serpentsoft if you're still having this issue can you please send me your theme so that I may take a closer look?

aristath commented 9 years ago

Closing due to inactivity. If you're still having this issue then please feel free to reopen it and send me as many details as possible so that we may debug this. :)

hellor0bot commented 7 years ago

Hello, I'm having the same issue.

Function, returning an array of registered sidebars:

/**
 * Returns Array of Registered Sidebars
 */
function prefix_get_registered_sidebars() {
    global $wp_registered_sidebars;
    $choices = array();
    foreach ( $wp_registered_sidebars as $sidebar ) {
        $choices[ $sidebar['id'] ] = $sidebar['name'];
    }
    return $choices;
}

Creating a select field in Kirki:

Kirki::add_field( 'csco_theme_mod', array(
    'type'        => 'select',
    'settings'    => 'layout_widgets_sidebar',
    'label'       => esc_html__( 'Sidebar', 'domain' ),
    'section'     => 'layout',
    'default'     => 'sidebar-archive',
    'priority'    => 10,
    'choices'     => prefix_get_registered_sidebars(),
) );

I suppose, there's something wrong with priorities.

Hope, you could look into this.

Thank you!

hellor0bot commented 7 years ago

OK, so I wrapped the Kirki::add_field in a function and hooked it on init action. It's working. Seems, like it's really an action priority issue.

webangon commented 7 years ago

Can you share the code,please? I am having same issue.

aristath commented 6 years ago

See https://github.com/aristath/kirki/issues/1702#issuecomment-351792415 I posted an example there