• Resolved rcwdm

    (@rcwgsy)


    If the slug is generated from a field in the form, how can we force the slug to be unique instead of adopting a numerical append.

    eg: If user enter /example slug and it already exists, it will automatically post the form and create /example-1, instead I’d prefer it doesn’t allow the user to submit the form unless they choose a unique slug.

    So every time a user submits a form, they will get a unique slug of their choosing.

    • This topic was modified 3 years, 4 months ago by rcwdm.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    ACF Extended Forms “Post Action” use the native WordPress wp_insert_post() (when creating a post) and wp_update_post() (when updating a post) functions behind the scene.

    Unfortunately, you don’t really have the control over the slug using numbers when it already exist, since it is tied to the native WordPress logic. You could try to make some research on the web, I never had that case.

    The best approach is, as you said, to validate the slug field to make sure it is unique before submitting the form. You can simply use the native acf/validate_value hook to validate the field value. See documentation.

    In that hook you could check if the value (the slug) already exists in the database for the said post type, using the native get_page_by_path() function and return an error if that’s the case. See documentation.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter rcwdm

    (@rcwgsy)

    Thanks for this. Again, I just don’t understand what I am looking at.

    Not sure why I struggle to grasp this stuff but, I wouldn’t know what to do with that information.

    I’ve asked ACF directly too and they aren’t too sure what will work with the ACF extended plugin itself.

    If you were to validate a field to make sure it is a unique slug, display a custom error message if the slug already exists, or publish the post with the validated unique slug. How would you construct that code to do this?

    Thread Starter rcwdm

    (@rcwgsy)

    Hi, I’ve got this to work…but…

    So it validates the slug before allowing the user to submit the post.

    The problem occurs when they come to update the form on the frontend — as the validation checks once again and won’t allow the update to happen as it’s treating it as a new post.

    Is there a way to remove the ACF field from the ‘update post form’ that was completed and validated already in the ‘create post form’? But still allow the user to update other fields?

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    I’m glad to hear that you managed to make it work. In fact, as explained in my answer, the acf/validate_value hook will work for both back-end and front-end ACF Form.

    Regarding your question, to remove the “Slug” field from the “Update Post Form”, you have multiple solutions:

    – If you decide to use the same Field Group in both “Create Post Form” & “Update Post Form”, then you can render specific fields only in the “Update Post Form” using the “Overrride Form render” setting under the “HTML” tab of the form setting. See screenshot.

    Note that you’ll need to manually list all your fields using the {field:field_name} or {field:field_12345abce} template tags, and of course not adding your “Slug” field in order to hide it.

    – An another solution, if you decide to keep using the same Field Group in both Forms, is to use the acf/prepare_field hook (See documentation) and return false; to hide the field based on the current page (for example if your form is displayed on a page called /form-update)

    – The last solution would be to use one Field Group for the “Create Post Form”, and a different Field Group (without the “Slug” field) for the “Update Post Form”.

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter rcwdm

    (@rcwgsy)

    Thank you. Sorted.

    I split fields into different forms, so the validation form was not longer present when updating the CPT.

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Great to hear!

    If you enjoy this plugin and its support, feel free to submit a review. It always helps and its much appreciated ??

    Have a nice day!

    Regards.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Force unique slug on form submit’ is closed to new replies.