• Resolved NixXxon23

    (@nixxxon23)


    Hello,

    first of all, thank you for the great plugin!!! Such a time saver!

    I dont get it how to pre fill the form.
    I have a form with this shortcode “[cf7form cf7key=”ohne-titel_copy”]”

    If I try to use your example code in my functions.php it does nothing and I cant find the failure. Maybe you can help?

    $content = get_the_content();
    //let's find our shortcode
    preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches );
    $args = array();
    if( isset( $matches[2] ) ){
      foreach( (array) $matches[2] as $key => $shortcode ){
        if( 'contact-form-7' === $shortcode ) //do I need to change this to "ohne-titel_copy"? --> Already tried but did not help
          $args[] = shortcode_parse_atts( $matches[3][$key] );
      }
    }
    //here I am assuming there is a single form on the page, if you have multiple, you will need to loop though each one
    if(!empty($args) && isset($args[0]['id'])){
      $short_id = $args[0]['id'];
      $values = array();
    
      if(is_user_logged_in()){
        $user = wp_get_current_user();
        $values['your-name']= $user->display_name;
        $values['post-id']= get_the_ID();
      
        //now lets filter the values
        add_filter('cf7_2_post_form_values', function($field_values, $cf7_id) use ($short_id, $values){
          if($short_id == $cf7_id ){
            return $values;
          }
        }, 10,2);
      }
    }
    

    I’m pretty sure it’s just something small that I’m missing?!

    BR;
    Niko

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    I have a form with this shortcode “[cf7form cf7key=”ohne-titel_copy”]”

    is your form mapped to a post type? This functionality only works for mapped forms

    Thread Starter NixXxon23

    (@nixxxon23)

    Thanks for the fast reply.
    Yes, it is linked to a custom post type named “gast”. The submission works fine but it looks like the hook doesn’t work.

    I actually just need 1 field prefilled with the id of the current post.

    BR,
    Niko

    Plugin Author Aurovrata Venet

    (@aurovrata)

    The submission works fine but it looks like the hook doesn’t work.

    I just checked on my server and the hook works fine.

    Did you check if the hook is being called at all?

    use the filter in your functions.php, id doesn’t look like you need the code in your page template. This will be simpler.

    
    add_filter('cf7_2_post_form_values', 'simple_cf7_form_values' ,10,4);
    function simple_cf7_form_values($field_values, $cf7_id, $post_type, $ck7_key){
      if( 'my-form'!=$ck7_key ) return $field_values;
       $field_values['your-name'] = 'prefilled';
       return $field_values;
    }
    
    Thread Starter NixXxon23

    (@nixxxon23)

    Thank you! You are awesome – works like a charm.
    But I ran into another issue. I also use “Date Time Picker Field
    By Carlos Moreira”. According to your desctipition it should be compatible.

    All fields but the date / time field are submitted.

    Any idea? I already tried to unmap / map the field again but no luck.
    The field is a simple “textfield”.

    Thank you very much again for your awesome plugin. Such a time saver!

    BR;
    Niko

    Plugin Author Aurovrata Venet

    (@aurovrata)

    Thank you! You are awesome – works like a charm.

    great. So I am marking this thread as resolved.

    Do leave a review when you have a moment to spare.

    But I ran into another issue

    please start a new support thread for each issue, this makes it easier to track as well as for others to find.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Load default values (pre-fill form)’ is closed to new replies.