Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hazratbilal0079/81dfb7e40f2a9a04951a5e0994ec17b9 to your computer and use it in GitHub Desktop.
Save hazratbilal0079/81dfb7e40f2a9a04951a5e0994ec17b9 to your computer and use it in GitHub Desktop.
Create Zoom Meeting in wordpress Through Hook and insert in the post type meta fields
add_action('jet-form-builder/custom-action/generatezoomclass', function ($request, $action_handler) {
global $wpdb;
$post_id = isset($request['inserted_post_id']) ? intval($request['inserted_post_id']) : 0;
$zoom_account_id = "your_zoom_account_id";
$zoom_client_id = "your_zoom_client_id";
$zoom_client_secret = "your_zoom_client_password";
if (!$post_id) {
$action_handler->add_error('post_id', 'Post ID is missing.');
return;
}
// Get post title for the meeting topic
$post = get_post($post_id);
if (!$post) {
$action_handler->add_error('post_id', 'Post not found.');
return;
}
$meeting_topic = $post->post_title;
// Zoom API Endpoint
$zoom_api_url = 'https://api.zoom.us/v2/users/me/meetings';
// Get Zoom access token
$access_token = get_zoom_access_token($zoom_client_id, $zoom_client_secret, $zoom_account_id);
if (!$access_token) {
$action_handler->add_error('zoom_api', 'Failed to get Zoom access token.');
return;
}
// Prepare meeting data
$meeting_data = [
'topic' => $meeting_topic,
'type' => 2, // Scheduled meeting
'settings' => [
'host_video' => false,
'participant_video' => false,
'join_before_host' => true,
'mute_upon_entry' => true,
'waiting_room' => false,
],
];
// Add start_time if classes_date_time is provided
if (isset($request['classes_date_time'])) {
$datetime = $request['classes_date_time'];
$meeting_data['start_time'] = gmdate('Y-m-d\TH:i:s\Z', $datetime); // Convert timestamp to ISO 8601 format
$meeting_data['duration'] = 60; //set duration in minutes. You can adjust this value.
$meeting_data['timezone'] = 'UTC'; // Zoom requires the timezone.
}
// Make Zoom API request
$args = [
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
'Content-Type' => 'application/json',
],
'body' => wp_json_encode($meeting_data),
'method' => 'POST',
'data_format' => 'body',
];
$response = wp_remote_post($zoom_api_url, $args);
if (is_wp_error($response)) {
$action_handler->add_error('zoom_api', 'Zoom API request failed: ' . $response->get_error_message());
return;
}
$body = wp_remote_retrieve_body($response);
$meeting_response = json_decode($body, true);
if (isset($meeting_response['code'])) {
$action_handler->add_error('zoom_api', 'Zoom API error: ' . $meeting_response['message']);
return;
}
// Save Zoom meeting data to post meta
if (isset($meeting_response['start_url'])) {
update_post_meta($post_id, 'start_url', $meeting_response['start_url']);
}
if (isset($meeting_response['join_url'])) {
update_post_meta($post_id, 'join_url', $meeting_response['join_url']);
}
if (isset($meeting_response['id'])) {
update_post_meta($post_id, 'class_id', $meeting_response['id']);
}
if (isset($meeting_response['password'])) {
update_post_meta($post_id, 'class_password', $meeting_response['password']);
}
}, 10, 2);
// Function to get Zoom access token
function get_zoom_access_token($client_id, $client_secret, $account_id) {
$token_url = 'https://zoom.us/oauth/token';
$args = [
'headers' => [
'Authorization' => 'Basic ' . base64_encode($client_id . ':' . $client_secret),
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => [
'grant_type' => 'account_credentials',
'account_id' => $account_id,
],
'method' => 'POST',
'data_format' => 'body',
];
$response = wp_remote_post($token_url, $args);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
$token_response = json_decode($body, true);
if (isset($token_response['access_token'])) {
return $token_response['access_token'];
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment