Wordpress Gravity Forms Serialized List - php

I've got a List field with Gravity Forms to populate some custom fields in a custom post type. The site is a recipe submission site, and I'm looking for users to be able to add ingredients individually for better SEO. My issue is, when I submit the form only the last input field under ingredients is passed to the recipe.
I know I need a serialized list as this custom field pulls an array, but I'm at a complete loss of how to do that. The array should read something like this
a:8:{i:0;s:26:"4oz piece of salmon/person";i:1;s:12:"1 egg/person";i:2;s:37:"1-2 multi-colored bell peppers/person";i:3;s:12:"Greek olives";i:4;s:9:"Olive oil";i:5;s:13:"Salt & Pepper";i:6;s:22:"Basil (fresh or dried)";i:7;s:0:"";}
I don't even know where to begin in putting together a serialized array for one form field, so any nudge in the right direction is greatly appreciated.

Unfortunately Gravity Forms is configured to store these as separate meta records. One option is to customize the Gravity Forms forms_model.php file, create_post function, which unserializes the field contents and loops through each item to create a new post_meta record.
The following code should replace the case for field type list, and will prevent the creation of individual meta records on a predefined array of Gravity Form fields:
case "list" :
$skipped_list_fields = array('<meta name for field to skip unserializing>',
'<meta name for another field to skip unserializing>');
$value = maybe_unserialize($value);
if (in_array($meta_name, $skipped_list_fields)) {
if(!rgblank($value))
add_post_meta($post_id, $meta_name, $value);
} else {
if(is_array($value)){
foreach($value as $item){
if(is_array($item))
$item = implode("|", $item);
if(!rgblank($item))
add_post_meta($post_id, $meta_name, $item);
}
}
}
break;

Jason, someone had a similar situation https://stackoverflow.com/questions/20591802/how-to-save-comma-separated-inputs-in-gravity-forms-to-a-global-array-on-form-su - I think a similar edit in functions.php in your theme is the direction...

With a Custom Field form field configured as a List type and having filled out the form like this, I'm seeing all the ingredients as separate post meta items (but all attached to the same key) like this.
Is this different from what you are seeing or are you trying to achieve something different?

Use the 'List' field type from the 'Advanced Fields' area when creating/editing your form. That data is already serialized and saved as an array in a single field. Just use the gform_after_submission hook to save the GF field as postmeta.

Related

Delete ACF user profile repeater field row from user account (WordPress)

I am using ACF repeater field for user profiles, which shows like this in the user profile page:
That's cool.
But on the front-end I have a form that I want to use to delete a specific row. My custom form simply lists all the rows with radio buttons, and so if the user selected number 3 and submitted the form then I want to delete the third row (in this example, the row with Catherine Davies would be deleted).
The form works fine in that it submits as expected and returns the value the user selected, but my code to delete the row that was selected doesn't seem to work.
The ACF documentation seems a little vague on the subject. Based on a combination of the ACF doc and this StackOverflow post, I expect this code to work but it does not:
$user_ID = get_current_user_id();
$field = 'extra_user_info'; // Name of the repeater field
$row_to_delete = $_POST["row_to_delete"];
delete_sub_row($field, $row_to_delete, 'user_' . $user_id);
Just to be sure, even if I hardcode the $row_to_delete variable to any number (from 1 to 4) it still does not delete any row.
Just to clarify, I wish to delete an entire row.
Note: I realise I could just embed the ACF form on the front-end, but for reasons I won't go into this is not an option, hence using my own custom form.
There could be another issue but in your example at least, your $user_ID variable has capital letters, while the variable passed to delete_sub_row() is lowercase. PHP variables are case-sensitive.

Adding tag and custom post meta

The above function then checks for a condition for two scenarios:
1. Image is selected: a post is created from the first function when an image is selected. Because it already has the post_id, it simply updates the "dummy" content with the real content from the input fields
2. Image is not selected: as an image is not selected, then there is no post created from the first function. Because there is no post (thus no post_id), it creates a new post using the input fields.
Problem:
There are total 4 input fields that I need to have: title (title), description(content), tag(rh_tag) and custom field (custom_1).
I am having a problem with tag and custom field in a scenario when an image is selected (the function is not completed).
So, let say, if I do not select an image (thus no post is created from the first function), then the second function (and "else" part) simply creates a new post with all the four input. This works just fine.
However, if I select an image, then I am missing something and I can't somehow make the function work. I tried to add same $post =array as the second function, but it was not successful.
Can someone help me out how to "complete" the first function so that I can add tag and custom post meta even when an image is selected?
At first, look at WordPress codex. As I see, you have incorrect usage of update_post_meta. You should use post_id as first argument of the function, not array
At second, before accessing value from $_POST array, be sure that it exists:
$customValue = isset($_POST['custom_1']) ? $_POST['custom_1'] : 'default value';

php shortcode function to add other sum other shortcode values

im doing some web designing with wordpress at the moment.
I have created a page with a web form where a client can input some information (name, email, invoice number, price, gst, etc).
I used a plugin called contact form 7 to provide the web form, when the user inputs all of their information, the plugin then emails an html formatted invoice to my email address, with the fields occupied by shortcodes which take on the value from the web form.
For example, in the name field of my invoice table, i enter [first-name] and the name the user inputs gets emailed to me in the invoice.
I need to find a way to sum the value of two of the other fields, one of the fields on my invoice form is called total, which should be equal to [gst]+[price] that the user inputs on the web form.
Ive tried to look for a php shortcode function that can take its arguments as the values of other shortcodes, but havent had any luck. I have never really used php before either so wouldnt know how to write one.
I managed to find this, which doesnt seem to work, as when the email comes through all i see is [sumsc][gst] [price][/sumsc]
add_shortcode('sumsc','sumsc_func');
function sumsc_func( $atts, $content = null ) {
$sum=0;
$content=str_replace(array(" ","] [","]["),array(" ","][","]|["),$content);
$codes=explode("|",$content);
foreach($codes as $code){
$sum+=do_shortcode($code);
}
return " <div>".$sum."
</div>";
}
Any help would be greatly appreciated, I also need to the functionality to be able to multiply a shortcode value by a certain number. For example the price needs to be divided by 0.6 to give the quantity which appears on the invoice. Thanks!
Create a hidden field in the form say with a unique id say it fields_sum.
Use jquery on that page and catch form submission event. Get values of those fields, sum it and set the result to fields_sum field. And then use field shortcode where you want.
jQuery script would be something like (Algorithm)
$(document).ready(function(){
$("your_form_id").submit(function(e){
// get value of field one
// get value of field two
// sum them
// set sum to the hidden field
});
});
I hope this works and enough to get a solution like this.

How to display all taxonomy term fields when adding node in Drupal

I'm trying to make form to post advert in newspaper and want to let user select style of future post and show how it will be printed. So i create taxonomy vocab. "style" with name, sample image and max. characters to this type.
Next i'm creating field taxonomy term relation in advert content type.
Now i need to show in adding form not just radios or select with titles but title, sample image and other fields from taxonomy.
I think i need to use hook_form_alter so i use mymodule_form_ad_node_form_alter(&$form, &$form_state, $form_id) but what i can't understand is how in drupal way i can get rest of taxonomy fields? In $form array is only tids and titles.
Is it possible not to query DB again but setup existing query to retrieve all fields i need?
Also how i can insert that additional information into form? Is it other way than #suffix #prefix on radios?
If this is a Drupal 7 then you can use an Entity Reference field along with the Entity Reference view widget instead of a term reference field. That will allow you to specify a view to use to populate the field options, and you can create an entity reference view that display the additional taxonomy term fields you want to display.

Drupal views filtering content by node id

I have the following scenario:
I have a content type called 'Product' in which there are the default input fields and a number of custom input fields that have been created using CCK.
When a new product is created, all of the data entered is posted as a node (as normal).
Part of the problem is that I don't want the values from the custom input fields to be within the node content. Therefore, my thinking was to hide the display of these fields. I could then create a block of the custom input fields using views.
Is it possible within views to set up some filtering that will only display the values of the custom input fields on the node from which it was originally created? For example if the product I created had a node id of 1, is it possible to filter the view to display the values of the custom input fields that were created from node id of 1 and then display these values as a block on node id 1?
This might sound a confusing approach but I am keen to separate some of the data entered into the Product content type from the main node content, mainly for styling reasons.
Thanks, Mark.
Ah worked it out. It's always an easy solution isn't it?
In the Arguments section of the view, add Node: Node ID. Select the action of 'Provide default argument' and then select 'Node ID from URL' from the 'Default argument type:'.
This will then just display the values of the custom input fields that was created on a particular node.

Categories