delete_post_thumbnail example

if ( 'post' === $post_data['post_type'] ) {
            $error = $this->_toggle_sticky( $post_data$update );
            if ( $error ) {
                return $error;
            }
        }

        if ( isset( $post_data['post_thumbnail'] ) ) {
            // Empty value deletes, non-empty value adds/updates.             if ( ! $post_data['post_thumbnail'] ) {
                delete_post_thumbnail( $post_id );
            } elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) ) {
                return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
            }
            set_post_thumbnail( $post_id$post_data['post_thumbnail'] );
            unset( $content_struct['post_thumbnail'] );
        }

        if ( isset( $post_data['custom_fields'] ) ) {
            $this->set_custom_fields( $post_id$post_data['custom_fields'] );
        }

        
if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) {
            if ( wp_attachment_is( 'audio', $post_id ) ) {
                $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
            } elseif ( wp_attachment_is( 'video', $post_id ) ) {
                $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
            }
        }

        if ( $thumbnail_support ) {
            $thumbnail_id = (int) $postarr['_thumbnail_id'];
            if ( -1 === $thumbnail_id ) {
                delete_post_thumbnail( $post_id );
            } else {
                set_post_thumbnail( $post_id$thumbnail_id );
            }
        }
    }

    clean_post_cache( $post_id );

    $post = get_post( $post_id );

    if ( ! empty( $postarr['page_template'] ) ) {
        
$result = set_post_thumbnail( $post_id$featured_media );
            if ( $result ) {
                return true;
            } else {
                return new WP_Error(
                    'rest_invalid_featured_media',
                    __( 'Invalid featured media ID.' ),
                    array( 'status' => 400 )
                );
            }
        } else {
            return delete_post_thumbnail( $post_id );
        }

    }

    /** * Checks whether the template is valid for the given post. * * @since 4.9.0 * * @param string $template Page template filename. * @param WP_REST_Request $request Request. * @return bool|WP_Error True if template is still valid or if the same as existing value, or false if template not supported. */


    $thumbnail_id = (int) $_POST['thumbnail_id'];

    if ( $json ) {
        check_ajax_referer( "update-post_$post_id);
    } else {
        check_ajax_referer( "set_post_thumbnail-$post_id);
    }

    if ( '-1' == $thumbnail_id ) {
        if ( delete_post_thumbnail( $post_id ) ) {
            $return = _wp_post_thumbnail_html( null, $post_id );
            $json ? wp_send_json_success( $return ) : wp_die( $return );
        } else {
            wp_die( 0 );
        }
    }

    if ( set_post_thumbnail( $post_id$thumbnail_id ) ) {
        $return = _wp_post_thumbnail_html( $thumbnail_id$post_id );
        $json ? wp_send_json_success( $return ) : wp_die( $return );
    }

    
Home | Imprint | This part of the site doesn't use cookies.