I need a way to make a checkbox field out of a post and meta data. Basically pulling all posts from a category, showing title, excerpt, featured image and an extra field. Tell me it can be done:)
I need a way to make a checkbox field out of a post and meta data. Basically pulling all posts from a category, showing title, excerpt, featured image and an extra field. Tell me it can be done:)
Would the resulting checkbox be part of the Gravity Form?
Yes.. So in specific... post in category is a vendor name of the wholesale distributor. I want to show the list of vendors (with hopefully a bit of html around it) so that the user can check which vendors they want to get catalogs from...
Here is a rough bit of code from a side project I did that will help you started: http://pastie.org/1285749
add_action("gform_pre_render_12", "filter_lead_categories");
Update the 12 to your form id.
$posts = new WP_Query(array('category__and' => $catnames, 'posts_per_page' => 20));
Customize the query to get the posts you want to display as checkboxes.
$i = 0;
foreach($posts->posts as $post) {
$companies[$i]['text'] = '<a href="' . get_permalink($post->ID) . '" target="_blank">' . $post->post_title . '</a>';
$companies[$i]['value'] = $post->ID;
$i++;
}
This bit of code loops through the posts you queried and creates an array of choices that we will use to replace the existing choices for the field these checkboxes should appear in. By default, Gravity Forms will strip out HTML from the "text" property, but by adding an additional filter (gform_field_choices), we can allow HTML.
$form['fields'][1]['choices'] = $companies;
Update the 1 to the index of the field you would like to replace the choices with. You can view the page source to get this id.
add_filter("gform_field_choices", "decode_specialchars", 10, 2);
function decode_specialchars($choices, $field){
$choices = htmlspecialchars_decode($choices);
return $choices;
}
As mentioned above, the last part of this is making sure the HTML you add for the "text" property of each checkbox won't be stripped out by Gravity Forms:
And that's pretty much it. This is a rough base, but hopefully it will give you an idea of all the pieces involved. :)
Oh now that's amazing service. Exactly what I needed. I'm not finding much in the area of documentation surrounding this and probably could have saved you some time if the docs included some of this more advanced hook handling. But you saved me some time by giving me the answer outright so I can't complain!
Working like a charm!!! Thanks man. I've just got to doctor up the html now and I'll be done:)
One more question... See below:
http://screencast.com/t/sbL493uJw
The page is showing the dynamic posts from categories (with thumb and text:) but the email forwarding shows the prefiltered choices. Is it possible to filter the choices for email directing as well?
Unfortunately, this is not possible in the admin. The actual functionality of routing the emails based on the custom options is possible, but would require some more customization. Email me at david@rocketgenius.com and I can send you some code where I've done this for a side project.