• Resolved ThomasLG

    (@thomaslg)


    Hi!
    I’m trying to add new form fields using hooks/filters. So far I managed to add the field itself, the problem is storing and using the field input. I have been looking at the plugin functions, but don’t really get how I should use $this/$booking.
    It would be awesome if you could take a quick look at it and point me in the right direction. I’ll get straight to it, here’s the code so far (just using functions.php at the moment):

    // ADDING NEW FIELDS: WORKING!
    add_filter( 'rtb_booking_form_fields', 'buoy_custom_fields', 10, 2 );
    function buoy_custom_fields( $fields, $request ) {
      $fields['reservation']['fields']['retter']['title'] = 'Retter';
      $fields['reservation']['fields']['retter']['request_input'] = empty( $request->request_retter ) ? '' : $request->request_retter;
      $fields['reservation']['fields']['retter']['callback'] = 'rtb_print_form_text_field';
    
      return $fields;
    }
    
    // SAVING VALIDATED DATA: NOT WORKING!
    add_filter( 'rtb_validate_booking_submission', 'buoy_custom_load', 10, 3 );
    function buoy_custom_load( $this, $booking ) {
      $this->retter = empty( $_POST['rtb-retter'] ) ? '' : sanitize_text_field( stripslashes_deep( $_POST['rtb-retter'] ) );
    
    // I believe I should be adding something to $booking here?
    
      return $this;
    }
    
    // SAVING INPUT TO POST METADATA: NOT WORKING
    add_filter( 'rtb_insert_booking_metadata', 'buoy_insert_meta', 10, 2 );
    function buoy_insert_meta( $meta, $this) {
      $meta['retter'] = $_POST['rtb-retter'] ;
    // Tried using "$this->booking->retter" etc. - produces error msg
    
      return $meta;
    }
    
    // DISPLAY INPUT IN MAIL BY TEMPLATE TAG: TAG IS DISPLAYED, NO INPUT DATA
    add_filter( 'process_template', 'buoy_template_tags', 10, 2 );
    function buoy_template_tags( $template_tags, $notification) {
      $template_tags['{retter}'] = "test";
    
      return $template_tags;
    }

    https://www.ads-software.com/plugins/restaurant-reservations/

Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter ThomasLG

    (@thomaslg)

    If I using “rtb_insert_booking_metadata” try storing for instance $meta[‘retter’] = $this->retter; I get a fatal error from the web server for using $this…. Apparently that’s not the way to do it, although that’s how you do it in the insert_post_data function itself in Booking.class.php….

    Hi ThomasLG,

    Nice work! Looks like you’re almost there. Nice to see my filters getting used. I hope to write a step-by-step tutorial for doing this in the future. In the meantime, the following should get you on track. Please note I haven’t run this code so you might need to fix typos, etc.

    // Validate data
    //
    // In Booking.class.php, it passes $this, which is an instance of itself,
    // a rtbBooking object. $this is a special variable that a class uses
    // to refer to itself. So when we receive it in the action callback, we
    // call it $booking.
    //
    // This filter is only for validating the data and storing it in the
    // booking object. You don't actually save anything here.
    add_filter( 'rtb_validate_booking_submission', 'buoy_custom_load' );
    function buoy_custom_load( $booking ) {
      $booking->retter = empty( $_POST['rtb-retter'] ) ? '' : sanitize_text_field( stripslashes_deep( $_POST['rtb-retter'] ) );
    
      // You only need to add something to $booking if you want to send
      // a validation error. For instance, if you wanted this field to be
      // required, you'd have to set an error like this:
      // https://github.com/NateWr/restaurant-reservations/blob/master/includes/Booking.class.php#L384-L390
    
      return $booking;
    }
    
    // Save metadata fields
    //
    // Here is where we add the information from the booking to the array
    // of metadata that is actually stored in the database. So now we have
    // the info stored in the database.
    add_filter( 'rtb_insert_booking_metadata', 'buoy_insert_meta', 10, 2 );
    function buoy_insert_meta( $meta, $booking) {
    
      // Always pull the data from teh $booking object here instead of post,
      // because that way we can be certain the data has been sanitized
      // during the validation process
      $meta['retter'] = $booking->retter;
    
      return $meta;
    }
    
    // Load the new metadata when a booking is loaded
    //
    // This is the missing piece of the puzzle. When the booking is retrieved
    // from teh database, you need to make sure to pull out your new meta
    // data.
    //
    // (Actually, I'm missing a filter here, which I'll add soon to make
    // this easier. But this will work.)
    add_action( 'rtb_booking_load_post_data', 'buoy_load_post_data', 10, 2 );
    function buoy_load_post_data( $booking, $post ) {
    
    	// Load the post meta
    	$meta = get_post_meta( $booking->ID, 'rtb', true );
    
    	// Assign your new metadata to the booking object
    	$booking->retter = empty( $meta['retter'] ) ? '' : $meta['retter'];
    }
    
    // Process a new template tag
    //
    // You were almost there. The booking is passed into the notification so
    // you can access your metadata there.
    add_filter( 'rtb_notification_template_tags', 'buoy_template_tags', 10, 2 );
    function buoy_template_tags( $template_tags, $notification) {
      $template_tags['{retter}'] = $notification->booking->retter;
    
      return $template_tags;
    }

    I wrote a brief tutorial on adding template tags which you might also find helpful for adding a bit of the final polish details.

    (I just fixed a bad reference to $this in buoy_load_post_data so if you already copied the code take another look.)

    Thread Starter ThomasLG

    (@thomaslg)

    Thanks a lot, this all makes sense. And I noticed the $this that was supposed to be $post ??
    I can’t make it work though, I get an email notification saying Retter: {retter} as if it isn’t set. I’ll continue playing with it…

    Btw, is there any way to add the new form field to the backend bookings list?

    The filter is wrong for the template tags. That’s why it’s not working. Change this line:

    add_filter( 'process_template', 'buoy_template_tags', 10, 2 );

    To this:

    add_filter( 'rtb_notification_template_tags', 'buoy_template_tags', 10, 2 );

    The $this that I changed should be $booking, but it can be $post too. ??

    You can add columns to the bookings table. It extends the WP_List_Table class, so it follows the same patterns. You’ll want to add the column to the table and then print the value.

    Actually, I believe if you name the column in the table retter it will print the value for you. Not sure though.

    Thread Starter ThomasLG

    (@thomaslg)

    Ok, I’ll take a look at it tomorrow then, along with the possibility to change input field types etc. Thanks a lot anyways, this one works like a charm now.
    (Oh yes, I’ll use $booking instead of $post, sanitize, sanitize!)

    Thread Starter ThomasLG

    (@thomaslg)

    Yes that seems to work fine, so the code for adding a new column is:

    //Add column in backend
    add_filter( 'rtb_bookings_table_columns', 'buoy_custom_col' );
    function buoy_custom_col( $columns ) {
      $columns['retter'] = 'Retter';
      return $columns;
    }

    (For anyone else interested)

    Thanks for sharing Thomas!

    Thread Starter ThomasLG

    (@thomaslg)

    Is there any way to add a dropdown (select) form field instead of the standard text input/textarea? I noticed in the github template-functions.php, it contains a function for this, while in the wordpress plugin it’s deleted from template-functions.php, so using “rtb_print_form_select_field” just throws me an error.

    Hi Thomas,

    Yes, the Party field is being converted to a select field in the next version. Thats why the rtb_print_form_select_field exists in GitHub but not in the current release — it wasn’t implemented yet.

    You can just copy that field from the git repo, rename it, then add it to your own plugin/theme. As long as its in the global namespace you can reference any callback to print your field.

    Thread Starter ThomasLG

    (@thomaslg)

    That seems to work fine. So basically I can make any type of fields I want, as long as I make a new function with a unique name, and it outputs some value which can be processed by the plugin (inserted in bookings, metadata etc)?

    Exactly! It’s pretty loose and you can rewrite the entire form if you want (I’m working on a point-and-click custom fields addon now).

    Be sure to check out how the default fields use <?php echo rtb_print_form_error( $slug ); ?> to feed validation errors back to the form. And keep in mind that even if you remove a field, the validation routine will still try to validate for that data, so you may need to strip out unnecessary validation errors.

    Thread Starter ThomasLG

    (@thomaslg)

    Sounds sweet. I’ve looked through a whole bunch of booking/reservation plugins, this seems to be the only one with a calendar, customizable form, and a bookings backend. I don’t mind coding a bit to make the customizations for now, but if you make an UI for it, this plugin will be even better ??

    An issue: I’m using the shortcode to print the form in a floating DIV, but when I click submit to validate it, it jumps to the defined bookings page and prints a form with the error msgs along with it. It might have to be this way cus of the way the plugin is made(?), but if you did the validation feedback through AJAX or javascript (before the necessary php validation of course) the user wouldn’t have to load the bookings page when using the shortcode. Unless you see some quickfix for this, I guess it’s a future feature request…

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘New fields using hooks’ is closed to new replies.