theme development - Retrieve data using wpdb to use for customizer controls

So I have the following code:add_action('customize_register', 'homepage_sections');productsfunct

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

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

Share Improve this question edited Sep 18, 2019 at 12:29 joebegborg07 asked Sep 15, 2019 at 22:46 joebegborg07joebegborg07 939 bronze badges 6
  • 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 Commented Sep 16, 2019 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 Commented Sep 16, 2019 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 Commented Sep 16, 2019 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 Commented Sep 17, 2019 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 Commented Sep 18, 2019 at 9:32
 |  Show 1 more comment

1 Answer 1

Reset to default 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;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745144138a4613568.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信