Home Forums Plugin Support WP User Frontend Pro Featured image automatically created when posting

This topic is: not resolved

This topic contains 1 reply, has 2 voices, and was last updated by  Nayem 3 years, 10 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #54080

    Alexandre
    Member
    Post count: 73

    Hi

    We have set this function to have a featured image automatically created when posting

    We thought we would share this with the community

    The first image of the post is used for the featured image

    You can also set in the code below the id of a picture to be the “default” featured image in case there is no image in the post

    Copy this function in the functions.php of your theme

    What would be nice to add is the possibility to automatically update the featured image if editing the post

    if somebody knows how to do that !

    /* WP USER FRONTEND : CREE AUTOMATIQUEMENT UNE FEATURED IMAGE AVEC LA PREMIERE IMAGE DU POST */
    function wpforce_featured() {
              global $post;
              $already_has_thumb = has_post_thumbnail($post->ID);
                  if (!$already_has_thumb)  {
                  $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                              if ($attached_image) {
                                    foreach ($attached_image as $attachment_id => $attachment) {
                                    set_post_thumbnail($post->ID, $attachment_id);
                                    }
                               } else {
                                    set_post_thumbnail($post->ID, '1052');
                               }
                            }
          }  //end function
    add_action('the_post', 'wpforce_featured');
    add_action('save_post', 'wpforce_featured');
    add_action('draft_to_publish', 'wpforce_featured');
    add_action('new_to_publish', 'wpforce_featured');
    add_action('pending_to_publish', 'wpforce_featured');
    add_action('future_to_publish', 'wpforce_featured');
    add_action('update_post', 'wpforce_featured');
    #54173

    Nayem
    Moderator
    Post count: 6480

    Hello Alexandre,

    Thank you for sharing this. Hope this will be helpful for others.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Featured image automatically created when posting’ is closed to new replies.