最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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;
}
发布评论

评论列表(0)

  1. 暂无评论