This tutorial/snippet expands on the Set Post Status by Field Value snippet. Here is a refresher on the issue:
Currently, the post status of a post generated from a Gravity Form submission is set via the Post Title, Post Body or Post Excerpt fields. So what do you do if you want the post status to be based on some form of user input? It’s a form-specific option (as opposed to field specific) which means Gravity Forms’ powerful conditional logic can not help us here.
The original snippet demonstrated the simplest approach and relied on the field values being set as valid post statuses. This snippet allows you to format the field values/labels however you want and then map which field values correspond to which post statuses in the code.
/*** Set Post Status by Field Value (Advanced)* http://gravitywiz.com/2012/05/04/set-post-status-by-field-value-advanced/*/
// update "3" to the ID of your formadd_filter('gform_post_data_3', 'gform_dynamic_post_status', 10, 3);function gform_dynamic_post_status($post_data, $form, $entry) { // update "5" to the ID of your custom post status field if($entry[5]) { switch($entry[5]) { case 'Yes, please review my post.': $post_data['post_status'] = 'pending'; break; case 'No, please publish my post.': $post_data['post_status'] = 'publish'; break; } } return $post_data;}How do I install this snippet?
Use a simple copy and paste spell to copy the snippet above and paste it in your theme’s functions.php file.
Do I need to modify the snippet to work for my form?
Yes. Make note of the comments inline for pointers on where you will need to make your modifications.
- Update the
3in the filter namegform_post_data_3to the ID of your own form. - Update the
5in the two places you see$entry[5]to your field ID. - Update each case statement (ie
case 'Yes, please review my post.':) to match one of your field value options. - Update each
$post_data['post_status'] = 'your_post_status'assignment to equal the correct post status for the corresponding field value.
With this snippet you can map any field value to any post status. This snippet would work with a field configured like so:
Set Post Status by Field Value (Advanced)Even More Advanced
At the risk of exhausting this topic (wizards like to be thorough!), I want to share one more example of setting the post status by field value: setting the post status by a Product field value.
/*** Set Post Status by Product Field Value* http://gravitywiz.com/2012/05/04/set-post-status-by-field-value-advanced/*/
// update "3" to the ID of your formadd_filter('gform_post_data_3', 'gform_dynamic_post_status', 10, 3);function gform_dynamic_post_status($post_data, $form, $entry) { // update "6" to the ID of your custom post status field if($entry[6]) { $values = explode('|', $entry[6]); switch($values[0]) { case 'Basic Package': $post_data['post_status'] = 'draft'; break; case 'Premium Package': $post_data['post_status'] = 'publish'; break; } } return $post_data;}Usage
You can follow the same installation and modification instructions above. The only difference is this little line of code.
$values = explode('|', $entry[6]);
A product field value is stored as a pipe (|) delimited list of values. The first value will be the product label or, if values are available and enabled for the Product field type (ie Drop Down Product Fields), the label will be replaced by the value as the first value. The second value will be the price.
| Format template | $entry[6] = 'label/value|price' |
| Format example w/ values enabled | $entry[6] = 'basic|0' |
| Format example w/ values not enabled | $entry[6] = 'Basic Package|0' |
For this example, we just want the label. We use the PHP explode() function to “split” the combined string into separate pieces where ever there is a pipe. This gives us an array of $values.
| Before Explode | $values = 'Basic Package|0' |
| After Explode | |
We know that the product option label will be the first value in our $values array because it was the first value in the combined product value string. An array’s index starts at 0 so we can assume that $values[0] will give us the label of the selected product option.
With that in mind, we can create a switch case (just like we did in the previous snippet) and set the post status dynamically based on which product option was selected.
switch($values[0]) {case 'Basic Package': $post_data['post_status'] = 'draft'; break;case 'Premium Package': $post_data['post_status'] = 'publish'; break;}Here is an example of how the Product field might be configured:
Set Post Status by Product Field ValueSummary
Whew! That’s a lot to take in. I think I’m going to go have a rest in my tower. If you have any questions, just ask!
Comments