Navigation überspringen

[Gelöst] uploaded new featured imag but the old one is still attached to the post

This support ticket is created vor 4 Jahren, 8 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Dieses Thema enthält 7 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Nigel vor 4 Jahren, 8 Monaten.

Assistiert von: Nigel.

Author
Artikel
#1574147
Bildschirmfoto 2020-04-04 um 17.20.41.png

Tell us what you are trying to do?

I don´t use the wordpress media library on my toolset forms.

I hope you can give me a solution to the following problem:

when i have created a new post with a featured image and save the post the image is going to the media library.
So far everything as expected.

When i do use an front end edit form to edit that post and change the featured image to a new one the new one is also saved in the media library. But the old one which is not need anymore is still there and not deleted.

Ok i have no problem with this because the admin can go to the media library after some time and delete all the unattached images in wordpress. (
Now the big problem: the old featured images is still attached to the post. Why is that? it should be unattached.
Concerns:
Allright let´s do an example with user profile pics or profile background images.
Users like to change those images after some while. My site will be spammed with a lot of old images, which are not needed anymore.
1. So i need i solution which will delete the old pic when i upload a new one
or
2. so images must be unattached from the post.

Hope you could help me out of this.
Attached is a screenshot of the problem.

Thank you for your time.
Cheers

#1575179

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi Steffen

A media library entry stores the ID of the post it is attached to in the post_parent column of wp_posts, but it is largely meaningless and even WordPress itself doesn't manage it properly when editing posts in the backend.

The same image from the media library can be re-used many times in many posts simultaneously but can only ever record one parent.

The fact that images can be re-used is why they are not deleted when they are replaced with a front-end form (and that won't change).

But you can use the Forms API to delete the image. I'd recommend the cred_before_save_data hook (the cred_save_data hook would be too late as the old featured image would have already been replaced and you wouldn't know which one it was to be able to delete it).

https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

If you try that and get stuck, let me know.

#1575297

Hey Nigel,

thanks for your information. This makes totally sense.
As you can think i haven´t resolved the problem.

i tried this:

add_action('dlt_feat_img_article', 'dlt_feat_img_article',10,1);
function dlt_feat_img_article($form_data)
{
    if ($form_data['id']==1055)
    {
            // remove this field data
            wp_delete_attachment($attachment_id, true);
    }
}

and this

add_action('dlt_feat_img_article', 'dlt_feat_img_article',10,1);
function dlt_feat_img_article($form_data)
{
    if ($form_data['id']==1055)
    {
        if (isset($_POST['_featured_image']))
        {
            // remove this field data
            unlink($_POST['_featured_image']);
        }
    }
}

none is working. could you help me out with this?
Thank you. Cheers!

#1575319

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

That's not right.

The queue is very busy right now, but when I have a little more time later I'll give you a working example, if you don't mind waiting.

#1575359

Hey Nigel,

no problem from my site. take your time.

#1578845

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Thanks for your patience.

It took a little while to put it together.

You can try the following code. You should only need to edit the ID of the edit form. (It's added as an array so you can include multiple forms if required.)

add_action( 'cred_before_save_data', 'ts_delete_old_featured_image' );
function ts_delete_old_featured_image( $form_data ){

    $form_id = array( 316 ); // Edit form ID(s)

    if ( in_array( $form_data['id'], $form_id ) ){

        // get the current featured image
        $featured_image_path = get_the_post_thumbnail_url( $form_data['container_id'], 'full' );
        $featured_image_file = basename( $featured_image_path );

        $new_image_path = $_POST['_featured_image'];
        $new_image_file = basename( $new_image_path );

        if ( $featured_image_file != $new_image_file ) {
            // delete the current featured image
            $featured_image_id = get_post_thumbnail_id( $form_data['container_id'] );
            wp_delete_attachment( $featured_image_id, true );
        }
    }
}

I tested it on my own site, but it is fairly basic, doesn't include any error handling etc., I leave it to you to improve as required.

#1586947

Hey Nigel,

thanks a lot this works great for me. Sorry i was a few days of.
I noticed now that i have the same problem just with a custom image field.
Let´s call this "custom_img".

Could you help me out with the code for a custom image field called: "custom_img"?
This would be great!

cheers

#1588391

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

The code and logic is similar but different if dealing with image custom fields, so I added another function that you can use in addition to the previous one.

I tested this on my own site and it worked, but note that if you have an image field where clients add multiple images then the logic would need modifying again, and it would really be time to call in a developer to help with this.

Here's the code I used, note that you will need to edit the two places that refer to the custom image field (with the wpcf- prefix, i.e. "wpcf-custom_img").

add_action( 'cred_before_save_data', 'ts_delete_old_custom_image' );
function ts_delete_old_custom_image( $form_data ){
 
    $form_id = array( 316 ); // Edit form ID(s)
 
    if ( in_array( $form_data['id'], $form_id ) ){
 
        // get the current custom image
       $custom_image_url = get_post_meta( $form_data['container_id'], 'wpcf-custom_img', true );
       $custom_image_file = basename( $custom_image_url );

       $new_image_maybe_path = $_POST['wpcf-custom_img'][0];
       $new_image_file = basename( $new_image_maybe_path );
       
        if ( $custom_image_file != $new_image_file ) {
            // delete the current custom image
            $custom_image_id = attachment_url_to_postid( $custom_image_url );
            if ( isset( $custom_image_id) ){
                wp_delete_attachment( $custom_image_id, true );
            }
        }
    }
}