Closed Serpentsoft closed 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
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
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 ...
@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()
) );
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 if you're still having this issue can you please send me your theme so that I may take a closer look?
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. :)
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!
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.
Can you share the code,please? I am having same issue.
See https://github.com/aristath/kirki/issues/1702#issuecomment-351792415 I posted an example there
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.