Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save renventura/77d2ead97f443c71c4e57cb7adbc128b to your computer and use it in GitHub Desktop.
Save renventura/77d2ead97f443c71c4e57cb7adbc128b to your computer and use it in GitHub Desktop.
Snippet for programmatically creating entries in WPForms.
<?php
// Create entry.
$entry_id = wpforms()->entry->add( array(
'form_id' => absint( $form_id ),
'user_id' => absint( $user_id ),
'fields' => wp_json_encode( $fields ),
'ip_address' => sanitize_text_field( $user_ip ),
'user_agent' => sanitize_text_field( $user_agent ),
'date' => $date,
'user_uuid' => sanitize_text_field( $user_uuid ),
) );
@goodyis
Copy link

goodyis commented Aug 27, 2024

@renventura I'm missing something, this does not work.
I see this is older, did it work previously?

@goodyis
Copy link

goodyis commented Aug 27, 2024

@renventura
Copy link
Author

I’m honestly not too sure. Been a few years since I’ve used WPForms. It’s possible something in this code is no longer the way of doing it, but I’m not up to date on the plugin so I couldn’t say for certain. It definitely worked when I wrote this, though.

@krug-medien
Copy link

example value for "$fields":
$fields = [ '1' => [ 'name' => 'First name', 'value' => 'Robert', 'id' => 1, 'type' => 'text' ], '2' => [ 'name' => 'Last name', 'value' => 'Smith', 'id' => 2, 'type' => 'text' ], '3' => [ 'name' => 'Email address', 'value' => '[email protected]', 'id' => 3, 'type' => 'email' ] ];

@krug-medien
Copy link

the code works, BUT it only adds the entry to the database, it does not send confirmation mails, create pdf files, ... . on top of that the fields are not added to the table "wp_wpforms_entry_fields" (what happens, if you submit a form non programmatically). does anyone know how to trigger a "real" submit with php?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment