kirki icon indicating copy to clipboard operation
kirki copied to clipboard

A big issue in add_field function

Open Serpentsoft opened this issue 11 years ago • 11 comments

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

Serpentsoft avatar Sep 16 '15 00:09 Serpentsoft

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

aristath avatar Sep 16 '15 00:09 aristath

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

Serpentsoft avatar Sep 16 '15 03:09 Serpentsoft

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 ...

Anydog avatar Sep 16 '15 14:09 Anydog

@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()
) );

aristath avatar Sep 26 '15 16:09 aristath

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.

Serpentsoft avatar Sep 30 '15 18:09 Serpentsoft

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

aristath avatar Oct 10 '15 02:10 aristath

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. :)

aristath avatar Oct 13 '15 17:10 aristath

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 avatar Feb 22 '17 06:02 hellor0bot

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.

hellor0bot avatar Feb 22 '17 07:02 hellor0bot

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

webangon avatar Oct 13 '17 12:10 webangon

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

aristath avatar Dec 14 '17 18:12 aristath