Gravity Forms & Rest Api Fix

I looked for a fix to this problem but came up completely empty.

Here’s the scenario you are using Gravity Forms on your site. You are rendering your posts with the Rest API. You have a gravity form getting displayed in a post using a short code or the gutenberg plugin.

When you submit a form you get a 401 error that says “Sorry, you are not allowed to edit this post.” With a code of rest_cannot_edit.

Every tip, article, github issue, stack overflow question led me nowhere. Then I compared the form our client was seeing on their posts to what a healthy, working gravity form looks like.

Heres what a bad form looks like

Heres what a good form looks like

The problem area is in the action. When a post is pulled through the Rest API with a Gravity Form in it – the action on the form gets overridden. Unfortunately with Gravity Forms I didn’t see a hook where this behavior could be overridden.

So I came up with a little hack with a couple lines of javascript.

We are using vue.js on the project where I wrote this fix. So conveniently all I had to do what grab the slug of the current post from state. Depending on your use case you might need to figure out a different way to grab the slug. You’ll also want to make sure that the DOM has been completely loaded before running the function. Lastly make sure you set the ajax option on the shortcode in the post to false.

Here is a really neutered version of the code where we are using this.

Join the Conversation

2 Comments

    1. That would be amazing. I’ll have to try that hook. I probably glossed right over that when when reviewing the hook documentation. Thanks for following up!

Leave a comment

Your email address will not be published. Required fields are marked *