dpzbh1779
2018-09-12 21:25
浏览 54
已采纳

从前端表单限制ACF的“acf / validate_save_post”操作

I am using ACF's acf/validate_save_post action hook to validate a serial number from a front-end form with a 3rd party API before the form submits and saves to a post type.

If the call to the 3rd party API returns an error I am using this action hook to display validation errors using the acf_add_validation_error() function.

Due to the acf/validation_save_post action hook being called when a post is published (and before, when doing the JS validation), the serial number ends up being validated 3 times. This ends up locking the user out due to limits set on the API side.

Is there a way to lock the action hook out from running when a post is published and only have it run when it is doing the JS validation?

I have tried using wp_doing_ajax() in an if statement but I think all the work is done via AJAX anyway, unless I am wrong on that. It still appears to run more than once while using wp_doing_ajax() anyway so maybe there is another solution out there.

图片转代码服务由CSDN问答提供 功能建议

我正在使用ACF的 acf / validate_save_post 动作挂钩来验证前面的序列号 -end表单在表单提交并保存到帖子类型之前使用第三方API。

如果对第三方API的调用返回错误,我将使用此操作挂钩显示验证 使用 acf_add_validation_error()函数的错误。

由于发布帖子时调用 acf / validation_save_post 动作挂钩(和 在进行JS验证之前,序列号最终被验证3次。 由于在API端设置了限制,这最终会锁定用户。

是否有办法在发布帖子时将动作挂钩锁定为不运行,并且仅在运行时运行 它正在进行JS验证吗?

我尝试在 if 语句中使用 wp_doing_ajax(),但我认为所有的工作都是 无论如何,通过AJAX完成,除非我错了。 无论如何,当使用 wp_doing_ajax()时,它似乎仍然运行不止一次,所以可能还有另一个解决方案。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanmu2941 2018-09-14 17:51
    已采纳

    In case anyone finds this in the future, the solution to the issue is to check if this is an AJAX request via wp_doing_ajax(). This will stop validation while publishing the post via the admin or the acf/save_post action hook.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题