• I have some code from WP-Engineer that I used to create a hybrid “Featured Image” plugin for a client site from the first image in a post if there is no existing featured image. It also uses a default image if there is no featured image and there are no images in a post.

    However, if I use the first image in a post as the featured image, I need to remove that image from the post.

    Can I just use wp_delete_attachment or do I also have to search the post text and remove the IMG tag?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Sort of the other way around. You want to keep the attachment, just convert it to a featured image. Use set_post_thumbnail() with the post and attachment IDs. But you do need to remove the img tag to prevent it from appearing twice. Don’t forget to remove any associated caption shortcode as well.

    FWIW, if you did delete the attachment, if the image file were still on the server, it will still appear, but the media editor will not work right. If the image were removed from the server, the img tag’s alt text would still display, and any link will still sort of work. So no matter what, you need to deal with the img tag.

    Thread Starter Dave Navarro, Jr.

    (@dnavarrojr)

    Okay, here’s my problem…

    I have a couple dozen people posting articles to the site. About two-thirds of them use Feature Image to attach a photo. The rest either insert an image into the article or don’t use a photo at all.

    It’s important that we have consistency for images in each post. Each post MUST have an image and the image must be right-aligned at the top of the article.

    So, I have code that checks to see if the user sets a featured image, if they do, then we’re good. If they don’t set a featured image, I want to see if there is an attached photo and if so, use it as the featured image and remove it from within the post. Otherwise it’s duplicated in the post later. If they don’t set a featured image or attach one in the post, I set a default image as the featured image.

    When the post is shown, I display the featured image inside the post, right-aligned. If they inserted an image into the post, then it’s duplicated and looks wrong.

    We’re working on training people, but I’m trying to do as much “in code” as possible to prevent mistakes.

    Moderator bcworkz

    (@bcworkz)

    Understood. Converting the first image attachment into a featured image is straight forward and yields consistent results, but not necessarily optimal results. Removing the img tag and possibly associated caption shortcode can get a bit tricky. Besides the expected search and replace strategy, you need to be sure you are removing all content between the correct tags. Finding the correct closing tag is not always that simple, as tag delimiters may occur in caption content.

    You also need to ensure the img tag relates to the attachment that was converted. Just because it was the first attachment, it may no longer be the first img tag encountered. It may also possibly occur more than once for some reason.

    IMHO, this is not a coding issue, it is human resource issue. As such, you should not code a fix, you should manage your resources. A coding fix only encourages non-compliance since the software fixes the transgression anyway.

    Perhaps non-conforming posts should be rejected upon submission, forcing the author to assign a featured image in order to successfully submit their post. They could of course then assign the featured image without removing it from the content, but this condition is easier to scan for than it is to fix by code. The presence of a featured image URL in content could throw a warning on submission.

    I of course know nothing of your circumstance, so this alternate approach may be completely unworkable. But it could also lead to yet another approach, so I’m throwing it into the ring anyway.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Remove image(s) from a post?’ is closed to new replies.