Summarize content using Gemini API and save to a field

i have a wordpress content field. i want gemini to summarize and place to a field. can you help me with code

Absolutely — you can do this with the free Google Gemini API, and I can give you a clean, ready‑to‑use WordPress code snippet that:

  • Takes your content field
  • Sends it to Gemini
  • Gets a summary
  • Saves it into another field (ACF, custom meta, excerpt, etc.)

Below is a clear, flexible version you can drop into functions.php or WPCode.


WordPress PHP snippet: summarize content using Gemini API and save to a field

This example:

  • Reads the post content
  • Sends it to Gemini 1.5 Flash (free tier)
  • Saves the summary into a custom field called _summary

You can change the field name to anything.

add_action('save_post', 'jmf_generate_gemini_summary', 20, 3);

function jmf_generate_gemini_summary($post_id, $post, $update) {

    // Avoid infinite loops
    remove_action('save_post', 'jmf_generate_gemini_summary');

    // Only run for posts (change to 'page' if needed)
    if ($post->post_type !== 'post') {
        return;
    }

    // Get the content to summarize
    $content = $post->post_content;

    if (empty($content)) {
        return;
    }

    // Your Gemini API key
    $api_key = 'YOUR_GEMINI_API_KEY_HERE';

    // Gemini API endpoint
    $url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=' . $api_key;

    // Build the request
    $body = json_encode([
        'contents' => [
            [
                'parts' => [
                    ['text' => "Summarize this content in 1–2 sentences:nn" . wp_strip_all_tags($content)]
                ]
            ]
        ]
    ]);

    // Send request
    $response = wp_remote_post($url, [
        'headers' => [
            'Content-Type' => 'application/json'
        ],
        'body' => $body
    ]);

    // Handle errors
    if (is_wp_error($response)) {
        return;
    }

    $data = json_decode(wp_remote_retrieve_body($response), true);

    // Extract summary text
    $summary = $data['candidates'][0]['content']['parts'][0]['text'] ?? '';

    if (!empty($summary)) {
        update_post_meta($post_id, '_summary', sanitize_text_field($summary));
    }

    // Re‑enable hook
    add_action('save_post', 'jmf_generate_gemini_summary', 20, 3);
}

🧩 Where the summary gets saved

This line controls the destination:

update_post_meta($post_id, '_summary', sanitize_text_field($summary));

You can change _summary to:

  • excerpt → to overwrite the post excerpt
  • seo_title → for SEO plugins
  • acf_summary → for ACF fields
  • _custom_field_name → anything you want

Example for ACF:

update_field('summary', $summary, $post_id);

⭐ Want a button in the editor instead of auto‑summarizing?

I can give you:

  • A Gutenberg button
  • A Classic Editor button
  • A WP Admin metabox with “Generate Summary”
  • A REST API endpoint
  • A custom plugin version

Just tell me your preferred workflow and I’ll tailor the code.