2

So I have the following code:

add_action('customize_register', 'homepage_sections');
//products
function homepage_sections($wp_customize){
    $wp_customize->add_panel('homepage_sections', array(
        'title'             => 'Homepage Sections',
        'priority'          => '20'
    ));
    $wp_customize->add_section('homepage_settings_section', array(
        'title'             =>  'Homepage settings',
        'panel'             =>  'homepage_sections',
    ));
    $wp_customize->add_setting('homepage_settings_setting', array(
        'default'           =>  1
    ));
    $wp_customize->add_control('homepage_settings_control', array(
        'section'           =>  'homepage_settings_section',
        'settings'          =>  'homepage_settings_setting',
        'label'             =>  'Number of sections',
        'description'       =>  'Number of sections in homepage',
        'type'              =>  'number'
    ));


    global $wpdb;
    $sections=$wpdb->get_results('SELECT section_id, section_title FROM vt_homepage_sections;');

    foreach($sections as $key){
        $section_id=$key->section_id;
        $cust_setting_id=$section_id.'_setting';
        $cust_control_id=$section_id.'_control';


        $wp_customize->add_setting($cust_setting_id,array(

        ));
        $wp_customize->add_control($cust_control_id,array(
            'settings'          =>  $cust_setting_id,
            'section'           =>  'homepage_settings_section',
            'label'             =>  'test Control'
        ));
    }
}

Issue Everything works fine when i don't use variables which contain a value fetched using $wpdb. Is $wpdb object loaded after customizer framework?

When I use the code above, the customizer objects in context should appear in the customizer panel, however they don't. Would appreciate hints to what's wrong with my code above.

regards,

J

  • 1
    $wpdb is already "good" (initialized and you can use its methods) in your homepage_sections(). And your code seems just fine. What error you get? – Sally CJ Sep 16 '19 at 0:32
  • Thanks for the reply @SallyCJ. There's no error (even though wp_debug is true. They just don't show up in customizer. – joebegborg07 Sep 16 '19 at 5:31
  • You may want to use a var_dump and error_log to troubleshoot: ob_start(); var_dump($sections); error_log('sections = ' . ob_get_clean(),0). This will export the var_dump to your system's PHP log, where you can figure out if $sections really contains what you expect. – Mike Baxter Sep 16 '19 at 20:31
  • $sections simply returns an array of objects, and each object contains a property with the value of 'section_id' db field. – joebegborg07 Sep 17 '19 at 4:48
  • Turn on WP_DEBUG_LOG and check wp-content/debug.log for any errors relevant to the issue. And what exactly do you mean by "the above customized objects font appear in the customizer panel"? Show what you see. – Sally CJ Sep 18 '19 at 9:32
2
+50

So it seems that there maybe two issues here first it seems that you are not establishing a new instance of a WP_Customize_Control like below and also it is important to remember that you have to give at least the min array items to both the setting and the control or it will not show, you had an empty array in the setting.

you also want to make sure that you are not starting your naming conventions with numbers, so I switched them around below.

Here is some changes to your code that you can try:

add_action('customize_register', 'homepage_sections');
//products
function homepage_sections($wp_customize){
    $wp_customize->add_panel(
        'homepage_sections', 
        array(
            'title'             => 'Homepage Sections',
            'priority'          => '20'
        )
    );
    $wp_customize->add_section(
        'homepage_settings_section', 
        array(
            'title'             =>  'Homepage settings',
            'panel'             =>  'homepage_sections',
        )
    );
    $wp_customize->add_setting(
        'homepage_settings_setting', 
        array(
            'default'           =>  1
        )
    );
    $wp_customize->add_control(
       new WP_Customize_Control(
          'homepage_settings_control', 
           array(
              'section'           =>  'homepage_settings_section',
              'settings'          =>  'homepage_settings_setting',
              'label'             =>  'Number of sections',
              'description'       =>  'Number of sections in homepage',
              'type'              =>  'number'
           )
        )
    );


    global $wpdb;
    $sections=$wpdb->get_results('SELECT section_id, section_title FROM vt_homepage_sections;');

    foreach($sections as $key) :
        $section_id=$key->section_id;
        $cust_setting_id = 'setting_' . $section_id;
        $cust_control_id = 'control_' . $section_id;


        $wp_customize->add_setting(
            $cust_setting_id,
            array(
                'default'   => '',
                'transport' => 'refresh',
            )
        );
        $wp_customize->add_control(
            new WP_Customize_Control(
                $cust_control_id,
                array(
                    'settings'          =>  $cust_setting_id,
                    'section'           =>  'homepage_settings_section',
                    'label'             =>  'test Control'
                )
            )
        );
    endforeach;
}
  • 1
    Problem solved Rudy. Thank you :) – joebegborg07 Sep 20 '19 at 11:47
  • Your welcome. Thank you. – Rudy Lister Sep 21 '19 at 16:05

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.