User Registration Form
-
Hello,
I have created the form for user registration. I have 3 questions.
1) How can I assign the username automatically as WordPress does? Currently, in the dropdown for username, I only see ACF fields that I created.
2) How can I automatically login the user after registration?
3) How can I redirect to a page after the registration?
Thanks.
-
Sorry. Also, I have a custom field for image upload, but I cannot see this image in the user profile.
Hello,
Thanks for the feedback!
1. WordPress requires both a username & an email on front-end user registration and in back-end, when an admin manually create a user. However with ACF Extended Form, the only requried field is an email. In fact, if there is no username submitted, the ACFE Form will automatically use the e-mail as username.
2. In order to login a user after the form submission, you can use the
acfe/form/submit/userhook (See documentation) and programatically log the user using native WP functions. Usage example:add_action('acfe/form/submit/user/form=my-form', 'my_form_submit', 10, 5); function my_form_submit($user_id, $type, $args, $form, $action){ // log user after user registration wp_clear_auth_cookie(); wp_set_current_user($user_id); wp_set_auth_cookie($user_id); }Note that a setting will be added in the Form UI in a future update in order to do it from the UI.
3. You can use the “Redirect Action” in order to redirect the form after submission (See documentation).
In that action, like in other Actions in the UI, you can use Template Tags in order to render dynamic values from previous actions (See documentation). For example, if you want to redirect the form on the “User Action” author page, you can set the Redirect URL setting to:
{action:user:permalink}. You can also render the User ID with{action:user:ID}if you prefer, like this:/my-success-page?user_id={action:user:ID}4. In order to upload an Image Field you’ll have to check the said field in the “Save ACF Fields”. See screenshot.
Hope it helps!
Have a nice day!
Regards.
Thanks for your response.
The image field does not show up there, but I’ll check again.
One last thing. Is there a way to create more than one post at a time? Something similar ACF repeater field. So, the user will add a row and when submits, it will create let’s say 3 posts.
Hello,
Thanks for the feedback!
The Image field will be displayed in the “Save ACF Fields” checkboxes if it’s a top level field. If the image is part of a complex field such as a Group or a Repeater, you’ll have to check and save the whole Group/Repeater in that “Save ACF Fields”. This is due to the ACF core logic which doesn’t allow to save only parts of sub fields.
Regarding your question about creating multiple posts based on a Repeater, you can achieve that programmatically with Forms Hooks (See documentation).
Here is a usage example:
add_action('acfe/form/submit/form=my-form', 'my_form_submit', 10, 2); function my_form_submit($form, $post_id){ // loop repeater if(have_rows('my_repeater')): while(have_rows('my_repeater')): the_row(); // get sub field $sub_field = get_sub_field('my_sub_field'); // insert post wp_insert_post(array( 'post_type' => 'post', 'post_title' => $sub_field // ... )); endwhile; endif; }See
wp_insert_post()documentation for more details about available arguments.Hope it helps!
Have a nice day!
Regards.
Thanks a lot for your help. So, I select custom action in the form and add the action name, right? What is the action name in this case? I added this as action name but it did not do anything. my_form_submit. Or, we need two action? One is this and the second is create a post?
Hello,
Thanks for the feedback!
You’re not forced to use a “Custom Action” in order to insert posts programmatically using values from your form.
The code I shared above use the
acfe/form/submithook, which is a global hook executed anytime the form is submitted, independently from the actions in the UI (See documentation).Technically, you could have no actions at all in the form UI, and completely manage post insertion, user registration, add validation errors etc… in pure PHP using these global hooks.
The very first code I shared regarding automatic user login is not a global hook, as it uses the
acfe/form/submit/userhook, which is executed when the “User Action” is processed during the form submission (See documentation).Here is a small recap of forms hooks, pay attention to names:
// global hooks acfe/form/validation/form=my-form // validation of form 'my-form' acfe/form/submit/form=my-form // submission of form 'my-form' // actions hooks acfe/form/validation/user/form=my-form // validation of User Action in form 'my-form' acfe/form/submit/user/form=my-form // submission of User Action in form 'my-form'This logic is also applied to others actions (Post Action, Term Action etc…).
If you prefer to use a Custom Action for your “Repeater Insert Post” logic, in order to keep your code organized, it’s perfectly fine. The Custom Actions hooks use the same logic (See documentation), using the name you’ll give to it in the hooks:
// custom action hooks acfe/form/validation/my-action/form=my-form // validation of Custom Action 'my-action' in form 'my-form' acfe/form/submit/my-action/form=my-form // submission of Custom Action 'my-action' in form 'my-form'What is nice with the Custom Action approach, is that if you have the same Repeater in two forms and want to do the same “Insert a post for each row” in two different forms, then you don’t have to write the same code 2 times.
You just have to add the Custom Action with your unique name in both forms, and add the PHP hook without specifying a form name, like this:
// notice there is no '/form=my-form' in the hook name // so this code will be executed anytime 'my-action' is in a form add_action('acfe/form/submit/my-action', 'my_action_submit', 10, 2); function my_action_submit($form, $action){ // loop repeater // insert posts... }Bonus: You can also simply add the “loop repeater and insert post” code inside the very first “automatic user login” code I shared, it will also work.
So in the end, it’s just a question of personal preference. It depends on what kind of code organization you want to adopt.
Hope it’s now more clear!
Have a nice day!
Regards.
`add_action(‘acfe/form/submit/my-action’, ‘my_action_submit’, 10, 2);
function my_action_submit($form, $action){// loop repeater
if(have_rows(‘task_list_next_30_days’)):
while(have_rows(‘task_list_next_30_days’)): the_row();// get sub field
$sub_field = get_sub_field(‘task_title_next_30_days’);// insert post
wp_insert_post(array(
‘post_type’ => ‘task’,
‘post_title’ => $sub_field,
‘post_status’ => ‘published’,
// …
));endwhile;
endif;}
This is my code but it does not create posts. 🙁
Hello,
Thanks for the feedback!
Unfortunately “it doesn’t work” is too vague. You need to isolate the problem to understand what is going on in your code.
I would recommend to enable the WP_DEBUG mode in your
wp-config.phpfile using the following code (See documentation):define('WP_DEBUG', true); // enable debug define('WP_DEBUG_LOG', true); // enable logs define('WP_DEBUG_DISPLAY', false); // disable errors on screenErrors and logs will be located in
/wp-content/debug.log. I would recommend to log some variables in your code, to make sure:- Your hook is executed
- Your repeater exists
- Your sub field exists
- And so on…
Here is a simple debug log you can start with:
add_action('acfe/form/submit/form=my-form', 'my_form_submit', 10, 2); function my_form_submit($form, $post_id){ acf_log('my-form is submitted!'); } add_action('acfe/form/submit/my-action', 'my_action_submit', 10, 2); function my_action_submit($form, $action){ acf_log('my-action is submitted!'); if(have_rows('task_list_next_30_days')): while(have_rows('task_list_next_30_days')): the_row(); acf_log('new repeater row'); $sub_field = get_sub_field('task_title_next_30_days'); acf_log('sub field value:', $sub_field); // insert post $new_post_id = wp_insert_post(array( 'post_type' => 'task', 'post_title' => $sub_field )); acf_log('new post id:', $new_post_id); endwhile; endif; }Hope it helps!
Have a nice day!
Regards.
The topic ‘User Registration Form’ is closed to new replies.