Gravity Forms + Custom Post Types - Version 1.0

Version Description

  • Initial Relase. First version.

=

Download this release

Release Info

Developer bradvin
Plugin Icon wp plugin Gravity Forms + Custom Post Types
Version 1.0
Comparing to
See all releases

Version 1.0

gravity-forms-custom-post-type-mappings.php ADDED
@@ -0,0 +1,168 @@
1
+ <?php
2
+ /*
3
+ Plugin Name: Gravity Forms Custom Post Type Mappings
4
+ Plugin URI: http://themergency.com/
5
+ Description: Allow simple way to map a Gravity Form post entry to a custom post type
6
+ Version: 1.0
7
+ Author: Brad Vincent
8
+ Author URI: http://themergency.com/
9
+ License: GPL2
10
+ */
11
+
12
+ // Get Gravity Forms over here!
13
+ @include_once(WP_PLUGIN_DIR . "/gravityforms/gravityforms.php");
14
+ @include_once(WP_PLUGIN_DIR . "/gravityforms/forms_model.php");
15
+
16
+ // check if Gravity Forms is installed and exists
17
+ if(class_exists('RGForms') && class_exists('RGFormsModel')) {
18
+
19
+ if (!class_exists('GF_CustomPostTypeMappings')) {
20
+
21
+ class GF_CustomPostTypeMappings {
22
+
23
+ private $_plugin_name; //the name of the plugin
24
+
25
+ //PHP 4 constructor
26
+ function GF_CustomPostTypeMappings() {
27
+ $this->__construct();
28
+ }
29
+
30
+ function __construct() {
31
+ $this->init();
32
+ }
33
+
34
+ function gf_populate_taxonomy_dropdowns($form) {
35
+ foreach($form['fields'] as &$field) {
36
+ $classes = explode(' ', $field['cssClass']);
37
+
38
+ foreach($classes as $class) {
39
+ //check if the class starts with 'taxomony-'
40
+ if($this->starts_with($class, 'taxonomy-')) {
41
+ $field['type'] = 'select';
42
+ $taxonomy = str_replace('taxonomy-', '', $class);
43
+ $field['choices'] = $this->gf_get_taxonomy_dropdown_choices($taxonomy);
44
+ }
45
+ }
46
+ }
47
+
48
+ return $form;
49
+ }
50
+
51
+ function gf_get_taxonomy_dropdown_choices($taxonomy, $withEmpty = true) {
52
+ $terms = get_terms($taxonomy, 'hide_empty=0');
53
+
54
+ $items = array();
55
+
56
+ if($withEmpty) {
57
+ $items[] = array('text' => '--select--', 'value' => '');
58
+ }
59
+
60
+ foreach($terms as $term) {
61
+ $items[] = array('value' => $term->term_id, 'text' => $term->name);
62
+ }
63
+
64
+ return $items;
65
+ }
66
+
67
+ function gf_set_post_type($post_data, $form){
68
+ //check if the form we are dealing with saves a post
69
+ if($this->gf_is_form_a_post_form($form)){
70
+ $classes = explode(' ', $form['cssClass']);
71
+
72
+ //loop through the classes and see if we have set the posttype class
73
+ foreach($classes as $class) {
74
+ //check if the class starts with 'posttype-'
75
+ if($this->starts_with($class, 'posttype-')) {
76
+ $target_post_type = str_replace('posttype-', '', $class);
77
+ }
78
+ }
79
+
80
+ if (!isset($target_post_type)) {
81
+ //lastly check if we have saved the mapping in an option
82
+ $options = get_option( $this->_plugin_name );
83
+
84
+ $target_post_type = $options["gf_".$form["id"]];
85
+ }
86
+
87
+ if (isset($target_post_type))
88
+ $post_data["post_type"] = $target_post_type;
89
+ }
90
+ return $post_data;
91
+ }
92
+
93
+ function gf_is_form_a_post_form($form) {
94
+ foreach ($form["fields"] as $field) {
95
+ if(in_array($field["type"],
96
+ array("post_category","post_title","post_content",
97
+ "post_excerpt","post_tags","post_custom_fields","post_image")))
98
+ return true;
99
+ }
100
+ return false;
101
+ }
102
+
103
+ function gf_save_taxonomy($lead, $form) {
104
+ // Check if the submission contains a WordPress post
105
+ if(isset($lead['post_id'])) {
106
+
107
+ foreach($form['fields'] as &$field) {
108
+ $classes = explode(' ', $field['cssClass']);
109
+
110
+ foreach($classes as $class) {
111
+ //check if the class starts with 'taxomony-'
112
+ if($this->starts_with($class, 'taxonomy-')) {
113
+ $taxonomy = str_replace('taxonomy-', '', $class);
114
+ $term_id = (int) $lead[$field['id']];
115
+ wp_set_object_terms($lead['post_id'], $term_id, $taxonomy);
116
+ }
117
+ }
118
+ }
119
+ }
120
+ }
121
+
122
+ /**
123
+ * starts_with
124
+ * Tests if a text starts with an given string.
125
+ *
126
+ * @param string
127
+ * @param string
128
+ * @return bool
129
+ */
130
+ function starts_with($haystack, $needle){
131
+ return strpos($haystack, $needle) === 0;
132
+ }
133
+
134
+ /*
135
+ * returns true if a needle can be found in a haystack
136
+ */
137
+ function str_contains($haystack, $needle) {
138
+ if (empty($haystack) || empty($needle))
139
+ return false;
140
+
141
+ $pos = strpos(strtolower($haystack), strtolower($needle));
142
+
143
+ if ($pos === false)
144
+ return false;
145
+ else
146
+ return true;
147
+ }
148
+
149
+ function init() {
150
+ $this->_plugin_name = 'gravity-forms-custom-post-type-mapping';
151
+
152
+ //alter the way forms are rendered by inserting taxomony dropdowns if needed
153
+ add_filter ('gform_pre_render' , array(&$this, 'gf_populate_taxonomy_dropdowns') );
154
+
155
+ //set the post types for a form
156
+ add_filter("gform_post_data", array(&$this, 'gf_set_post_type'), 10, 2);
157
+
158
+ //intercept the form save and save any taxonomy links if needed
159
+ add_action('gform_post_submission', array(&$this, 'gf_save_taxonomy'), 10, 2);
160
+ }
161
+ }
162
+
163
+ add_action("init",
164
+ create_function('', "global \$gfcpt; \$gfcpt = new GF_CustomPostTypeMappings();"));
165
+ }
166
+ }
167
+
168
+ ?>
readme.txt ADDED
@@ -0,0 +1,54 @@
1
+ === Gravity Forms + Custom Post Types ===
2
+ Contributors: bradvin
3
+ Donate link: http://themergency.com/donate/
4
+ Tags: form,forms,gravity,gravity form,gravity forms,CPT,custom post types,custom post type,taxonomy,taxonomies
5
+ Requires at least: 3.0
6
+ Tested up to: 3.1-beta
7
+ Stable tag: trunk
8
+
9
+ Easily map your forms that create a post to a custom post type. Also map dropdown fields to custom taxonomies.
10
+
11
+ == Description ==
12
+
13
+ > This plugin is an add-on for the [Gravity Forms Plugin](http://bit.ly/getgravityforms "visit the Gravity Forms website").
14
+ > If you don't yet own a license of the best forms plugin for WordPress, go and [buy one now!](http://bit.ly/getgravityforms "purchase Gravity Forms!")
15
+
16
+ Gravity forms allows you to create posts from a form using post fields. The problem I found was that you cannot get the form to save to a custom post type without writing some php code. I wanted a way to easily map a form to a custom post type so that it would automatically save to the chosen custom post type. All you need to do is add a CSS Class Name of **"posttype-YOUR_POST_TYPE"** to the form and your work is done.
17
+
18
+ Eg. adding a CSS class name of "posttype-movies" will save the post to the custom post type of "movies".
19
+
20
+ I also wanted to link a dropdown to a custom taxonomy, so when the form is displayed, the dropwdown is populated with the terms from that custom taxonomy. And then when the post (or custom post type) is saved, it automatically links the post to the selected taxonomy term. All you need to do is add a CSS class of **"taxonomy-YOUR_TAXONOMY"** to the field.
21
+
22
+ Eg. adding a CSS class name of "taxonomy-actors" will link the dropdown to the "actors" custom taxonomy.
23
+
24
+ In summary, the plugin has the following features:
25
+
26
+ * Map a form to a custom post type
27
+ * Map a form dropdown field to a custom taxonomy
28
+
29
+ == Installation ==
30
+
31
+ 1. Upload the plugin folder 'gravity-forms-custom-post-types' to your `/wp-content/plugins/` folder
32
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
33
+ 3. Set the CSS class names of the form or a dropdown field
34
+
35
+ == Screenshots ==
36
+
37
+ 1. How to map a form to a custom post type.
38
+ 2. How to map a dropdown field to a custom taxonomy.
39
+
40
+ == Changelog ==
41
+
42
+ = 1.0 =
43
+ * Initial Relase. First version.
44
+
45
+ == Frequently Asked Questions ==
46
+
47
+ = Does this plugin rely on anything? =
48
+ Yes, you need to install the Gravity Forms plugin for this plugin to work.
49
+
50
+ = How do I setup the mappings between a form and a custom post type? =
51
+ You need to set the CSS Class Name for a form to be "posttype-YOURPOSTTYPEHERE". (see screenshots)
52
+
53
+ = How do include a dropdown with a list of taxonomy terms in my form? =
54
+ You need to set the CSS Class Name of a dropdown field to be "taxonomy-YOURTAXONOMYHERE" (see screenshots)
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file