Templates

Appointment Booking

prefix . 'appointment_services'; // Hardcoded table name $service = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $service_id)); if ($service) { $price = $service->price; // Insert appointment booking data $wpdb->insert( $wpdb->prefix . 'appointment_booking', [ 'user_id' => get_current_user_id(), 'service_id' => $service_id, 'booking_date' => $booking_date, 'booking_time' => $booking_time, 'name' => $name, 'email' => $email, 'phone' => $phone, 'price' => $price, 'status' => 'pending' ], [ '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%f', '%s' ] ); // Check if insert was successful if ($wpdb->insert_id) { wp_redirect(home_url('')); // Redirect to home or another success page exit; } else { wp_die('Failed to book the appointment.'); } } else { wp_die('Invalid service selected.'); } } // Login form shortcode public function abp_appointment_login_form_shortcode() { if (is_user_logged_in()) { return '

You are already logged in.

'; } ob_start(); ?>
$log, 'user_password' => $pwd, 'remember' => true, ); $user = wp_signon($creds, false); if (is_wp_error($user)) { echo '

Login failed. Please check your credentials.

'; } else { wp_redirect(home_url('')); exit; } } return ob_get_clean(); } // Register form shortcode public function abp_appointment_register_form_shortcode() { if (is_user_logged_in()) { return '

You are already logged in.

'; } ob_start(); ?>
$username, 'user_email' => $email, 'user_pass' => $password, 'display_name' => $full_name, ); $user_id = wp_insert_user($userdata); if (is_wp_error($user_id)) { echo '

Registration failed. Please try again.

'; } else { wp_set_current_user($user_id); wp_set_auth_cookie($user_id); wp_redirect(home_url('')); exit; } } return ob_get_clean(); } // Shortcode for Bookings page public function abp_bookings_page_shortcode() { // If form is submitted, verify nonce first if (isset($_POST['cancel_booking_id'])) { // Verify nonce in form handler if (!isset($_POST['book_appointment_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['book_appointment_nonce'])), 'book_appointment_action')) { wp_die('Invalid request.'); } if (is_user_logged_in()) { $current_user_id = get_current_user_id(); global $wpdb; $table_name = $wpdb->prefix . 'appointment_booking'; $booking_id = intval($_POST['cancel_booking_id']); $wpdb->update( $table_name, array('status' => 'Canceled'), array('id' => $booking_id, 'user_id' => $current_user_id) ); echo '

Your booking has been canceled.

'; } } // Display the bookings if user is logged in if (!is_user_logged_in()) { return '

Please login to view your bookings.

'; } $current_user_id = get_current_user_id(); global $wpdb; $table_name = $wpdb->prefix . 'appointment_booking'; $bookings = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}appointment_booking WHERE user_id = %d", $current_user_id)); ob_start(); if ($bookings) { echo ''; } else { echo '

' . esc_html__('No bookings found.', 'text-domain') . '

'; } return ob_get_clean(); } //end function abp_book_appointment_form_shortcode() { ob_start(); // Fetch available services global $wpdb; $table_name = $wpdb->prefix . 'appointment_services'; $services = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}appointment_services"); ?>

Select Service

Select Date

Select Time

Enter Your Details

Summary

Service:

Price: $

Date:

Time:

get_var("SELECT COUNT(*) FROM {$wpdb->prefix}appointment_booking"); // Fetch approved appointments $approved_appointments = $wpdb->get_var($wpdb->prepare( " SELECT COUNT(*) FROM {$wpdb->prefix}appointment_booking WHERE status = %s", 'approved' )); // Fetch total customers $total_customers = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->users}"); // Fetch total services $total_services = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}appointment_services"); echo '

Total Appointments

' . esc_html($total_appointments ? $total_appointments : 0) . '

Total Services

' . esc_html($total_services ? $total_services : 0) . '

Approved Appointments

' . esc_html($approved_appointments ? $approved_appointments : 0) . '

Total Customers

' . esc_html($total_customers ? $total_customers : 0) . '

'; } //end // Fetch appointments public function abp_render_appointments() { global $wpdb; $appointments_table = $wpdb->prefix . 'appointment_booking'; if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $appointments_table)) != $appointments_table) { echo '

No appointments found or table missing.

'; return; } if (isset($_POST['update_appointment_status']) && isset($_POST['appointment_id']) && isset($_POST['appointment_status'])) { // Verify nonce if (!isset($_POST['appointment_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['appointment_nonce'])), 'update_appointment_status_nonce')) { die('Nonce verification failed.'); } $appointment_id = intval($_POST['appointment_id']); $new_status = sanitize_text_field(wp_unslash($_POST['appointment_status'])); $wpdb->update( $appointments_table, ['status' => $new_status], ['id' => $appointment_id], ['%s'], ['%d'] ); echo '

Appointment status updated.

'; } if (isset($_POST['delete_appointment']) && isset($_POST['appointment_id'])) { $appointment_id = intval($_POST['appointment_id']); $wpdb->delete( $appointments_table, ['id' => $appointment_id], ['%d'] ); echo '

Appointment deleted successfully.

'; } $appointments = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}appointment_booking"); if (empty($appointments)) { echo '

No appointments found.

'; return; } ?>

Appointments

ID User Service Booking Date Booking Time Status Actions
id); ?> name); ?> service_id); ?> booking_date); ?> booking_time); ?>
get_results(" SELECT u.ID, u.user_login, u.user_email, u.display_name, COUNT(a.id) as total_appointments FROM {$wpdb->users} u LEFT JOIN {$wpdb->prefix}appointment_booking a ON u.ID = a.user_id GROUP BY u.ID "); if ($users) { echo ''; foreach ($users as $user) { echo ''; } echo '
User ID Username Email Display Name Total Appointments
' . esc_html($user->ID) . ' ' . esc_html($user->user_login) . ' ' . esc_html($user->user_email) . ' ' . esc_html($user->display_name) . ' ' . esc_html($user->total_appointments) . '
'; } else { echo '

No customers found.

'; } } //end } new ABP_Admin(); ?>