|
<?php |
|
/* |
|
Forminator - set new user as author of new post |
|
|
|
This is only for user registration forms that also have postdata field |
|
and have user activation set to "default" |
|
|
|
Tested with Forminator 1.17.2 |
|
by adamcz/WPMU DEV |
|
|
|
|
|
USE AS MU PLUGIN |
|
|
|
|
|
Config explained in comments |
|
*/ |
|
|
|
// get the entry ID into temporary $_POST key (as otherwise it's not available) |
|
add_action( 'forminator_custom_form_submit_before_set_fields', 'forminator_usereg_author_entry_id', 20, 3 ); |
|
function forminator_usereg_author_entry_id( $entry, $form_id, $field_data_array ) { |
|
|
|
$_POST['_forminator_author_entry_id'] = $entry->entry_id; |
|
$_POST['_forminator_author_form_id'] = $form_id; |
|
|
|
} |
|
|
|
|
|
/* after submission is saved successfully |
|
- fetch entry based on saved entry ID |
|
- find newly registered user ID |
|
- find created post ID |
|
- assign this user to that post |
|
*/ |
|
add_action( 'forminator_form_after_save_entry', 'forminator_usereg_author_set', 10, 2 ); |
|
function forminator_usereg_author_set( $form_id, $response ) { |
|
|
|
|
|
/* CONFIGURE HERE */ |
|
$forms = array( 14369 ); // comma separated IDs of forms this should work with |
|
$usermail_field = 'email-1'; // field ID of user registration mail |
|
$postdata_field = 'postdata-1'; // field ID of post data field |
|
/* CONFIGURATION END */ |
|
|
|
|
|
|
|
if ( ! in_array( $form_id, $forms ) ) { |
|
return; |
|
} |
|
|
|
if ( $response && is_array( $response ) ) { |
|
if ( $response['success'] ) { |
|
|
|
if ( $form_id == $_POST['_forminator_author_form_id'] ) { |
|
|
|
// fetch entry data |
|
$entry = Forminator_API::get_entry( $form_id, $_POST['_forminator_author_entry_id'] ); |
|
|
|
// get new user ID |
|
$user_mail = $entry->meta_data[$usermail_field]['value']; |
|
$userid = get_user_by( 'email', $user_mail ); |
|
|
|
// get new post ID |
|
$postid = $entry->meta_data[$postdata_field]['value']['postdata']; |
|
|
|
// update that post to set new user as author |
|
$args = array( |
|
'ID' => $postid, |
|
'post_author' => $userid->ID, |
|
); |
|
|
|
wp_update_post( $args ); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
} |
|
|
|
} |