2

I am working with JetEngine forms and i need to add a dropdown of users to select from in one of the form.

is there any way to do this? i dont see any field type of users and also for the select option. if i choose filled dynamicly i have some pre build functions to get the data but none for the users?

How can I set this dropdown or build a function to be show as one of the dynamiclly generated options?

I have not found much on serching this issue and not in their documentation

Lupin
  • 1,225
  • 11
  • 17

2 Answers2

0

As i did not find much info on the web, and there is no real good documentation for the JetEngine Plugins i started digging in the plugins' code. I found that the best solution for me was to create my own form generator - that would give me a custom function to select from when creating a new select field in the form builder.

This is the code i used for creating the custom generator:

add_filter( 'jet-form-builder/forms/options-generators', 'my_jet_form_custom_option_generator' ,10, 1);
add_filter( 'jet-engine/forms/options-generators', 'my_jet_form_custom_option_generator' ,10, 1);

function my_jet_form_custom_option_generator($instances) {
    require(plugin_dir_path(__FILE__). '/jetBuilder/generators/get-users.php');
    $instances[] = new Jet_Form_Builder\Generators\Get_Users();
    return $instances;
}

And this is the basic structure of the actual get-users generator:

namespace Jet_Form_Builder\Generators;

require_once(WP_PLUGIN_DIR.'/jetformbuilder/includes/generators/base.php'); 


class Get_Users extends Base {

    /**
     * Returns generator ID
     *
     * @return string
     */
    public function get_id() {
        return 'my_generator;
    }

    /**
     * Returns generator name
     *
     * @return string
     */
    public function get_name() {
        return __( 'My Cool Custom Generator', '' );
    }

    /**
     * Returns generated options list
     *
     * @param $args
     *
     * @return array
     */
    public function generate( $args ) {

        global $wpdb;
                
        // this is a dummy SQL....
        $sql = "SELECT {$wpdb->users}.user_email, {$wpdb->users}.display_name, {$wpdb->users}.ID, {$wpdb->usermeta}.meta_value
            FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON ({$wpdb->users}.ID = {$wpdb->usermeta}.user_id)
            WHERE 1=1 "; // CHANGE QUERY FOR YOUR NEEDS....

        $result = array();
        
        $table  = $wpdb->users;
        $rows   = $wpdb->get_results(
                            
            $wpdb->prepare( $sql , $field),
            ARRAY_A
        );

        if ( empty( $rows ) ) {
            return $result;
        }
        
        $result[] = array(
                'value' => 0,
                'label' => '-----------',
            );

        foreach ( $rows as $row ) {
            $result[] = array(
                'value' => $row['ID'],
                'label' => $row['display_name'],
            );
        }

        return $result;

    }

}

Hope this helps...

Lupin
  • 1,225
  • 11
  • 17
0

There is an alternative way of doing it without code as they introduced a dynamic function to pull the user list via query builder.

Detail explanation here https://youtu.be/v3QR3824F4Y?t=420

Moxet Jan
  • 120
  • 8