' . esc_html($result) . '

'; } ?>

AI Content to Facebook

Click the button below to log in with Facebook:

Manual Post to Facebook



AI Generated Post to Facebook


'; } function aiccfb_facebook_page_id_callback() { $options = get_option('aiccfb_facebook_settings'); echo ''; } function aiccfb_facebook_access_token_callback() { $options = get_option('aiccfb_facebook_settings'); echo ''; } function aiccfb_facebook_app_id_callback() { $options = get_option('aiccfb_facebook_settings'); echo ''; } function aiccfb_post_to_facebook($content, $image_url = '') { $options = get_option('aiccfb_facebook_settings'); if (empty($options['facebook_access_token']) || empty($options['facebook_page_id'])) { return "Facebook access token or page ID not set or empty."; } $page_access_token = aiccfb_get_page_access_token($options['facebook_access_token'], $options['facebook_page_id']); if (!$page_access_token) { return "Failed to get page access token."; } if (!empty($image_url)) { // Post with an image $url = "https://graph.facebook.com/{$options['facebook_page_id']}/photos"; $data = [ 'url' => $image_url, 'caption' => $content, 'access_token' => $page_access_token, ]; } else { // Text-only post $url = "https://graph.facebook.com/{$options['facebook_page_id']}/feed"; $data = [ 'message' => $content, 'access_token' => $page_access_token, ]; } $response = wp_remote_post($url, [ 'body' => $data, 'method' => 'POST', 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], ]); if (is_wp_error($response)) { $error_message = $response->get_error_message(); return "Error posting to Facebook: $error_message"; } $body = json_decode(wp_remote_retrieve_body($response), true); if (isset($body['error'])) { return "Error posting to Facebook: " . $body['error']['message']; } return "Successfully posted to Facebook. Post ID: " . $body['id']; } function aiccfb_get_page_access_token($user_access_token, $page_id) { $url = "https://graph.facebook.com/{$page_id}?fields=access_token&access_token={$user_access_token}"; $response = wp_remote_get($url); if (is_wp_error($response)) { return false; } $body = json_decode(wp_remote_retrieve_body($response), true); if (isset($body['access_token'])) { return $body['access_token']; } return false; } add_action('wp_ajax_generate_ai_content', 'aiccfb_handle_generate_ai_content'); function aiccfb_handle_generate_ai_content() { // Verify nonce check_ajax_referer('aiccfb_facebook_nonce_action', 'aiccfb_facebook_nonce'); $prompt = isset($_POST['prompt']) ? sanitize_text_field($_POST['prompt']) : ''; $content = aiccfb_generate_content_with_gpt4($prompt); echo wp_kses_post($content); wp_die(); // Terminate the AJAX request correctly } // New AJAX action for posting content add_action('wp_ajax_post_ai_content_to_facebook', 'aiccfb_handle_post_ai_content_to_facebook'); function aiccfb_handle_post_ai_content_to_facebook() { // Verify nonce check_ajax_referer('aiccfb_facebook_nonce_action', 'aiccfb_facebook_nonce'); if (!current_user_can('publish_posts')) { wp_die('Insufficient permissions'); // Check user capabilities } $content = isset($_POST['content']) ? sanitize_textarea_field($_POST['content']) : ''; // Assuming you might have an image URL, adjust as needed $image_url = isset($_POST['image_url']) ? esc_url_raw($_POST['image_url']) : ''; $result = aiccfb_post_to_facebook($content, $image_url); echo esc_html($result); wp_die(); // Terminate the AJAX request correctly } ?>