Hardcode input and post to database
WordPress Event Management, Calendars & Registration › Forums › General Question › Hardcode input and post to database
This topic contains 10 replies, has 2 voices, and was last updated by Alex Steer 9 years, 9 months ago.
-
AuthorPosts
-
June 15, 2015 at 10:14 am #17335
Hello,
Is it possible to hardcode an input field in a booking form and post it to the system?
It would also need to use the validation.
How would I go about that, is there a snippet?
Many thanks.
-
This topic was modified 9 years, 9 months ago by
Alex Steer.
Alex Steer
June 15, 2015 at 5:16 pm #17343Yes it is, please this post for details: http://wp-event-organiser.com/blog/tutorial/using-form-api-add-additional-fields/
Stephen Harris
June 15, 2015 at 7:07 pm #17349Hi Stephen,
That worked brilliantly.
However, I am stuck on somthing that I wouldn’t mind your advice on…
I have created some custom fields on the event admin that populate the key => value for some radios.
They work useing the api code you pointed me too – no problem, but I would like to add a description under each radio option.The description is stored in a custom field too but I am unsure of the best way to add.
What would be the best way of acheiving this?
Many thanks for your help.
Alex Steer
June 16, 2015 at 5:14 pm #17365The best way might be to edit the template file (see
event-organiser-pro/templates/booking-form/
).The
EO_Booking_Form_Element
class hasset( $key, $value )
andget( $key )
methods so you can pass any data to the templates (e.g. array of descriptions indexed by option value).I’d recommend prefixing your key with some unique to avoid any collisions with core’s keys.
Stephen Harris
June 16, 2015 at 5:45 pm #17366Thanks Stephen,
I managed to get somthing working in similar way using $value on the template. – It’s good to know you suggested somthing similar.
This worked ok… The issue I am now left with relates to the api array because the key and value are stored in the custom fields.
When I submit and trigger validation I lose my added form fields – the variables containing key and value disapear – I have also tried sessions too.
I’m now looking at another way to make the array values populate upon condition.
Many thanks.
Alex Steer
June 16, 2015 at 7:13 pm #17368Not sure I follow… is the key not fixed and known?
If you need to get the event ID at any point you can use
$form->get( 'event_id' )
Stephen Harris
June 16, 2015 at 7:28 pm #17369Overall i’m trying to list some radio options which are editable on the events page (in custom fileds) rather than through the booking form editor.
I have it pulling through the data and creating the radio options (with individual descriptions) depending on whether they are populated.
This works on the single event page but breaks when the form bounces off validation.
I’m guessing im loosing access to the declared variables when/after posting.
Thanks
Alex Steer
June 16, 2015 at 7:43 pm #17371How are you adding the fields? The form shouldn’t ‘forget’ fields in the same page request.
Stephen Harris
June 16, 2015 at 7:53 pm #17372I have the api code in a seperate plugin I created like suggested in the docs.
My code:
function my_add_element( $form, $event_id ){
$exopone = types_render_field("price-option-one", array('output'=>'raw')); $exoptwo = types_render_field("price-option-two", array('output'=>'raw')); $exopthree = types_render_field("price-option-three", array('output'=>'raw')); //Create element to add to the field $element = EO_Booking_Form_Element_Factory::create( array( 'type' => 'radio', 'id' => '_supplementary_contact', //a unique ID 'required' => 1, //make this field optional 'options' => array( $exopone => $exopone, $exoptwo => $exoptwo, $exopthree => $exopthree ), 'label' => 'Please choose a price option'
) );
//Add the field element, optionally specify where
$form->add_element( $element, array( ‘at’ => 1 ) );
}
add_action( ‘eventorganiser_get_event_booking_form’, ‘my_add_element’, 10, 2 );
?>I was planning to unset them after depending on whether they exist – or use if else.
Alex Steer
June 16, 2015 at 8:08 pm #17373Hi think the problem is
types_render_field()
– you’re not passing the event ID so I’m assuming that it’s using the global post to get the ID, and since that’s not always going to available (or correct), that would explain the mixed results.Is there a similar function where you can pass the event ID (
$event_id
)Stephen Harris
June 16, 2015 at 8:48 pm #17375Look like you were bang on.
I swapped the code for:
function my_add_element( $form, $event_id ){ $url = explode('?', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ); $ID = url_to_postid( $url[0] ); $exopone = do_shortcode('[types field="price-option-one" id="'. $ID .'"]'); $exoptwo = do_shortcode('[types field="price-option-two" id="'. $ID .'"]'); $exopthree = do_shortcode('[types field="price-option-three" id="'. $ID .'"]'); //Create element to add to the field $element = EO_Booking_Form_Element_Factory::create( array( 'type' => 'radio', 'id' => '_supplementary_contact', //a unique ID 'required' => 1, //make this field optional 'options' => array( $exopone => $exopone, $exoptwo => $exoptwo, $exopthree => $exopthree ), 'label' => 'Please choose a price option' ) ); //Add the field element, optionally specify where $form->add_element( $element, array( 'at' => 1 ) ); } add_action( 'eventorganiser_get_event_booking_form', 'my_add_element', 10, 2 );
and it now works as intended – it still needs testing!
Thanks for pointing me in the right direction.
-
This reply was modified 9 years, 9 months ago by
Stephen Harris.
Alex Steer
-
This topic was modified 9 years, 9 months ago by
-
AuthorPosts