Plugin Directory

Changeset 3470201


Ignore:
Timestamp:
02/26/2026 11:13:36 AM (4 weeks ago)
Author:
dhrumilk
Message:

Added OpenAI-powered AI featured image generation for posts without images.

Location:
featured-image-from-content
Files:
23 added
3 edited

Legend:

Unmodified
Added
Removed
  • featured-image-from-content/trunk/featured-image-from-content.php

    r3469109 r3470201  
    44 * Plugin URI:  https://wordpress.org/plugins/featured-image-from-content/
    55 * Description: This plugin is useful for automatically setting featured images from post content if no featured images are available.
    6  * Version: 1.6
     6 * Version: 1.7
    77 * Requires at least:   5.2
    88 * Requires PHP:    5.6
     
    1111 * License: GPL v2 or later
    1212 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    13  * Text Domain: featured-image-from-content
     13 * Text Domain: FIFC
    1414 */
    15 
    16 if (!defined('ABSPATH')) {
    17     exit; // Exit if accessed directly
     15if ( ! defined( 'ABSPATH' ) ) {
     16    exit;
    1817}
    1918
    20 // Register the admin page
    21 function fifc_featured_image_from_content_admin_page() {
    22     add_submenu_page(
    23         'options-general.php',
    24         'Featured Image from Content',
    25         'Featured Image from Content',
    26         'manage_options',
    27         'featured_image_from_content',
    28         'fifc_featured_image_from_content_render_admin_page'
    29     );
    30 }
    31 add_action('admin_menu', 'fifc_featured_image_from_content_admin_page');
     19define( 'FIFC_VERSION', '1.7' );
     20define( 'FIFC_PATH', plugin_dir_path( __FILE__ ) );
     21define( 'FIFC_URL', plugin_dir_url( __FILE__ ) );
    3222
    33 // Render the admin page
    34 function fifc_featured_image_from_content_render_admin_page() {
    35     ?>
    36     <div class="wrap">
    37         <h1>Featured Image from Content</h1>
    38         <form method="post" action="options.php">
    39             <?php settings_fields('fifc_featured_image_from_content_settings'); ?>
    40             <?php do_settings_sections('fifc_featured_image_from_content_settings'); ?>
    41             <table class="form-table">
    42                 <tr valign="top">
    43                     <th scope="row">Select Post Type:</th>
    44                     <td>
    45                         <?php
    46                         $post_types = get_post_types(['public' => true], 'objects');
    47                         foreach ($post_types as $post_type) {
    48                             if ($post_type->name !== 'attachment') {
    49                                 $checked = in_array($post_type->name, get_option('fifc_featured_image_from_content_post_types', [])) ? 'checked' : '';
    50                                 echo '<label><input type="checkbox" name="fifc_featured_image_from_content_post_types[]" value="' . esc_attr($post_type->name) . '" ' . $checked . '> ' . esc_html($post_type->label) . '</label><br>';
    51                             }
    52                         }
    53                         ?>
    54                     </td>
    55                 </tr>
    56             </table>
    57             <?php submit_button(); ?>
    58         </form>
    59     </div>
    60     <?php
     23require_once FIFC_PATH . 'includes/class-fifc-plugin.php';
     24
     25function fifc_run_plugin() {
     26    $plugin = new FIFC_Plugin();
     27    $plugin->init();
    6128}
    6229
    63 // Initialize plugin settings
    64 function fifc_featured_image_from_content_init() {
    65     register_setting(
    66         'fifc_featured_image_from_content_settings',
    67         'fifc_featured_image_from_content_post_types',
    68         'fifc_featured_image_from_content_sanitize_post_types'
    69     );
    70 }
    71 add_action('admin_init', 'fifc_featured_image_from_content_init');
    72 
    73 // Sanitize selected post types
    74 function fifc_featured_image_from_content_sanitize_post_types($input) {
    75     $allowed_post_types = array_keys(get_post_types(['public' => true]));
    76     $selected_post_types = [];
    77     if (is_array($input)) {
    78         foreach ($input as $post_type) {
    79             if (in_array($post_type, $allowed_post_types)) {
    80                 $selected_post_types[] = $post_type;
    81             }
    82         }
    83     }
    84     return $selected_post_types;
    85 }
    86 
    87 // Set featured image from content
    88 function fifc_set_featured_image_from_content($post_ID) {
    89     $post = get_post($post_ID);
    90     $post_types = get_option('fifc_featured_image_from_content_post_types', []);
    91     if (in_array($post->post_type, $post_types) && !has_post_thumbnail($post_ID)) {
    92         $content = $post->post_content;
    93         preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
    94         if (!empty($matches)) {
    95             $image_url = $matches[1];
    96 
    97             // Check if the image already exists in the media library
    98             $existing_image_id = fifc_get_image_id_from_url($image_url);
    99             if ($existing_image_id) {
    100                 set_post_thumbnail($post_ID, $existing_image_id);
    101             } else {
    102                 // Download the image and create a new attachment
    103                 $upload_dir = wp_upload_dir();
    104                 $image_data = wp_remote_get($image_url);
    105                 if (!is_wp_error($image_data) && $image_data['response']['code'] === 200) {
    106                     $image_name = basename($image_url);
    107                     $filename = $upload_dir['path'] . '/' . $image_name;
    108 
    109                     // Replace file_put_contents with wp_upload_bits to save the image in the upload directory
    110                     $file = wp_upload_bits($image_name, null, $image_data['body']);
    111                     if (!$file['error']) {
    112                         $attachment = array(
    113                             'post_mime_type' => $file['type'],
    114                             'post_title' => sanitize_file_name($image_name),
    115                             'post_content' => '',
    116                             'post_status' => 'inherit'
    117                         );
    118                         $attach_id = wp_insert_attachment($attachment, $file['file'], $post_ID);
    119                         require_once(ABSPATH . 'wp-admin/includes/image.php');
    120                         $attach_data = wp_generate_attachment_metadata($attach_id, $file['file']);
    121                         wp_update_attachment_metadata($attach_id, $attach_data);
    122                         set_post_thumbnail($post_ID, $attach_id);
    123                     }
    124                 }
    125             }
    126         }
    127     }
    128 }
    129 add_action('save_post', 'fifc_set_featured_image_from_content');
    130 
    131 // Helper function to get image ID from its URL
    132 function fifc_get_image_id_from_url($image_url) {
    133     $image_id = attachment_url_to_postid($image_url);
    134     return $image_id;
    135 }
     30fifc_run_plugin();
  • featured-image-from-content/trunk/readme.txt

    r3469299 r3470201  
    22Contributors: dhrumilk, dhruvishah2203, mikinc860
    33Donate link: https://buymeacoffee.com/dhrumilk
    4 Tags: featured image, auto featured image, set featured image automatically, image from content, auto set thumbnail
     4Tags: auto featured image, set featured image automatically, image from content, ai featured image, openai image generator
    55Requires at least: 5.2
    66Tested up to: 6.9
    77Requires PHP: 5.6
    8 Stable tag: 1.6
     8Stable tag: 1.7
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
    1111
    12 Automatically set the featured image from the first image inside your post content. Save time and streamline your WordPress publishing workflow.
     12Automatically set the featured image from the first image inside your post content. If no image exists, generate one using OpenAI. Save time and streamline your WordPress publishing workflow.
    1313
    1414== Description ==
     
    1616Featured Image from Content is a lightweight and powerful WordPress plugin that automatically sets the featured image (post thumbnail) using the first image found inside your post content.
    1717
    18 If you manage a blog, news website, magazine, affiliate site, or high-volume content website, manually assigning featured images to every post can be time-consuming. This plugin eliminates that manual work and ensures every post has a visually appealing featured image.
     18NEW: If your post does not contain any image, the plugin can automatically generate a featured image using OpenAI.
    1919
    20 Perfect for bloggers, agencies, developers, and automated content websites.
     20If you manage a blog, news website, magazine, affiliate site, or high-volume content website, manually assigning featured images to every post can be time-consuming. This plugin eliminates that manual work and ensures every post always has a visually appealing featured image.
     21
     22Perfect for bloggers, agencies, developers, AI-powered websites, and automated content platforms.
    2123
    2224= Why Use This Plugin? =
    2325
    2426• Automatically assigns featured images 
     27• AI generates featured images when none exist 
    2528• Prevents publishing posts without thumbnails 
    2629• Improves SEO and social media sharing 
     
    3437The plugin scans your post content and automatically selects the first image to set as the featured image when you publish or update a post.
    3538
     39= AI Featured Image Generation (OpenAI Integration) =
     40If no image is found in the content, the plugin can generate a relevant featured image using OpenAI.
     41
     42To use this feature:
     431. Add your OpenAI API Key in the plugin settings.
     442. Enable AI image generation.
     453. When a post has no image, the plugin automatically generates one based on the post content or title.
     46
     47This ensures every post always has a featured image.
     48
    3649= Supports Multiple Post Types =
    37 Choose which post types (Posts, Pages, Custom Post Types) should have featured images automatically assigned.
     50Choose which post types (Posts, Pages, Custom Post Types) should have featured images automatically assigned or generated.
    3851
    3952= SEO Friendly =
     
    4457• Overall content engagement 
    4558
    46 = Perfect for Automated Websites =
     59= Perfect for Automated & AI Websites =
    4760Works great with:
    4861• Auto blogging websites 
     62• AI content generation websites 
    4963• Content importers 
    5064• News websites 
     
    5771= How It Works =
    5872
     73Case 1: Post Contains Image
    59741. Write or import content that contains at least one image.
    60752. Publish or update the post.
     
    62774. That image is set as the featured image.
    6378
     79Case 2: Post Has No Image
     801. Write or import content without any image.
     812. Make sure OpenAI API Key is added in settings.
     823. Publish or update the post.
     834. The plugin generates a relevant image using OpenAI.
     845. The generated image is automatically set as the featured image.
     85
    6486No manual action required.
     87
     88= OpenAI API Setup =
     89
     901. Go to Settings → Featured Image from Content.
     912. Enter your OpenAI API Key.
     923. Save changes.
     934. Enable AI Image Generation option.
     94
     95Note: OpenAI usage may incur charges based on your OpenAI account plan.
    6596
    6697= Compatibility =
     
    75106
    76107• Automatically assign featured images for bulk imported posts 
     108• Generate AI images for text-only posts 
    77109• Ensure consistent blog archive layouts 
    78110• Maintain professional content presentation 
     
    851173. Go to Settings → Featured Image from Content.
    861184. Select the post types where you want to automatically set featured images.
    87 5. Save changes.
    88 6. Done!
     1195. (Optional) Add your OpenAI API Key to enable AI image generation.
     1206. Save changes.
     1217. Done!
    89122
    90123== Frequently Asked Questions ==
    91124
    92125= Does this overwrite existing featured images? =
    93 No. The plugin only sets a featured image if one is not already assigned.
     126No. The plugin only sets or generates a featured image if one is not already assigned.
    94127
    95128= Does it work with custom post types? =
     
    97130
    98131= What if a post has no images? =
    99 The plugin will not assign a featured image if no image is found in the content.
     132If OpenAI API Key is configured, the plugin will generate a featured image automatically. Otherwise, no image will be assigned.
     133
     134= Is the OpenAI feature mandatory? =
     135No. The plugin works perfectly without OpenAI. AI image generation is optional.
    100136
    101137= Does it affect website performance? =
     
    106142
    107143== Changelog ==
     144
     145= 1.7 =
     146* Added OpenAI-powered AI featured image generation for posts without images.
     147* Added OpenAI API Key configuration option in the settings page.
     148* Improved overall performance and code optimization.
     149* Minor bug fixes and general improvements.
    108150
    109151= 1.6 =
     
    129171
    130172== Screenshots ==
    131 
    132 1. Plugin settings page where you can select post types.
    133 2. Example showing automatic featured image assignment.
     1731. Plugin settings page where you can select post types and OpenAI API key configuration.
Note: See TracChangeset for help on using the changeset viewer.