• Resolved pzh20

    (@pzh20)


    I have a situation where I need to add a post in one form, this includes the post title, content, featured image and a few CPT fields.

    I then want to redirect the user to part 2 of the post entry (so Update Post) where through some Javascript I do some face detection of people in the featured image, and the user enters details of the faces detected.

    I therefore want the first page to submit the post, save it and redirect to page 2, passing it the $post_ID of the post just created and page 2 open this post for edit before saving again.

    How can I achieve this via the Update Post Form?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    Yes, you could do it using two different methods:

    1. Transparent method

    Let’s say you only allow one post per user (ie: a profile page). Then you could create 2 pages:

    • /create-profile
    • /update-profile

    On the /create-profile page you add a form which create a ‘profile’ post type, with the currently logged user as author in the “Post Create Action”. Then add a “Redirect Action” to redirect the user on /update-profile.

    On the /update-profile page, you could retrieve the latest created profile post id of the currently logged user using a query, and then pass the page id to the form. Usage example:

    // get current user profile page
    $get_user_profile_page = get_posts(array(
        'post_type'      => 'profile',
        'posts_per_page' => 1,
        'author'         => get_current_user_id(),
        'fields'         => 'ids'
    ));
    
    // check the page exists
    if($get_user_profile_page){
        
        // retrieve page id
        $profile_page_id = current($get_user_profile_page);
        
        // pass the profile page id to the form
        acfe_form(array(
            'name'            => 'my-form-update',
            'profile_page_id' => $profile_page_id
        ));
        
    }

    That profile page id can then be used in your Form UI using the Template Tag {form:profile_page_id}. You can use it within your Post Action “Update Target” to update that page. See documentation.

    2. Using a param in the redirect URL

    If you allow multiple posts per user, then you’ll have to pass the post id in the URL of the redirection. To do so, on the /create-profile form, in the “Redirect Action” you can use the following URL: /update-profile?profile_id={post:ID}.

    {post:ID} will tell the Redirect Action to use the previous “Post Action” created post. See documentation about Template Tags.

    Then on your /update-profile page you’ll have to retrieve that URL parameter using $_GET, do some security check (make sure the user has the right to edit that ID etc…) and display the update form by passing the profile page id like in the example 1.

    Hope it helps!

    Haver a nice day!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    Konrad, Thanks once again for responding so quickly.

    Do I need to register the parameters beforehand with something like;

    add_action('init','my_register_param');
    function my_register_param() { 
        global $wp; 
        $wp->add_query_var('my_post_Id'); 
    }

    Then do a $_Get(“my_post_Id”);

    As I’m using Elementor as my page builder, I’m adding these code snippets in a Code Widget in the page. The page will already have the form imbedded via a shortcode, so I then do something like

    $my_photo_Id = $_Get("my_post_Id");
      
        // pass the profile page id to the form
        acfe_form(array(
            'name'            => 'my-form-update',
            'my_photo_Id' => $my_photo_Id
        ));

    This I assume would normally be in a template for the 2nd post update page. I’m not sure if this will work inside Elementor.

    Regards
    Pete

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    The query_vars are mainly used to for the permalinks such as /my-page/profile-id/12 for example. You’re not forced to use it if you don’t want to. See query vars documentation.

    In PHP, you can retrieve the parameters in the URL /my-page/?my_post_id=12 using $_GET['my_post_id'] (the uppercase is important). See PHP GET documentation.

    If you’re using the [acfe_form] shortcode, you can show/hide the form using the acfe/form/load hook. In this hook you can also append data, such as the URL parameter my_profile_id. See Integration documentation.

    Usage example:

    add_filter('acfe/form/load/form=my-form-update', 'my_form_update_settings', 10, 2);
    function my_form_update_settings($form, $post_id){
        
        // verify the URL parameter exists
        if(!empty($_GET['my_profile_id'])){
        
            // get url parameter
            $my_profile_id = (int) $_GET['my_profile_id'];
        
            // get profile page author
            $profile_author_id = (int) get_post_field('post_author', $my_profile_id);
        
            // get current logged user
            $current_user_id = (int) get_current_user_id();
            
            // do additional security check if needed
            // remember the visitor can enter any kind of value in the URL parameter!
        
            // if the current user is the profile page author
            if($current_user_id === $profile_author_id){
            
                // append custom data to the form for later use in UI
                $form['my_profile_id'] = $my_profile_id;
            
                // show the form
                return $form;
            
            }
            
        }
        
        // otherwise hide the form
        return false;
        
    }

    Note that WordPress hooks have to be located in your theme’s functions.php file or in a plugin.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    Great, sounds just like what I need. Thanks again

    Thread Starter pzh20

    (@pzh20)

    I’m really struggling with this. I know it’s being called, but instead of the post ID created in the first form, it is passing the ID of the page the first form is on.

    I’m not sure that your example works in my case as I’m dealing with a post and not a profile entry.

    Jus to reiterate, I have a form that creates a post called Photograph. The form includes a photograph and some fields. When it is saved, it redirects to the second form where I want to edit the post just saved in the first form. I’ve modified the example code above but as I say it’s passing the ID of the Page the create post form is on.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    In my example I used my_profile_id, but it was just an example, you can pass anything you want and call it how you want.

    As detailed in my previous answer, this form code invocation:

    acfe_form(array(
        'name' => 'my-form',
        'my_data' => 12
    ));

    Or this hook (if you’re using a shortcode):

    add_filter('acfe/form/load/form=my-form', 'my_form_settings', 10, 2);
    function my_form_settings($form, $post_id){
        
        $form['my_data'] = 12;
        return $form;
        
    }

    Will both pass my_data to the form. Allowing you to retrieve its value (in this example 12) using {form:my_data} anywhere in the Form UI. See screenshot.

    If you’re not really comfortable with PHP (using $_GET, doing WP_Query etc…), then I would recommend to first make a simple test form and train yourself on it. Only use static values first (don’t make queries), don’t chain forms, just do some basic tests.

    Once you made it work with basic values, add more difficulty by doing some queries with get_posts() like in my previous answer, or adding some condition, or chaining test forms between each others.

    So if the form stops working, you pretty much know which step is faulty (when you added the query? when you added the condition? etc…).

    Here is something you can start with:

    // retrive parameter from url: /my-page?param=45
    $url_parameter = $_GET['param'];
    
    // invoke acfe_form() and pass the url parameter
    acfe_form(array(
        'name' => 'my-form',
        'my_data' => $url_parameter // should be 45
    ));

    With this code, you can add {form:my_data} in your Post Action “Update Target” as in the screenshot above. This will tell the Post Action to update the Post ID 45 if you call your form page /my-form?param=45.

    Now if you call your page with this url: /my-form?param=12, the form will update the Post ID 12. Which means now your form can dynamically update any post just by passing a parameter in the URL. You can test it.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    So this is what I have

    add_filter('acfe/form/load/form=add-photo-2', 'my_form_update_settings', 10, 2);
    function my_form_update_settings($form, $post_id){
        
        // verify the URL parameter exists
        if(!empty($_GET['photo_Id'])){
        	write_log('Photo post ID  '.$_GET['photo_Id']);	
            // get url parameter
            $my_photo_Id = (int) $_GET['photo_Id'];
        
            // get profile page author
            $profile_author_id = (int) get_post_field('post_author', $my_photo_Id);
        
            // get current logged user
            $current_user_id = (int) get_current_user_id();
            
            // do additional security check if needed
            // remember the visitor can enter any kind of value in the URL parameter!
        
            // if the current user is the profile page author
            if($current_user_id === $profile_author_id){
            
                // append custom data to the form for later use in UI
                $form['add-photo-2'] = $my_photo_Id;
            
                // show the form
                return $form;
            
            }
            
        }
        
        // otherwise hide the form
        return false;
    }

    The write log writes out 200 which is the ID of the page the first form is on, and this is the URL that I get

    https://the-harrisons.info/add-photo-2/?photo_Id=200 where 200 is the id of the page

    I haven’t really modified your code, only renamed variables etc to make it more like mine.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    So the problem come from the “Redirect Action” from the first form which send the wrong post ID.

    In order to print the Post ID of the previous “Post Action” in a “Redirect Action”, you can use the Template Tag {action:post:ID}. See documentation.

    The Redirect URL setting should looke like: /add-photo-2?photo_Id={action:post:ID}

    • {post:ID} = print the page ID where the form is
    • {action:post:ID} = print the previous “Post Action” ID

    Hope it helps!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    Great Konrad, that sends the correct post ID.

    On the Update Post form, can I show the featured image and other fields from the post but not allow them to be updated?

    Many thanks
    Pete

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    You can display any ACF field values, such as an Image on front-end PHP templates using the native get_field() function (See documentation). You’ll find template usage example for Image field in the ACF documentation here.

    Note that you’ll have to display this image outside of the form (at the top of it or at the bottom), since it’s a custom PHP code.

    If you want to render custom PHP code (such as a generated image here) inside the form, you can use the Dynamic Render field (See documentation). Simply add this field in the Field Group that is mapped in the form, and then you’ll be able to write PHP code that will be rendered in the field using the acf/render_field hook, as explained in the documentation.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    Can I remove fields from the post that I originally put in when I created the form, or do I have to create a new form?

    Many thanks

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Sorry I’m not sure to understand your question. Can you please screenshots/videos showing what you want to achieve?

    Thanks.

    Regards.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Split adding new post over 2 pages’ is closed to new replies.