Reputation: 35
I am trying to create a select box/dropdown menu in the Wordpress theme customization area where the options are to be extracted from a column called "alias" in a table named "wp_revslider_sliders" from my Wordpress DB.
I have already created the section, and the basic setting and control for the drop down menu (see Fig. 1 below), but being a novice in this area, I can't figure out how to query the Wordpress DB, extract the results from the "Alias" column of my "wp_revslider_sliders" table and insert them output in to the "choices array" below
Fig. 1
$wp_customize->add_control(
'select_revslider',
array(
'type' => 'select',
'label' => 'Please Select a Slider:',
'section' => 'example_section_one',
'choices' => array(
'wordpress' => 'WordPress',
),
)
);
Fig. 2
function example_customizer( $wp_customize ) {
$wp_customize->add_section(
'example_section_one',
array(
'title' => 'Example Settings',
'description' => 'This is a settings section.',
'priority' => 35,
)
);
$wp_customize->add_setting(
'select_revslider',
array(
'default' => 'wordpress',
)
);
$wp_customize->add_control(
'select_revslider',
array(
'type' => 'select',
'label' => 'Please Select a Slider:',
'section' => 'example_section_one',
'choices' => array(
'wordpress' => 'WordPress',
),
)
);
Upvotes: 2
Views: 1367
Reputation: 1826
Within your function.php file crate function like this:
function alias_from_wp_revslider_sliders() {
$sql = "SELECT aliasname FROM wp_revslider_sliders";
$result = $conn->query($sql);
$alias_list = array();
foreach($alias_list as $alias) {
$alias_list[$alias->aliasname] = $alias->aliasname;
}
return $alias_list;
}
and than when you add the control instead of choices call the function we created inside functions.php
$wp_customize->add_control(
'select_revslider',
array(
'type' => 'select',
'label' => 'Please Select a Slider:',
'section' => 'example_section_one',
'choices' => function alias_from_wp_revslider_sliders(),
)
Upvotes: 1