Quantcast
Channel: WordPress.org Forums » [Forminator Forms – Contact Form, Payment Form & Custom Form Builder] Support
Viewing all articles
Browse latest Browse all 1451

Issue with Creating Custom Post After User Registration Using Forminator Form

$
0
0

Replies: 0

Dear Forminator Support Team,

I am experiencing an issue with creating a custom post automatically after a user registers using a Forminator form on my WordPress site. I have implemented custom code to create a new post of type “custom” based on ACF fields when a user registers with the “Subscriber” role. However, the custom post is not being created as expected.

Here is the custom code I am using in my functions.php file:

function create_custom_post_after_registration($user_id) {
// Check user role
$user = get_userdata($user_id);
if (in_array('subscriber', $user->roles)) {
// Get the country value from the ACF field
$country = get_field('country', 'user_' . $user_id);

// Check if the country field is not empty
if (!empty($country)) {
// Set up the custom post data with the country as the title
$post_data = array(
'post_title' => $country, // Set the title to the country ACF field value
'post_content' => '', // Set an empty content for now
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'city', // Use the slug 'city' for the custom post type
);

// Insert the post into the database
$post_id = wp_insert_post($post_data);

// Update the 'country' custom field with the user's country data
update_field('country', $country, $post_id);
}
}
}
add_action('user_register', 'create_custom_post_after_registration');

I would appreciate any guidance on why the custom post creation is not working as expected when using the Forminator form and how to resolve this issue.

Thank you for your assistance.

Best regards


Viewing all articles
Browse latest Browse all 1451

Trending Articles