• I had built a custom plugin to download an image from a remote server and attach it to a post on save_post

    The function is using download_url and media_handle_sideload

    Everything works fine when saved from the Edit page, but when you save the post from Quick Edit button, that post gets permanently messed up and won’t load at all, or will partially. I fixed this by checking for the global $post variable which doesn’t exist with Quick Edit.

    Regardless, the pages that were “quick saved” are still messed up even with the plugin gone from the server.

    I just checked the server processes with top and found the following when the edit.php page on the aforementioned posts is opened

    11022 www-data 20 0 532796 100584 24488 R 99.3 9.9 0:13.31 php5-fpm

    I have no idea how this is happening or why since the script that caused the problem is gone

    Here is the plugin code:

    function attach_images() {
        global $post;
        $post_id = $post->ID;
        if(!$post_id) {return;}
        $post_type = get_post_type( $post_id );
        $allow_types = array( 'resident', 'event' );
        if( !in_array( $post_type, $allow_types ) ) {
            return;
        }
    
        remove_action( 'save_post', 'attach_images' );
    
        for ($i = 0; $i <= 10; $i++) {
    
            //Check if title in this row already exists, skip if it does
            $gallery_check = get_field( 'gallery', $post_id )[$i]['image'];
            if( is_array( $gallery_check ) ) {
                return;
            }
            //get temp migration values
            $attachment_url = get_field('attachment_' . $i . '_url', $post_id);
            $attachment_title = get_field('attachment_' . $i . '_title', $post_id);
            $attachment_caption = get_field('attachment_' . $i . '_caption', $post_id);
    
            //download image file
            $tmp = download_url( $attachment_url );
            $file_array = array(
                'name' => basename( $attachment_url ),
                'tmp_name' => $tmp
            );
    
            // Check for download errors
            if ( is_wp_error( $tmp ) ) {
                @unlink( $file_array[ 'tmp_name' ] );
                return $tmp;
            }
    
            $image_id = media_handle_sideload( $file_array, $post_id );
            // Check for handle sideload errors.
            if ( is_wp_error( $image_id ) ) {
                @unlink( $file_array['tmp_name'] );
                return $image_id;
            }
            $attachment_url = wp_get_attachment_url( $image_id );
    
            //if uploaded and url is good, add values to row
            if($attachment_url != undefined && $attachment_url != '') {
                $value[] = array(
                    'image' => $image_id,
                    'title' => $attachment_title,
                    'caption' => $attachment_caption
                );
                update_field( 'gallery', $value, $post_id );
            }
        }
    
        add_action( 'save_post', 'attach_images' );
    }
    
    add_action( 'save_post', 'attach_images' );
  • The topic ‘Deleted custom plugin still crashing site, taking up all of CPU’ is closed to new replies.