Data, Maps, Usability, and Performance

Editing Gravity Forms Entries on The Front-End

Last updated on July 10, 2015 in Development

edit gravity forms on frontend

If you are using WordPress and the Gravity Forms plugin, you might want to allow users to edit their posts or entries on the front-end. This is not supported by the plugin but there are other plugins or addons like Gravity Forms Edit Entries, Gravity Forms Sticky List, Gravity Forms: Post Updates, and GravityView that provide such functionality. But, they also come with other features that you might not want or need and installing more and more plugins can certainly impact performance. Moreover, a few of the plugins that I have tried would create and delete entries in GravityFroms causing missing Entry IDs which isn’t ideal and it is additional work on the system. Today, I will show you how to edit Gravity Forms entries on a page by simply using GF actions, hooks, and the API.

Rewriting frontend and backend validation is a pain so let’s embed a new GF form on a page as it has all the validation in place. We use the function call in a page template file because it allows us to prefill the form using $field_values. You can use a query parameter in the page URL to specify your existing entry ID, pull the contents via GFAPI::get_entry(); and then we use the function call mentioned above and fill the form values with the $field_values parameter.

Submitting this form will create a new entry but we can easily fix that by dropping a conditional in gform_pre_submission action hook which gets run after form validation but before the entry is created and stored in the database. We could hook into page name by using an if statement that checks what page was the form submitted from via $_SERVER["REDIRECT_URL"]. So, if the page is “create entry”, we continue but if the page is “edit entry”, we look at what has been submitted via $_POST, use the values to update the entry via GFAPI::update_entry(), redirect via PHP header() call and kill the process via die(). The explanation seems long but the code is quite concise.

Here is a code example, and it also shows how to unserialize a Gravity Forms List Field:

Tags: , ,

Facebook Twitter Hacker News Reddit More...