Skip to content

WordPress Plugin Development

The AltText.ai WordPress Plugin allows you to provide your own data and logic where needed.

WordPress Hooks

The following are the actions and filters available from the plugin. You can use these to take actions when certain operations happen, and to provide your own custom data during alt text generation.

Alt Text Generated Notification

Purpose: Sent when alt text is generated for a new image attachment.

Action: atai_alttext_generated($attachment_id, $alt_text)

Input Parameters:

  • $attachment_id (integer): The ID of the attachment for which the alt text is being generated.
  • $alt_text (string): The generated alt text for the attachment.

Example usage:

         function on_alttext_generated($attachment_id, $alt_text) {
            // Do something with the alt text or attachment...
         }

         add_action('atai_alttext_generated', 'on_alttext_generated', 10, 2);

Custom SEO Keywords

Purpose: Used when you need to add/modify your own keywords to be used in the alt text generation.

Filter: atai_seo_keywords($keywords, $attachment_id, $parent_post_id)

Input Parameters:

  • $keywords (array): The current list of SEO keywords (obtained from the post data).
  • $attachment_id (integer): The ID of the attachment for which the alt text is being generated.
  • $parent_post_id (integer): The ID of the parent post for the attachment. It may be null if no parent post was found.

Returns: (array) The modified list of SEO keywords to use for alt text generation.

Example usage:

         function custom_atai_seo_keywords($keywords, $attachment_id, $parent_post_id) {
             $additional_keywords = array("cats climbing", "adorable cats", "orange cats", "cats and dogs");
             $modified_keywords = array_merge($keywords, $additional_keywords);
             return $modified_keywords;
         }

         add_filter('atai_seo_keywords', 'custom_atai_seo_keywords', 10, 3);

Custom Ecommerce Data

Purpose: Used to add/modify your own ecommerce data to be used in the generated alt text.

Filter: atai_ecomm_data($ecomm_data, $attachment_id)

Input Parameters:

  • $ecomm_data (array): The current array of ecomm_data. This array will have keys product and [optionally] brand
  • $attachment_id (integer): The ID of the attachment for which the alt text is being generated.

Returns: (array) The ecommerce product and optional brand data to use. The array keys MUST match the following:

  • product: The name of the product.
  • brand: The brand name of the product. This key is OPTIONAL.

Example usage:

         function custom_atai_ecomm_data($ecomm_data, $attachment_id) {
             $my_ecomm_data = array("product" => "Air Jordan", "brand" => "Nike");
             return $my_ecomm_data;
         }

         add_filter('atai_ecomm_data', 'custom_atai_ecomm_data', 10, 2);

Custom API Key

Purpose: You can provide your API key to the plugin via your own system if you do not want to have it available directly in the plugin settings.

Filter: atai_api_key($api_key)

Input Parameters:

  • $api_key (string): The current API key from the plugin settings, if any.

Returns: (string) The API key to use for AltText.ai.

Example usage:

         function custom_atai_api_key($api_key) {
             $my_api_key = "abcdefg123456"; // Retrieve your API key from your own system here
             return $my_api_key;
         }

         add_filter('atai_api_key', 'custom_atai_api_key', 10, 1);

Bulk Actions with Custom Post Types

Purpose: Used when you need your own custom post types to be eligible for the bulk action operations such as refreshing alt text.

Filter: atai_bulk_action_post_types($post_types)

Input Parameters:

  • $post_types (array): The current array of post types allowed for the Bulk Action: Refresh Alt Text feature

Returns: (array) The new list of post types eligible for the Bulk Action: Refresh Alt Text feature.

Example usage:

         function custom_bulk_action_post_types($post_types) {
             $custom_post_types = array("my_type1", "my_type2");
             return array_merge($post_types, $custom_post_types);
         }

         add_filter('atai_bulk_action_post_types', 'custom_bulk_action_post_types', 10, 1);

Skipping specific attachments

Purpose: Used when you need to control the specific attachments (images) which are processed.

Filter: atai_skip_attachment($skip, $attachment_id)

Input Parameters:

  • $skip (boolean): Whether the given attachment should be skipped.
  • $attachment_id (integer): The ID of the attachment for which the alt text is being generated.

Returns: (boolean) TRUE if the attachment should be skipped (no alt text generated), FALSE otherwise.

Example usage:

         function custom_atai_skip_attachment($skip, $attachment_id) {
             // Only allow attachments for MyCustomPostType to be processed.

             // Determine if this attachment is for the custom post type.
             $isCustomPostType = is_my_custom_post_type($attachment_id);

             return $isCustomPostType; // Only process the attachment if it is for the custom post type.
         }

         add_filter('atai_skip_attachment', 'custom_atai_skip_attachment', 10, 2);

Defined Constants

The following constants can be defined to control how the plugin works:

  • ATAI_API_KEY: Hardcodes your AltText.ai API Key to this value. Useful if you do not want to store it in the plugin settings.