Stop generating pages after form submission
-
I have made a form with the User action: Create user, then I used it as Shortcode inside a Page. I noticed after sending the form, it automatically creates a new page with a new permalink, is it possible to stop creating such like pages automatically, of course I do not use the Post action.
regards
-
Hello,
Thanks for the the feedback!
The ACF Extended Form “Create User” action doesn’t create any Post or Post Type, unless you specifically added a custom hook or code in your logic. I would recommend to double check that you don’t have any custom hook/code somewhere.
If the problem persists, maybe the problem comes from a third party plugin which would create a post when a user is created by WordPress. I would recommend to try to disable your plugins one by one (at the exception of ACF & ACF Extended), see if that fixes the issue.
Hope it helps!
Have a nice day!
Regards.
well thank you for your answer,
I have already double checked My website, and I have found this code which causing the Problem, which is natives ACF Code.// Save ACF custom field to date-time post function my_acf_save_post( $post_id ) { $acfDate = get_field('studyshoot_time', $post_id); //Test if you receive the data field correctly: //echo $acfDate; //exit (-1); $my_post = array(); $my_post['ID'] = $post_id; $my_post['post_date'] = $acfDate; wp_update_post( $my_post ); } add_action('acf/save_post', 'my_acf_save_post', 20);
can you suggest some changes?
I should say, you have a great Plugin. 👌. It is GreatHello,
Thanks for the feedback!
In fact, you should be careful when using the
acf/save_post
hook, as this action will be executed anywhere in the admin when updating a Post/User/Term/Options Page etc… and on front-end forms too (using the native ACF Form or ACFE Form).Since it is executed on different objects such as Post/User/Term/Options Page, the
$post_id
passed by ACF will reflect that object (See documentation). Example:$post_id = 123; // on a Post Type page $post_id = 'user_12'; // on a User page $post_id = 'term_45'; // on a Term page $post_id = 'my-options'; // on an Options Page
In the case of the ACFE Form, the
$post_id = false
, and since you usewp_update_post()
in that hook and assign withID = $post_id
(ID = false
), WordPress will try to retrieve the current post (the “Form” page on the front-end), and generate a new page from it.Based on those information, when using
acf/save_post
I recommend to always check and validate the$post_id
to make sure it targets the Post Type/User/Term/Options Page of your choice.Note that you can also use
is_admin()
as a condition if you want to make sure your code is only executed in the admin if needed. Usage example:add_action('acf/save_post', 'my_acf_save_post', 20); function my_acf_save_post($post_id){ // stop if: on front-end OR post_id is not numeric OR post_type is not "page" if(!is_admin() || !is_numeric($post_id) || get_post_type($post_id) !== 'page'){ return; } // you're now sure you're currently updating a post type "page" in the admin only // wp_update_post(...) }
You could also write it like that, if that’s more clear:
add_action('acf/save_post', 'my_acf_save_post', 20); function my_acf_save_post($post_id){ // stop if front-end if(!is_admin()){ return; } // stop if post_id is not numeric (user, term, options page etc...) if(!is_numeric($post_id)){ return; } // stop if post_type is not "page" if(get_post_type($post_id) !== 'page'){ return; } // you're now sure you're currently updating a post type "page" in the admin only // wp_update_post(...) }
PS: Thanks for the words of encouragement 🙂
Hope it helps!
Have a nice day!
Regards.
well this works very well, I have just updated the $post_id = false; and works fine after returning the wp_update_post( $my_post );
it only updates the current post and not other Post_id as you told.This is such great tip from you, I hadn’t noticed it before.
Thanks for the reply and I wish you continued success.
Ahmad!-
This reply was modified 3 years, 1 month ago by
Ahmad Al Hariri.
Hello,
I’m glad to hear it now works as expected!
Thanks for the nice review 🙂
Have a nice day!
Regards.
-
This reply was modified 3 years, 1 month ago by
- The topic ‘Stop generating pages after form submission’ is closed to new replies.