Help: Loading a Function in 'Select' 'Options' array

Can anyone help with loafing a function as the select options?

I am coming from designing blocks for Headway Themes and trying yo work out how to do this in Beaver Builder…

so in Headway for the options I would call the function like so:

options' => 'get_woocomerce_categories()'

So at the bottom of the module I have the function like thus:

function get_woocomerce_categories() {
        
        $args = array(
            'taxonomy' => 'product_cat',
            'orderby' => 'name',
            );
        
        $cat_options = array(
            '' => 'Show All'
            );
        
        $categories = get_categories( $args );
        
        foreach ( $categories as $category ) {
            
            $cat_options[$category->slug] = $category->name;
            
        }

        return $cat_options;

        
    }

Hey Jon,

Thanks for posting! I’m not quite sure I follow you 100% yet, so bare with me. The “select” setting type accepts an array of value/label pairs like so…

'my_select_field' => array(
    'type'          => 'select',
    'label'         => __( 'Select Field', 'fl-builder' ),
    'default'       => 'option-1',
    'options'       => array(
        'option-1'      => __( 'Option 1', 'fl-builder' ),
        'option-2'      => __( 'Option 2', 'fl-builder' )
    )
),

So for example, if a user selects Option 1, the value in your settings will be option-1. Does that make sense?

Thanks,
Justin

Thanks Justin

The function is creating the array as $cat_options so by calling the function it loads the array.

function my_array() {

$array = array('jan' => 'January', 'feb' => 'February', 'mar' => 'March');

return $array;

}

So if I used

'options' => 'my_array()',

I should get all the months in the select field.

Does that make sense now?

Yep that should work! Is it not? Sorry if I’m missing something :slight_smile:

No it doesnt work, it gives me the error in the module

Warning: Invalid argument supplied for foreach() in C:\wamp\www\project52\wp-content\plugins\bb-plugin\includes\field-select.php on line 4

Hey Jon,

Are you sure that get_woocomerce_categories is returning an array? I tested my self with this and it works…

function fl_options_test() {
    	return array( 'test' => 'Test', 'test2' => 'Test 2' );
}
'test_field' => array(
    'type'          => 'select',
    'label'         => __( 'Test Field', 'fl-builder' ),
    'default'       => 'test',
    'options'       => fl_options_test()
)

If you’re developing this as a plugin, send me a copy and I can have a look.

Justin

That was the same function I used in HW with no issues in generating, all good, I switched over to a suggest field that filtered through woos taxonomy.

Thanks
Jon

Cool! Feel free to send modules my way if you’d like me to test anything like that out :slight_smile:

Justin

[Content Hidden]

Hey Jon,

You can send zips to me at justin [at] fastlinemedia [dot] com. I can definitely take a look, just include a little info on Arc so I can get up and running with that.

I saw your email as well and would definitely like to chat! I have to step away for the evening, but I’ll be in touch asap.

Thanks!
Justin