Dear Sir/Madam,
I have a form for frontend user to do the registration and then insert a new custom post automatically.
When I manually add a user from dashboard, it create the custom post also, refer to below code, how can I only do the custom post creation if I do the registration from the form?
function auto_apply_competition($user_id){
if (!$user_id>0 && !empty($_POST['gf-competition-id']))
return;
$post_title = sprintf("%s - %s", get_the_title($_POST['gf-competition-id']), $_POST['gf-competition-id']);
$new_post = array(
'ID' => '',
'post_author' => $user_id,
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'publish',
'post_type' => 'vwcm-application',
'meta_input' => array(
'wpcf-competition-id' => $_POST['gf-competition-id'],
'wpcf-apply-level'=> $_POST['gf-apply-level'],
'wpcf-order-status' => "unpaid",
'wpcf-order-number' => '',
'wpcf-upload-receipt' => $_POST['gf-upload-receipt']
)
);
$post_id = wp_insert_post($new_post, 1);
wp_update_post( array(
'ID' => $post_id,
'post_name' => $post_id,
'post_title' => sprintf("%s - %s (%s)", get_the_title($_POST['gf-competition-id']), $_POST['gf-competition-id'], $post_id)
));
}
add_action( 'user_register', 'auto_apply_competition', 10, 1 );
Hi,
Thank you for contacting us and I'd be happy to assist.
The "user_register" is a generic WordPress hook, which is why it is executed whenever a new user is added, through the admin area or the front-end Toolset form.
If your goal is to create the automatic post, only when the user is added through the front-end Toolset form, you can instead use the "cred_submit_complete" hook from Toolset Forms API:
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete )
Example:
add_action('cred_submit_complete', 'auto_apply_competition',10,2);
function auto_apply_competition($user_id, $form_data)
{
// if a specific form
if ($form_data['id']==12345)
{
// rest of the code to add a new post
}
}
Note: Please replace "12345" with the actual Toolset form's ID.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Dear Waqar,
Thanks for your reply. May I know whether the hook cred_submit_complete is only applied to post_form or user_form?
Thanks for writing back and the "cred_submit_complete" hook is applied to both, post and user forms.