Saturday, June 11, 2022
HomeWordPress DevelopmentHow you can cease media_sideload_image from operating when deleting a publish?

How you can cease media_sideload_image from operating when deleting a publish?


I’ve a code that downloads a picture and saves it to the media library if you save a publish that makes use of a sure block. The code runs excellent however downloads the picture once more when you delete the publish. How do you forestall media_sideload_image` from operating if you delete a publish?

My code :

operate post_extra_save( $post_id, $publish){
world $pagenow;
if ($pagenow == 'publish.php') { // if creating a brand new publish
    if ( has_blocks( $post->post_content ) ) {
        $blocks = parse_blocks( $post->post_content );
        foreach ( $blocks as $block ) {
            if ( $block['blockName'] === 'acf/opby-link' ) { // if block is used
                static $up to date = false;
                // If title has already been set as soon as, bail.
                if ( $up to date ) {
                    return;
                }
                // Since we're updating this publish's title, set this
                // variable to true to make sure it does not occur once more.
                $up to date = true;    
                require_once(ABSPATH . 'wp-admin/contains/media.php');
                require_once(ABSPATH . 'wp-admin/contains/file.php');
                require_once(ABSPATH . 'wp-admin/contains/picture.php');
                $url     = "https://cdn.pixabay.com/picture/2022/04/08/21/04/buildings-7120297_1280.jpg";
                media_sideload_image($url, $post_id, null, 'id');
            }// Finish if test opby-video.
        }
    }
}
}
add_action( 'save_post', 'post_extra_save', 10, 2 );
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments