1

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 customization 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 properly 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:

Figure 1

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',
 ),
 )
 );

So after creating the basic customization section, and the basic setting and control for the drop down menu, i tried the following approach

1) Create a separate PHP file (tablequerytest.php) to query the custom table and store results:

<?php

global $wpdb; //Accessing WP Database (non-WP Table) use code below.

$results = $wpdb->get_results('SELECT alias FROM wp_revslider_sliders');

foreach ($results as $result) {
$revchoices = array("text" => $result, "value" => $result);
}

return $revchoices;

?>

2) Insert the result of the above PHP file in to the $wp_customize->add_control choices array like so:

$wp_customize->add_control(
    'select_revslider',
array(
    'type' => 'select',
    'label' => 'Please Select a Slider:',
    'section' => 'example_section_one',
    'choices' => include('jktestrev.php'),
)
);

It seems the above does not work properly and creates problems in displaying the customizer panel. Appreciate any feedback or thoughts on how to make this work or point me to another workaround.

0

So i think I've found the answer to my own question. I modified the code in the original tablequerytest.php which queries the custom table and stores results from this:

<?php

global $wpdb; //Accessing WP Database (non-WP Table) use code below.

$results = $wpdb->get_results('SELECT alias FROM wp_revslider_sliders');

foreach ($results as $result) {
$revchoices = array("text" => $result, "value" => $result);
}

return $revchoices;

?>

To this:

<?php

global $wpdb;

    $query = 'select r.id, r.alias from wp_revslider_sliders r';
    $sliders = $wpdb->get_results($query, OBJECT);

    foreach($sliders as $slider)
    {
        $field[$slider->alias] = sprintf('%s',$slider->alias);
    }

    return $field;

?>

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.