PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Map form field to custom field located in an Array

  1. Kurt
    Member

    I have an array for a metabox like this:

    $meta_boxes = array(
    "featured" => array(
    	  "name" => "featured",
    	  "title" => $options['featuredtext'],
    	 "description" => "",
    	  "type" => "radio",
    	  "class" => "radio",
    	  "rows" => "",
    	  "width" => "",
    	  "options" => array("1" => "Yes", "2" => "No", )
    	),
             "price" => array(
    	  "name" => "price",
    	  "title" => $options['pricetext'],
    	  "description" => "",
    	  "type" => "text",
    	  "class" => "text",
    	  "rows" => "",
    	  "width" => "",
             "options" => ""
    	), )

    And several more values, can somebody guide me or help me with mapping a custom field in a regular form to one of this "price" or "featured" custom fields?

    When I click on the dropdown of custom fields available the mentioned fields are not available there and I'm afraid that this is happening because they are inside the array.

    I'll appreciate and help with this as it seems simple but I cannot find a way to populate the fields.

    Posted 13 years ago on Friday June 24, 2011 | Permalink
  2. I am a little confused on what you are trying to do. Are you trying to populate a Post Custom Field with values from your array? Would it be a drop down field? Can you be a little more specific on exactly what you are trying to accomplish?

    Posted 13 years ago on Friday June 24, 2011 | Permalink
  3. Kurt
    Member

    Thank you for your answer Alex,

    I have a custom post type with several metaboxes, this metabxoxes feature several custom fields that are populated in an array like the one above described above and it is based on this code: http://brassblogs.com/cms-platforms/wordpress/custom-write-panels-for-wordpress-3-0.

    I'm using custom post type + Gravity Forms plugin and everything works great with taxonomies and the actual post type, the form is posting to the right place.

    I only have problems with the custom fields, because they are in an array, I call them like this n a regular post page:

    $fields = get_post_meta($post->ID, 'key', true);  (where key = the $meta_boxes  array)

    Then to get the value of let's say that Price custom field in that array and display it in my website I just do this:

    echo $fields['price'];

    And everything works great.

    Now in my Gravity form I drag a "custom field" field to the form and go to Advanced properties and goto "Custom field name"-> Existing and the only thing I see there is "Key" that it is the array containing my custom fields but not the actual custom fields, and if i add a new one and name it price it does not work either, i just need to know how I map a Gravity Forms custom field in advanced properties to a custom field inside an array like this one, I need to map one input text like that and several dropdowns,
    I really appreciate your help and let me know if you need me to explain it better.

    Posted 13 years ago on Friday June 24, 2011 | Permalink
  4. What you will need to do is manually update the custom field using the gform_post_submission hook.
    Following is a code snippet to get you started.

    add_action("gform_post_submission", "add_custom_field", 10, 2);
    function add_custom_field($entry, $form){
        //NOTE: replace 1 with the ID of your form.
        if($form["id"] != 1)
           return;
    
        //getting post meta
        $meta = get_post_meta($entry["post_id"], 'key', true);
    
        //updating price property of array.
        //NOTE: replace "1" with the ID of the field you want to populate the price field.
        $meta["price"] = $entry["1"];
    
        //updating meta
        update_post_meta($entry["post_id"], "key", $meta);
    
    }
    Posted 13 years ago on Tuesday July 5, 2011 | Permalink
  5. This looks like exactly what I need. I can adapt that snippet for something else I'm doing.

    Where do I put that code, though? Does something like that go in my theme's functions.php file? Or should I put that in a plugin that I'm modifying?

    I've tried it in both places, and it isn't working. I could have another error, too, I'm sure

    Thanks.

    Posted 13 years ago on Saturday July 30, 2011 | Permalink
  6. You place that in your functions.php.

    http://gravityhelp.com/documentation/page/Where_Do_I_Put_This_Code%3F

    Be sure you change the ID of both the form and the field number for your value. Also, please post what is happening to you now, a link to your site and the actual code you're using. Error messages too, if you have any.

    Posted 13 years ago on Saturday July 30, 2011 | Permalink
  7. Thanks, I was able to figure out what the problem was. I just messed something up. (Go figure.)

    Anyway, thanks for the code! It was perfect.

    Posted 13 years ago on Saturday July 30, 2011 | Permalink
  8. You're welcome. Glad you got it sorted out.

    Posted 13 years ago on Saturday July 30, 2011 | Permalink