My Account

Forums

Forums General Select Field: Change / Add own data sources Reply To: Select Field: Change / Add own data sources

#66232
Dan Dickson
Participant

For reference, here is some sample code to dynamically pull data using a filter hook. I got hung up on the nested arrays.

add_filter( 'wsf_test_data_source_hook', 'wsf_test_data_source_hook_callback', 10, 3 );
function wsf_test_data_source_hook_callback( $data_grid, $field_id, $form_object ) {
    if( $field_id !== 325) { return $data_grid; }
	global $wpdb;
    $data_grid[ 'columns' ] = array(
        array( 'label' => __( 'name' ) ),
        array( 'label' => __( 'email' ) )
    );
	
	$results = $wpdb->get_results("SELECT name,email FROM directory", ARRAY_A);
	
    $data_grid[ 'groups' ] = array(
        array(
            'label' => __('data'),
            'rows' => array(
                array(
                    'data' => array()
                )
            )
        )
    );

	
	foreach ($results as $result)
	{
		$data_grid['groups'][0]['rows'][] = array('data' => array(
					$result['name'],
   					$result['email']));
	}
	
    return $data_grid;
}