set_post_thumbnail example


    protected function handle_featured_media( $featured_media$post_id ) {

        $featured_media = (int) $featured_media;
        if ( $featured_media ) {
            $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 );
        }
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 ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
            $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );

            // Accumulate term IDs from terms and terms_names.


    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 );
    }

    wp_die( 0 );
}

/** * Handles retrieving HTML for the featured image via AJAX. * * @since 4.6.0 */
$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'] ) ) {
        $post->page_template = $postarr['page_template'];
        $page_templates      = wp_get_theme()->get_page_templates( $post );

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