=== Dropndot AI Content for Custom Fields ===
Contributors: dropndot
Tags: ai, custom-fields, openai, gemini, content-generation
Requires at least: 6.0
Tested up to: 7.1
Stable tag: 2.0.0
Requires PHP: 7.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Smart AI content generation for WordPress custom fields with automatic token management and no cutoffs.

== Description ==

Dropndot AI Content for Custom Fields is a powerful WordPress plugin that brings AI-powered content generation directly to your custom fields. Generate high-quality content for any custom field with a single click, using your own OpenAI or Google Gemini API key, or the WordPress AI Client and whichever AI provider you have connected under Settings → Connectors (OpenAI, Google Gemini, Anthropic, and others).

= Key Features =

* **Smart Field Detection**: Automatically detects all custom fields (ACF, Meta Box, Pods, standard WordPress fields, Yoast SEO, Rank Math)
* **One-Click Generation**: Generate content for any field with a single click
* **Intelligent Content Detection**: Automatically detects appropriate content type based on field name
* **Custom Prompts with Limits**: Set custom AI prompts for each field with explicit character/word limits
* **Precise Length Control**: Dedicated character and word limit fields ensure AI respects your requirements
* **Complete Sentence Truncation**: Smart truncation at sentence boundaries, never cuts off mid-word
* **Advanced Internal Linker**: AI-powered keyword extraction and smart post matching for internal linking
* **AI Chat Assistant**: Chat with AI about your article content for suggestions and improvements
* **Preview & Edit**: Review AI-generated content before saving
* **Two Ways to Connect**: Use your own encrypted OpenAI / Google Gemini API key, or the WordPress AI Client (Settings → Connectors)
* **Model Selection**: Choose the OpenAI and Gemini model; the OpenAI list is filtered to the models your key can reach
* **Usage Guardrails**: Per-user rate limiting and site-wide daily request and token quotas
* **Response Cache**: Identical generations are served from a short-lived cache ("Regenerate" always bypasses it)
* **Monitoring**: Today's usage and a log of the 100 most recent AI calls
* **Optimized Token Usage**: Intelligent content truncation to prevent token limit errors
* **User Access Control**: Granular user permissions
* **Format Support**: Plain text, WYSIWYG, JSON, lists, and paragraphs
* **Professional UI**: Clean, modern WordPress admin interface

= How It Works =

1. Install and activate the plugin
2. Either enter your OpenAI or Google Gemini API key in the plugin settings, or connect an AI provider under **Settings → Connectors** (WordPress 7.0+)
3. Select which custom fields to enable
4. Set character/word limits for each field (optional)
5. Optionally set custom prompts for each field
6. Generate content with one click while editing posts

= Supported Field Types =

* Advanced Custom Fields (ACF)
* Meta Box
* Pods
* Standard WordPress custom fields
* Yoast SEO fields
* Rank Math fields
* WYSIWYG/rich text fields
* Plain text fields
* JSON fields

= Use Cases =

* Generate SEO meta descriptions (with precise character limits)
* Create article summaries
* Generate FAQs and quiz questions
* Create internal link suggestions
* Generate subtitles and taglines
* Chat with AI about content improvements
* Generate social media snippets with character limits

= Character & Word Limits =

The plugin features explicit character and word limit fields:

* **Character Limits**: Perfect for meta descriptions, Twitter posts, etc.
* **Word Limits**: Ideal for summaries, excerpts, and article sections
* **Smart Enforcement**: AI is instructed to respect limits during generation
* **Safety Net**: Intelligent post-processing ensures limits are never exceeded
* **Complete Sentences**: Truncation always happens at sentence boundaries
* **Per-Field Control**: Set different limits for each custom field

= Token Optimization =

The plugin includes advanced token management:

* **Adaptive Content Truncation**: Sends less article content when generating short outputs
* **Concise System Messages**: Optimized AI instructions to save tokens
* **Dynamic Limits**: Adjusts content based on character/word requirements
* **Configurable Ceilings**: Max output tokens, retry attempts and continuations are adjustable in settings

== External services ==

This plugin sends content to an AI provider to generate text. No request is ever made until a site administrator configures a provider (an API key in the plugin settings, or a provider under Settings → Connectors) and a permitted user actively triggers a feature. The plugin does not collect analytics and does not track visitors or site users.

When an API key is saved in the plugin settings, requests go directly from your server to the selected provider (OpenAI or Google Gemini). When no key is saved, requests go through the WordPress AI Client to the provider connected under Settings → Connectors.

What is sent for content generation, internal links and chat: the post title and post content of the entry being edited (truncated before sending), the custom prompt configured for the field, the chat messages you type, the recent chat history of the current conversation, and your WordPress display name so the assistant can address you by name.

When it is sent: only when a logged-in, permitted user clicks a "Generate" or "Regenerate" button, requests internal links, or sends a chat message.

= OpenAI API =

Used when "OpenAI (GPT)" is selected and an API key is saved. Requests are sent to https://api.openai.com/v1/chat/completions with the data listed above and your API key. To show which models your key can use, the settings page also sends your API key to https://api.openai.com/v1/models when the key is saved, when "Refresh available models" is clicked, or when the settings page is opened after the cached list (kept for 6 hours) has expired.

* Terms of use: https://openai.com/policies/terms-of-use/
* Privacy policy: https://openai.com/policies/privacy-policy/

= Google Gemini API =

Used when "Google Gemini" is selected and an API key is saved. Requests are sent to https://generativelanguage.googleapis.com/ with the data listed above and your API key.

* Terms of service: https://ai.google.dev/gemini-api/terms
* Privacy policy: https://policies.google.com/privacy

= WordPress AI Client / connected providers =

Used when no API key is saved in the plugin and an AI provider is connected under Settings → Connectors (WordPress 7.0+). The request is sent with the data listed above to whichever provider the site owner has connected. Official provider plugins register with the Connectors screen and send the request from your server. Provider terms depend on which connector you enable, for example:

* OpenAI terms: https://openai.com/policies/terms-of-use/
* OpenAI privacy: https://openai.com/policies/privacy-policy/
* Google Gemini terms: https://ai.google.dev/gemini-api/terms
* Google privacy: https://policies.google.com/privacy
* Anthropic terms: https://www.anthropic.com/legal/terms
* Anthropic privacy: https://www.anthropic.com/legal/privacy

= Upstash Search (optional) =

Used only for the internal linking feature, and only when "Use Upstash for Search" is enabled in the Internal Links settings and an Upstash REST URL and token are configured. When it is disabled, the plugin uses the built-in WordPress search instead and contacts no external search service.

What is sent: search queries derived from the keywords of the post being edited, sent to the Upstash Search REST endpoint you configure, together with your Upstash REST token.

When it is sent: only when a user generates internal link suggestions while the option is enabled.

Terms of service: https://upstash.com/trust/terms.pdf
Privacy policy: https://upstash.com/trust/privacy.pdf

== Installation ==

1. Upload the `dropndot-ai-content-custom-fields` folder to your `/wp-content/plugins/` directory
2. Activate the plugin through the 'Plugins' menu in WordPress
3. Go to **Settings → Dropndot AI Content for Custom Fields** and either enter an OpenAI or Google Gemini API key, or connect a provider under **Settings → Connectors**
4. Select which custom fields to enable
5. Set character/word limits for fields that need them
6. Start generating content!

== Frequently Asked Questions ==

= Do I need to pay for the API? =

Yes. You use your own OpenAI or Google Gemini API key, or the provider you connect under Settings → Connectors. Costs depend on that provider's pricing. This plugin does not add its own usage fees. The Usage Guardrails settings let you cap daily requests and tokens.

= I am updating from version 1.0.0. Do I need to enter an API key? =

No. If you do not save an API key, the plugin keeps using the WordPress AI Client and the provider you connected under Settings → Connectors, exactly as in 1.0.0. Your fields, prompts, limits, Quick Prompts and User Access settings are kept. You can add an API key later to call OpenAI or Gemini directly.

= How is my API key stored? =

API keys and the Upstash token are encrypted with a key derived from your site's WordPress salts, stored in a separate option that is not autoloaded, and never printed back into the settings page.

= How do character limits work? =

Simply enter a number in the "Char Limit" field (e.g., 140 for meta descriptions). The AI will generate content within that limit, and a safety net ensures it never exceeds the limit. Truncation happens at sentence boundaries for natural-looking content.

= Can I set both character and word limits? =

Yes, but typically you'd use one or the other. If both are set, the AI will respect whichever limit is more restrictive. For SEO meta descriptions, use character limits. For article summaries, word limits work better.

= What if I don't set any limits? =

That's fine! The plugin will generate content based on your custom prompt instructions. Limits are optional and only needed when you have specific requirements (like SEO meta descriptions).

= Does it work with Advanced Custom Fields (ACF)? =

Yes! It works perfectly with ACF, Meta Box, Pods, and standard WordPress custom fields. It also supports Yoast SEO and Rank Math fields.

= Can I use it on custom post types? =

Yes! The plugin works on all public post types. You can control access through user permissions.

= How does the internal links feature work? =

The AI analyzes your post content, extracts relevant keywords, and matches them with other posts in your database. You can review and approve links before they're applied to your content.

= Can I customize the AI prompts? =

Yes! Each field can have its own custom prompt. You can specify instructions, tone, format requirements, and any other details. The character/word limits are set in separate fields for precision.

= What happens if I get a "Token limit reached" error? =

The plugin includes automatic token optimization. It adaptively reduces article content when generating short outputs (like meta descriptions) to prevent token errors. You can also raise "Max Output Tokens" in the settings.

= Does the chat feature remember conversation history? =

Yes! The chat maintains conversation context throughout your session, allowing for follow-up questions and deeper analysis.

= Can I use both OpenAI and Gemini? =

You can switch between providers in settings; one provider is active at a time. When requests go through Settings → Connectors, the selected provider is a preference and the WordPress AI Client falls back to any compatible connected model.

= How do I control who can use AI features? =

Go to Settings → Dropndot AI Content for Custom Fields → User Access tab. Administrators always have access. You can grant access to specific users who can edit posts.

= What happens to my data when I delete the plugin? =

By default your settings are kept, so re-installing restores them. To erase everything on delete, enable "Remove all plugin data when the plugin is deleted" under Settings → General → Data & Uninstall. Deactivation never deletes data.

== Screenshots ==

1. Settings page with API configuration and connection status
2. Custom fields selection interface with character/word limits
3. Content generation modal with preview
4. Internal links suggestion interface
5. AI chat assistant
6. Quick prompts configuration
7. Usage monitoring
8. User access control for non-admin users
9. Internal links settings

== Changelog ==

= 2.0.0 =
* **NEW**: Connect with your own OpenAI or Google Gemini API key, sent directly to the provider. Sites without a key keep using the WordPress AI Client (Settings → Connectors) as in 1.0.0.
* **NEW**: Choose the Gemini model and the OpenAI model; the OpenAI list is filtered to the models your API key can reach, with a "Refresh available models" button.
* **NEW**: Configurable max output tokens, retry attempts and continuation count.
* **NEW**: Per-user request rate limiting plus site-wide daily request and token quotas with a hard stop (Settings → General → Usage Guardrails).
* **NEW**: Response cache and in-flight de-duplication for identical generations ("Regenerate" always bypasses it).
* **NEW**: Monitoring tab showing today's usage and the 100 most recent AI calls (user, action, model, field, status, tokens), stored in a local log table pruned after 30 days.
* **NEW**: "Remove all plugin data when the plugin is deleted" option (Settings → General → Data & Uninstall). Off by default, so deleting the plugin keeps settings, Quick Prompts and User Access unless you opt in.
* **SECURITY**: API keys and the Upstash token are encrypted at rest (site-salt derived), stored in a dedicated non-autoloaded option, and never rendered into the settings page. An Upstash token saved by 1.0.0 is encrypted and moved there automatically.
* **SECURITY**: Raw provider error messages are no longer shown in the browser; a generic message is displayed and the detail is kept in the local log.
* **CHANGED**: Explicitly granted users must also hold the edit_posts capability. A new `dnaic_user_has_access` filter can adjust access.
* **CHANGED**: The custom field list in the post editor no longer scans all post meta on every page load.
* **CHANGED**: Debug details for internal link errors are only returned when WP_DEBUG is enabled.
* **CHANGED**: Minimum WordPress version lowered to 6.0 for the API key connection; Settings → Connectors still requires WordPress 7.0.

= 1.0.0 =
* Initial release
* Custom field AI generation through the WordPress AI Client
* Character and word limits with sentence-boundary truncation
* Internal links feature
* AI chat assistant
* Custom prompts support
* User access control
* SEO field support (Yoast SEO, Rank Math)
* WYSIWYG formatting support
* Quick prompts feature

== Upgrade Notice ==

= 2.0.0 =
Adds direct OpenAI / Gemini API keys (encrypted), model selection, usage limits, response caching and a monitoring log. Existing settings are kept, and sites without an API key continue to use Settings → Connectors.

= 1.0.0 =
Initial release of Dropndot AI Content for Custom Fields.

== Support ==

For support, please use the plugin's support forum on WordPress.org.

Author: Dropndot Solutions
Author URI: https://dropndot.com

== Technical Details ==

= System Requirements =
* WordPress 6.0 or higher (WordPress 7.0 or higher to use Settings → Connectors)
* PHP 7.4 or higher
* An OpenAI or Google Gemini API key, or an AI provider connected in Settings → Connectors

= Token Management =
The plugin uses intelligent token management:
* Short outputs (< 200 chars): Uses ~5K article content
* Medium outputs (200-500 chars): Uses ~10K article content
* Long outputs (no limits): Uses up to 50K article content
* Adaptive based on custom prompt length

= API Limits =
* Output token ceiling configurable from 256 to 65,500 (default 16,384)
* Bounded automatic continuation for incomplete responses (default 1, max 3)
* Smart content truncation to prevent input token errors

= Privacy =
* API keys are encrypted (site-salt derived) before being stored in WordPress options and are never rendered back into the settings page
* Post content and prompts are sent to the AI provider you configure - see the "External services" section for exactly what is sent and when
* Data handling on the provider side is governed by that provider's own terms and privacy policy
* Every AI request is recorded in a local log table (wp_dnaic_api_log) on your own site, pruned after 30 days
* The plugin itself does not track users and sends no analytics
* All communication uses HTTPS

== Credits ==

Developed by Dropndot Solutions
Copyright (C) 2026 Dropndot Solutions
 