├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── composer.lock └── gravity-forms-javascript-dynamic-population.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.0.3 / 2018-10-26 4 | 5 | - Remove errant empty line 6 | 7 | ## 1.0.2 / 2018-10-26 8 | 9 | - Require composer/installers 10 | 11 | ## 1.0.1 / 2018-10-26 12 | 13 | - Add license and composer config file 14 | 15 | ## 1.0 / 2018-10-26 16 | 17 | - Initial release 18 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Matt Mirus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gravity Forms JavScript Dynamic Population 2 | 3 | Some caching systems ignore query parameters, preventing Gravity Forms from dynamically populating field values. 4 | 5 | This plugin bypasses that problem by filling Gravity Forms fields that have dynamic population enabled on the client side using JavaScript. 6 | 7 | ## Installation 8 | 9 | There are three options for installing this plugin: 10 | 11 | 1. With composer from [Packagist](https://packagist.org/packages/mmirus/gravity-forms-javascript-dynamic-population): `composer require mmirus/gravity-forms-javascript-dynamic-population` 12 | 2. With [GitHub Updater](https://github.com/afragen/github-updater) 13 | 3. By downloading the latest release ZIP from this repository and installing it like any normal WordPress plugin 14 | 15 | ## Usage 16 | 17 | Once you have the plugin installed and activated, just configure Gravity Forms' built-in dynamic population setting for the desired form fields as you normally would--no extra steps are needed. When values are passed for those form fields in the URL, the plugin's JavaScript will detect them and fill them in. 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mmirus/gravity-forms-javascript-dynamic-population", 3 | "description": "Dynamically populate Gravity Forms fields from query parameters using JavaScript.", 4 | "type": "wordpress-plugin", 5 | "license": "MIT", 6 | "homepage": "https://github.com/mmirus/gravity-forms-javascript-dynamic-population", 7 | "authors": [ 8 | { 9 | "name": "Matt Mirus", 10 | "email": "matt@mattmirus.com", 11 | "homepage": "https://github.com/mmirus" 12 | } 13 | ], 14 | "keywords": [ 15 | "wordpress", 16 | "gravity forms" 17 | ], 18 | "support": { 19 | "issues": "https://github.com/mmirus/gravity-forms-javascript-dynamic-population/issues" 20 | }, 21 | "require": { 22 | "php": ">=5.3.0", 23 | "composer/installers": "^1.6" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "57fb06e9c9a9e83e422aff89202fa7e2", 8 | "packages": [ 9 | { 10 | "name": "composer/installers", 11 | "version": "v1.6.0", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/composer/installers.git", 15 | "reference": "cfcca6b1b60bc4974324efb5783c13dca6932b5b" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/composer/installers/zipball/cfcca6b1b60bc4974324efb5783c13dca6932b5b", 20 | "reference": "cfcca6b1b60bc4974324efb5783c13dca6932b5b", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "composer-plugin-api": "^1.0" 25 | }, 26 | "replace": { 27 | "roundcube/plugin-installer": "*", 28 | "shama/baton": "*" 29 | }, 30 | "require-dev": { 31 | "composer/composer": "1.0.*@dev", 32 | "phpunit/phpunit": "^4.8.36" 33 | }, 34 | "type": "composer-plugin", 35 | "extra": { 36 | "class": "Composer\\Installers\\Plugin", 37 | "branch-alias": { 38 | "dev-master": "1.0-dev" 39 | } 40 | }, 41 | "autoload": { 42 | "psr-4": { 43 | "Composer\\Installers\\": "src/Composer/Installers" 44 | } 45 | }, 46 | "notification-url": "https://packagist.org/downloads/", 47 | "license": [ 48 | "MIT" 49 | ], 50 | "authors": [ 51 | { 52 | "name": "Kyle Robinson Young", 53 | "email": "kyle@dontkry.com", 54 | "homepage": "https://github.com/shama" 55 | } 56 | ], 57 | "description": "A multi-framework Composer library installer", 58 | "homepage": "https://composer.github.io/installers/", 59 | "keywords": [ 60 | "Craft", 61 | "Dolibarr", 62 | "Eliasis", 63 | "Hurad", 64 | "ImageCMS", 65 | "Kanboard", 66 | "Lan Management System", 67 | "MODX Evo", 68 | "Mautic", 69 | "Maya", 70 | "OXID", 71 | "Plentymarkets", 72 | "Porto", 73 | "RadPHP", 74 | "SMF", 75 | "Thelia", 76 | "WolfCMS", 77 | "agl", 78 | "aimeos", 79 | "annotatecms", 80 | "attogram", 81 | "bitrix", 82 | "cakephp", 83 | "chef", 84 | "cockpit", 85 | "codeigniter", 86 | "concrete5", 87 | "croogo", 88 | "dokuwiki", 89 | "drupal", 90 | "eZ Platform", 91 | "elgg", 92 | "expressionengine", 93 | "fuelphp", 94 | "grav", 95 | "installer", 96 | "itop", 97 | "joomla", 98 | "kohana", 99 | "laravel", 100 | "lavalite", 101 | "lithium", 102 | "magento", 103 | "majima", 104 | "mako", 105 | "mediawiki", 106 | "modulework", 107 | "modx", 108 | "moodle", 109 | "osclass", 110 | "phpbb", 111 | "piwik", 112 | "ppi", 113 | "puppet", 114 | "pxcms", 115 | "reindex", 116 | "roundcube", 117 | "shopware", 118 | "silverstripe", 119 | "sydes", 120 | "symfony", 121 | "typo3", 122 | "wordpress", 123 | "yawik", 124 | "zend", 125 | "zikula" 126 | ], 127 | "time": "2018-08-27T06:10:37+00:00" 128 | } 129 | ], 130 | "packages-dev": [], 131 | "aliases": [], 132 | "minimum-stability": "stable", 133 | "stability-flags": [], 134 | "prefer-stable": false, 135 | "prefer-lowest": false, 136 | "platform": { 137 | "php": ">=5.3.0" 138 | }, 139 | "platform-dev": [] 140 | } 141 | -------------------------------------------------------------------------------- /gravity-forms-javascript-dynamic-population.php: -------------------------------------------------------------------------------- 1 | allowsPrepopulate) return $field_content; 15 | return str_replace('name=', "data-prefill='{$field->inputName}' name=", $field_content); 16 | }, 10, 5); 17 | 18 | // fill values from query params for fields where dynamic population is enabled 19 | add_action('gform_register_init_scripts', function ($form) { 20 | $script = " 21 | const getUrlParameter = function(name) { 22 | name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); 23 | var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); 24 | var results = regex.exec(location.search); 25 | return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); 26 | }; 27 | const fields = [].slice.call(document.querySelectorAll('[data-prefill]')); 28 | fields.forEach(function(field) { 29 | const value = getUrlParameter(field.dataset.prefill); 30 | if (value === '') return; 31 | 32 | const fieldType = (field.tagName === 'INPUT') ? field.type : field.tagName.toLowerCase(); 33 | switch (fieldType) { 34 | case 'checkbox': 35 | case 'radio': 36 | field.checked = (value.split(',').indexOf(field.value) !== -1); 37 | break; 38 | case 'select': 39 | jQuery(field).val(value.split(',')); 40 | break; 41 | default: 42 | field.value = value; 43 | } 44 | }); 45 | "; 46 | 47 | GFFormDisplay::add_init_script($form['id'], 'populate_fields', GFFormDisplay::ON_PAGE_RENDER, $script); 48 | }, 10, 2); 49 | --------------------------------------------------------------------------------