Update post?
-
Is there any tutorial how to set up form that a post get updated?
Do i need to set form to the post? Or can i also use url parameter to update post?Best Regards
-
Hello,
Thanks for the feedback.
There’s different way to update a post using ACF Extended: Forms.
– You can display the form using
acfe_form('my-form')
or[acfe_form name="my-form"]
on the actual post you want to update. You’ll have to add an “Post Action” in the form, and set the Action as “Update Post”.In the “Save” tab you can leave the Target on “Current: Post”. In the “Load” tab you can also leave the source as “Current: Post”.
Remember to map the fields you want to Save & Load the data from. Also note that you have to check the ACF fields checkboxes you want to save & load aswell. See screenshots: https://imgur.com/a/b4qNyMg
– You can display the form on a different page (which isn’t related to the post you want to update). Let’s say you created a page
/post-update/
and display the form there. You have to decide how you want to pass the actual Post ID you want to update.You can pass the targeted Post ID in the URL, like that:
/post-update/?my-post=123
. If you do that, then you should use the template tag{request:my-post}
in the “Save Target” & “Load Source” settings (just type it inside). See screenshot: https://i.imgur.com/6PxfBFb.pngThe
{request:my-post}
retrieve the$_REQUEST
PHP variable, which get both$_POST
&$_GET
. In your case, it will retrieve$_GET['my-post']
from the URL.An another way to pass the targeted Post ID, without having it in the URL is to use
set_query_var()
function. Let’s say the current logged user can only update one post, let’s say the post143
, you add the following code in your/post-update/
PHP template:// Add your own logic here to find the good post id $allowed_post_id = 143; // Set Query Var set_query_var('post-update-id', $allowed_post_id); // Display Form acfe_form('my-form');
Then in your form, use the
{query_var:post-update-id}
template tag in theSave Target
&Load Source
settings. See screenshot: https://i.imgur.com/obUy0RG.pngThe template tag
{query_var:post-update-id}
will call the PHP functionget_query_var('post-update-id')
, to retrieve data set before (in the page like in this example).@ianchapman: You should read my answer above, it will help you to find the best way to pass data in your multi-step configuration. If you don’t want to pass the post ID in the URL, then you should go for the
query_var
method.Note: If you want to pass a
query_var
from the Step 1 form to the Step 2 form (let’s say you generate a query var from an action), then you have to leave the “Redirection” setting empty. In fact, redirections will clear all headers, which include any$_POST
orquery_var
data.If the redirection is necessary, then you’ll have to create your own logic with the currently logged user & check which fields has been already saved in the DB to determine which form to display for example.
If it’s alright to leave the redirection empty, then you’ll have access to all the necessary data to display the Step 2 Form. Let’s say you have a page
/multi-forms/
, you can do something like that in the PHP template:// Forms names $form_step_1_name = 'form_step_1'; $form_step_2_name = 'form_step_2'; $form_step_3_name = 'form_step_3'; // Check if form has been submitted if(acf_maybe_get_POST('_acf_form')){ // Get submitted form data $form = json_decode(acf_decrypt($_POST['_acf_form']), true); // If Form Step 1 has been submitted if(acf_maybe_get($form, 'form_name') === $form_step_1_name){ // Display Form Step 2 acfe_form($form_step_2_name); } // Elseif Form Step 2 has been submitted elseif(acf_maybe_get($form, 'form_name') === $form_step_2_name){ // Display Form Step 3 acfe_form($form_step_3_name); } // Elseif Form Step 3 has been submitted elseif(acf_maybe_get($form, 'form_name') === $form_step_3_name){ // Success! echo 'Success!'; } // No Form submitted }else{ // Display Form Step 1 acfe_form($form_step_1_name); }
Hope it helps!
Have a nice day.
Regards.
- The topic ‘Update post?’ is closed to new replies.