{"id":547,"date":"2019-11-04T19:48:32","date_gmt":"2019-11-04T18:48:32","guid":{"rendered":"https:\/\/awhitepixel.com\/?p=547"},"modified":"2020-07-25T12:00:36","modified_gmt":"2020-07-25T10:00:36","slug":"gravity-forms-coding-introduction","status":"publish","type":"post","link":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/","title":{"rendered":"Coding for Gravity Forms: An Introduction"},"content":{"rendered":"\n<p><a rel=\"noreferrer noopener\" aria-label=\"Gravity Forms (opens in a new tab)\" href=\"https:\/\/www.gravityforms.com\/\" target=\"_blank\">Gravity Forms<\/a> is perhaps the most well-known and best form builder plugin for WordPress. Not only is it easy to use and has plenty of add-ons for implementing payment or newsletter into your forms. But it&#8217;s also very developer-friendly and flexible. Personally I&#8217;ve been using and developing with Gravity Forms for at least 5 years &#8211; and I&#8217;ve never had a feature that wasn&#8217;t possible or really difficult to develop with Gravity Forms.<\/p>\n\n\n\n<p>Keep in mind that Gravity Forms is not free. You have to purchase a license. They offer several different types with different prices in order to get and use it. Once you&#8217;ve purchased a license you also get access to a selection of their add-ons, depending on which license you purchase.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gravity Forms for us developers<\/h2>\n\n\n\n<p>Gravity Forms offers a lot of filters and hooks, but there are no templates that you can override. The hooks offers a lot of flexibility for developers to extend and modify Gravity Forms&#8217;s behaviour. Take a look at <a rel=\"noreferrer noopener\" aria-label=\"Gravity Forms' developers documentation pages (opens in a new tab)\" href=\"https:\/\/docs.gravityforms.com\/category\/developers\/\" target=\"_blank\">Gravity Forms&#8217; developers documentation pages<\/a>. Most, if not all, customizations will be done onto some of Gravity Forms&#8217; objects; either directly or through Gravity Forms API (<code>GFAPI<\/code>) class. You can also interact with Gravity Forms using REST API (which is extending <a rel=\"noreferrer noopener\" href=\"https:\/\/developer.wordpress.org\/rest-api\/\" target=\"_blank\">WordPress&#8217; REST API<\/a>).<\/p>\n\n\n\n<p>Example possibilities for developers are modifying field outputs, changing field&#8217;s values before they get saved as a lead, and programatically adjust email notifications. Other examples include adding custom form settings, creating new field types, and injecting custom fields or field values into existing forms.<\/p>\n\n\n\n<p>Gravity Forms has an &#8220;Add-Ons&#8221; framework that has allowed other developers to create Add-Ons to Gravity Forms of their own. And quite a few has been made. Examples of Add-Ons are MailChimp signup, payment gateways with Stripe, Hubspot integration, Polls, Quiz, Zapier integration, and Dropbox. Which Add-Ons are available depends on your license. Take a look at an <a rel=\"noreferrer noopener\" aria-label=\"look here (opens in a new tab)\" href=\"https:\/\/www.gravityforms.com\/add-ons\/\" target=\"_blank\">overview here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Rendering a form<\/h2>\n\n\n\n<p>Outputting a form is done using shortcodes, e.g.:<\/p>\n\n\n\n<pre class=\"prettyprint\">[gravityform id=\"2\" name=\"Contact us\" ajax=\"true\"]\n<\/pre>\n\n\n\n<p>Don&#8217;t worry, Gravity Forms will add buttons and dialogs for user-friendly embedding forms in your posts. You or the content editors won&#8217;t be required to manually type shortcodes. <\/p>\n\n\n\n<p>But if you as a developer ever needs to embed a form programatically inside a template, all you need to know is the form ID and then simply echo the shortcode. When we output a shortcode in PHP we need to wrap it inside <code>do_shortcode()<\/code> so that WordPress converts the shortcode properly. Gravity Forms will handle adding all necessary scripts and styles for you even when you render a form with PHP.<\/p>\n\n\n\n<pre class=\"prettyprint\">echo do_shortcode('[gravityform id=\"2\" name=\"Contact us\" ajax=\"true\"]');\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Fetching form information<\/h2>\n\n\n\n<p>There are simple API functions available for fetching information, e.g. getting all forms or one specific form object. The objects are populated with all settings including all fields in the form, and you can also get all the form&#8217;s entries (form answers). Say you want to generate a <code>&lt;select&gt;<\/code> with all published forms offering the user to select between published forms:<\/p>\n\n\n\n<pre class=\"prettyprint linenums\">\/\/ ...\n$all_forms = GFAPI::get_forms();\nif (!empty($all_forms)) {\n\t$select = '&lt;select&gt;';\n\tforeach ($all_forms as $form) {\n\t\t$select .= '&lt;option value=\"' . $form['id'] . '\"&gt;' . $form['title'] . '&lt;\/option&gt;';\n\t}\n\t$select .= '&lt;\/select&gt;';\n}\necho $select;\n\/\/ ...\n<\/pre>\n\n\n\n<p>To follow good coding standards, especially considering Gravity Forms is a plugin that can be deactivated or simply not exist, always check if any of Gravity Forms&#8217; classes exist before you use them! Before the above code I would add:<\/p>\n\n\n\n<pre class=\"prettyprint linenums\">if (!class_exists('GFAPI')) {\n\treturn;\n}\n\/\/ Do stuff with GFAPI class\n<\/pre>\n\n\n\n<p>For retrieving a specific form&#8217;s object all you need is the form ID (1 in the below case):<\/p>\n\n\n\n<pre class=\"prettyprint\">$form = GFAPI::get_form(1);\n<\/pre>\n\n\n\n<p>You can even manipulate the form through the object, and then update it, for example changing the form&#8217;s title:<\/p>\n\n\n\n<pre class=\"prettyprint linenums\">$form = GFAPI::get_form(1);\n$form['title'] = __('New Form Title', 'txtdomain');\nGFAPI::update_form($form);\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Retrieving a form&#8217;s leads (answers)<\/h2>\n\n\n\n<p>Retrieving a form&#8217;s entries (form answers) for the form ID 1 is as simple as:<\/p>\n\n\n\n<pre class=\"prettyprint\">$entries = GFAPI::get_entries(1);\n<\/pre>\n\n\n\n<p>You can also retrieve a specific entry with its ID, and in the same fashion as updating the form, you can manipulate the entry object and call an update function on it to save it with your changes. The example below saves an empty string on the entry&#8217;s IP address meta, as well as the value of the field ID 2:<\/p>\n\n\n\n<pre class=\"prettyprint linenums\">$entry_to_change = GFAPI::get_entry(42);\n$entry_to_change['ip'] = '';\n$entry_to_change['2'] = 'New value';\nGFAPI::update_entry($entry_to_change);\n<\/pre>\n\n\n\n<p>All of the above is modifications on the very basic level. Stay tuned in the <a href=\"https:\/\/awhitepixel.com\/blog\/category\/gravity-forms\/\">Gravity Forms category<\/a> for more tutorials and more complex code functionality.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gravity Forms is perhaps the most well-known and best form builder plugin for WordPress. Not only is it easy to use and has plenty of add-ons for implementing payment or newsletter into your forms. But it&#8217;s also very developer-friendly and flexible. Personally I&#8217;ve been using and developing with Gravity Forms for at least 5 years [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":563,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":false,"footnotes":""},"categories":[28],"tags":[22],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Coding for Gravity Forms: An Introduction - A White Pixel<\/title>\n<meta name=\"description\" content=\"In this guide for beginner developers we&#039;ll look at the basics of how to code for Gravity Forms in WordPress. We&#039;ll briefly look at simple code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Coding for Gravity Forms: An Introduction - A White Pixel\" \/>\n<meta property=\"og:description\" content=\"In this guide for beginner developers we&#039;ll look at the basics of how to code for Gravity Forms in WordPress. We&#039;ll briefly look at simple code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/\" \/>\n<meta property=\"og:site_name\" content=\"A White Pixel\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-04T18:48:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-25T10:00:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/awhitepixel.com\/wp-content\/uploads\/2019\/10\/featured-gravityforms.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"1148\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"AWhitePixel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"AWhitePixel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/\"},\"author\":{\"name\":\"AWhitePixel\",\"@id\":\"https:\/\/awhitepixel.com\/#\/schema\/person\/35e73a52e02ff0446ec9cadae3dd4072\"},\"headline\":\"Coding for Gravity Forms: An Introduction\",\"datePublished\":\"2019-11-04T18:48:32+00:00\",\"dateModified\":\"2020-07-25T10:00:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/\"},\"wordCount\":690,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/awhitepixel.com\/#organization\"},\"keywords\":[\"plugin customization\"],\"articleSection\":[\"Gravity Forms\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/\",\"url\":\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/\",\"name\":\"Coding for Gravity Forms: An Introduction - A White Pixel\",\"isPartOf\":{\"@id\":\"https:\/\/awhitepixel.com\/#website\"},\"datePublished\":\"2019-11-04T18:48:32+00:00\",\"dateModified\":\"2020-07-25T10:00:36+00:00\",\"description\":\"In this guide for beginner developers we'll look at the basics of how to code for Gravity Forms in WordPress. We'll briefly look at simple code examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/awhitepixel.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Coding for Gravity Forms: An Introduction\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/awhitepixel.com\/#website\",\"url\":\"https:\/\/awhitepixel.com\/\",\"name\":\"A White Pixel\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/awhitepixel.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/awhitepixel.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/awhitepixel.com\/#organization\",\"name\":\"A White Pixel\",\"url\":\"https:\/\/awhitepixel.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/awhitepixel.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/awhitepixel.com\/wp-content\/uploads\/2020\/07\/cta-fox-wordpress-computer-blue.png\",\"contentUrl\":\"https:\/\/awhitepixel.com\/wp-content\/uploads\/2020\/07\/cta-fox-wordpress-computer-blue.png\",\"width\":365,\"height\":302,\"caption\":\"A White Pixel\"},\"image\":{\"@id\":\"https:\/\/awhitepixel.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/awhitepixel.com\/#\/schema\/person\/35e73a52e02ff0446ec9cadae3dd4072\",\"name\":\"AWhitePixel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/awhitepixel.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/54b82f3f4c246724797e2a5bfeffa27f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/54b82f3f4c246724797e2a5bfeffa27f?s=96&d=mm&r=g\",\"caption\":\"AWhitePixel\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Coding for Gravity Forms: An Introduction - A White Pixel","description":"In this guide for beginner developers we'll look at the basics of how to code for Gravity Forms in WordPress. We'll briefly look at simple code examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/","og_locale":"en_US","og_type":"article","og_title":"Coding for Gravity Forms: An Introduction - A White Pixel","og_description":"In this guide for beginner developers we'll look at the basics of how to code for Gravity Forms in WordPress. We'll briefly look at simple code examples.","og_url":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/","og_site_name":"A White Pixel","article_published_time":"2019-11-04T18:48:32+00:00","article_modified_time":"2020-07-25T10:00:36+00:00","og_image":[{"width":2000,"height":1148,"url":"https:\/\/awhitepixel.com\/wp-content\/uploads\/2019\/10\/featured-gravityforms.jpg","type":"image\/jpeg"}],"author":"AWhitePixel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"AWhitePixel","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/#article","isPartOf":{"@id":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/"},"author":{"name":"AWhitePixel","@id":"https:\/\/awhitepixel.com\/#\/schema\/person\/35e73a52e02ff0446ec9cadae3dd4072"},"headline":"Coding for Gravity Forms: An Introduction","datePublished":"2019-11-04T18:48:32+00:00","dateModified":"2020-07-25T10:00:36+00:00","mainEntityOfPage":{"@id":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/"},"wordCount":690,"commentCount":0,"publisher":{"@id":"https:\/\/awhitepixel.com\/#organization"},"keywords":["plugin customization"],"articleSection":["Gravity Forms"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/","url":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/","name":"Coding for Gravity Forms: An Introduction - A White Pixel","isPartOf":{"@id":"https:\/\/awhitepixel.com\/#website"},"datePublished":"2019-11-04T18:48:32+00:00","dateModified":"2020-07-25T10:00:36+00:00","description":"In this guide for beginner developers we'll look at the basics of how to code for Gravity Forms in WordPress. We'll briefly look at simple code examples.","breadcrumb":{"@id":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/awhitepixel.com\/gravity-forms-coding-introduction\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/awhitepixel.com\/"},{"@type":"ListItem","position":2,"name":"Coding for Gravity Forms: An Introduction"}]},{"@type":"WebSite","@id":"https:\/\/awhitepixel.com\/#website","url":"https:\/\/awhitepixel.com\/","name":"A White Pixel","description":"","publisher":{"@id":"https:\/\/awhitepixel.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/awhitepixel.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/awhitepixel.com\/#organization","name":"A White Pixel","url":"https:\/\/awhitepixel.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/awhitepixel.com\/#\/schema\/logo\/image\/","url":"https:\/\/awhitepixel.com\/wp-content\/uploads\/2020\/07\/cta-fox-wordpress-computer-blue.png","contentUrl":"https:\/\/awhitepixel.com\/wp-content\/uploads\/2020\/07\/cta-fox-wordpress-computer-blue.png","width":365,"height":302,"caption":"A White Pixel"},"image":{"@id":"https:\/\/awhitepixel.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/awhitepixel.com\/#\/schema\/person\/35e73a52e02ff0446ec9cadae3dd4072","name":"AWhitePixel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/awhitepixel.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/54b82f3f4c246724797e2a5bfeffa27f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/54b82f3f4c246724797e2a5bfeffa27f?s=96&d=mm&r=g","caption":"AWhitePixel"}}]}},"_links":{"self":[{"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/posts\/547"}],"collection":[{"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":8,"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":1416,"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/posts\/547\/revisions\/1416"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/media\/563"}],"wp:attachment":[{"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/awhitepixel.com\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}