Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Matthew Muro

    (@mmuro)

    VFB Pro can do this by using the vfb_field_default filter.

    Thread Starter balboj

    (@balboj)

    Thanks, I purchased it but still I don’t understand how to set the vfb_field_default.

    I’ve noticed that I’m not the only one who needs to use this feature.
    Could you, please, explain even to non experts how to set it?
    Would not it be nice that was already there by default?
    Thanks in advance, you’ve done an excellent job.

    Plugin Author Matthew Muro

    (@mmuro)

    The documentation I linked to has code to do exactly what you want to do. That code goes in your theme’s functions.php file.

    WordPress StackExchange or StackOverflow are always good places to go for custom code help.

    Hey – i hate to kidnap the forum, but i have a question along the same lines. I’ve got it populating fields, but I can’t get it to populate hidden fields.

    I return the default value just like for the rest of the fields, but my output is the following:

    <input type=”hidden” name=”vfb-59″ id=”vfb-hidden-59″ value=”” class=”vfb-text “>

    Thanks for your help Matthew. Great plugin by the way (just bought pro yesterday).

    Thread Starter balboj

    (@balboj)

    Ok, Matthew or farvgnugn, for sure I’m doing something wrong, but I just can’t figure it out.
    I put that code in the functions.php file of my theme, but what’s the next step?
    If I create a field name and a field mail, they do not fill on their own with the name and email of wordpress users…
    How do the fields understand that their default value must be set by the filter?

    balboj,

    I filter the data based on the form_id and field_id. Here’s an example of what I’m using. This will grow rather quickly if i keep going this route – this’d be better if they were just parameters in the shortcode, but that’s not implemented:

    add_filter( ‘vfb_field_default’, ‘vfb_filter_field_default’, 10, 4 );

    function vfb_filter_field_default( $default, $field_type, $field_id, $form_id ){

    switch ( $field_type ) :

    case ‘name’:

    if ( is_user_logged_in() ) :

    return vfb_get_name();

    endif;

    break;
    case ’email’:

    return vfb_get_email();
    // do nothing

    break;
    endswitch;

    switch ( $form_id ):

    case 3: // Job Application Form

    switch ( $field_id ):

    case 41: //Hidden Name field
    return vfb_get_name(); // I only allow logged in users access to this form.
    break;
    endswitch;

    break;

    case 5: // Test form
    switch ( $field_id ):

    case 57: // Hidden Name
    return vfb_get_name();
    break;
    case 58: // Hidden Email
    return vfb_get_email();
    break;
    case 59: // Visible Text field
    return ‘abcdefg’;
    break;

    endswitch;

    break;

    endswitch;

    return $default;
    }

    function vfb_get_name(){
    $current_user = wp_get_current_user();

    if ( !empty( $current_user->user_firstname ) && empty( $current_user->user_lastname ) )
    $first = $current_user->user_firstname;
    else if ( empty( $current_user->user_firstname ) && !empty( $current_user->user_lastname ) )
    $last = $current_user->user_lastname;
    else if ( !empty( $current_user->user_firstname ) && !empty( $current_user->user_lastname ) ) {
    $first = $current_user->user_firstname;
    $last = $current_user->user_lastname;
    }

    return “$first $last”;
    }

    function vfb_get_email(){
    if ( is_user_logged_in() ) :
    $current_user = wp_get_current_user();
    return $current_user->user_email;
    endif;

    return “”;
    }

    Hope that helps.

    Thread Starter balboj

    (@balboj)

    Thank you very much farvgnugn!
    Now I understand how it works and it was exactly what I wanted to do with the $field_type!
    The problem is that I can’t understand how to run the form_id and field_id.
    I would like to divide the FirstName and LastName fields in two simple text fields but if I use `switch ($ field_id):

    case ‘vfb-11’:

    if (is_user_logged_in ()):

    vfb_get_name return ();

    endif;

    break;`
    nothing happens.
    I have tried using “case 11”, “vfb-text-11”, “vfb-11”, “11”, but I can not understand what is the ID of the text field and how to operate it.
    Thank you again for your patience!

    balboj,

    If you look at the code i posted, you’ll see that the field_id to switch on is not ‘vfb-11’ it is just 11 without quotation marks.

    If you look at the vfb_get_name() function, you’ll see that it returns “$first $last”. You should be able to take that functionality and split it into 2 separate fields.

    let me paste it again and see if it’ll keep the formatting of my code this time:

    function vfb_get_name(){
    	$current_user = wp_get_current_user();
    
        if ( !empty( $current_user->user_firstname ) && empty( $current_user->user_lastname ) )
            $first = $current_user->user_firstname;
        else if ( empty( $current_user->user_firstname ) && !empty( $current_user->user_lastname ) )
            $last = $current_user->user_lastname;
        else if ( !empty( $current_user->user_firstname ) && !empty( $current_user->user_lastname ) ) {
            $first = $current_user->user_firstname;
            $last = $current_user->user_lastname;
        }
    
        return "$first $last";
    }
    Thread Starter balboj

    (@balboj)

    Thank you so much farvgnugn!

    I used just 11 without quotation marks, but the problem was an incorrectly placed break;

    Glad I could help. I’m a VFB newbie like yourself and am just trying to help others while I search for answers myself.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Forms to Auto-Populate Registered User Fields’ is closed to new replies.