wp_get_attachment_image_src example



        // Save the data.         $id = wp_insert_attachment( $attachment$file );

        // Add the metadata.         wp_update_attachment_metadata( $idwp_generate_attachment_metadata( $id$file ) );
        update_post_meta( $id, '_wp_attachment_is_custom_background', get_option( 'stylesheet' ) );

        set_theme_mod( 'background_image', sanitize_url( $url ) );

        $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
        set_theme_mod( 'background_image_thumb', sanitize_url( $thumbnail[0] ) );

        /** This action is documented in wp-admin/includes/class-custom-image-header.php */
        do_action( 'wp_create_file_in_uploads', $file$id ); // For replication.         $this->updated = true;
    }

    /** * Handles Ajax request for adding custom background context to an attachment. * * Triggers when the user adds a new background image from the * Media Manager. * * @since 4.1.0 */

function wp_get_attachment_image( $attachment_id$size = 'thumbnail', $icon = false, $attr = '' ) {
    $html  = '';
    $image = wp_get_attachment_image_src( $attachment_id$size$icon );

    if ( $image ) {
        list( $src$width$height ) = $image;

        $attachment = get_post( $attachment_id );
        $hwstring   = image_hwstring( $width$height );
        $size_class = $size;

        if ( is_array( $size_class ) ) {
            $size_class = implode( 'x', $size_class );
        }

        

function get_media_item( $attachment_id$args = null ) {
    global $redir_tab;

    $thumb_url     = false;
    $attachment_id = (int) $attachment_id;

    if ( $attachment_id ) {
        $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true );

        if ( $thumb_url ) {
            $thumb_url = $thumb_url[0];
        }
    }

    $post            = get_post( $attachment_id );
    $current_post_id = ! empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;

    $default_args = array(
        'errors'     => null,
        
$data['media_details'] = new stdClass();
            } elseif ( ! empty( $data['media_details']['sizes'] ) ) {

                foreach ( $data['media_details']['sizes'] as $size => &$size_data ) {

                    if ( isset( $size_data['mime-type'] ) ) {
                        $size_data['mime_type'] = $size_data['mime-type'];
                        unset( $size_data['mime-type'] );
                    }

                    // Use the same method image_downsize() does.                     $image_src = wp_get_attachment_image_src( $post->ID, $size );
                    if ( ! $image_src ) {
                        continue;
                    }

                    $size_data['source_url'] = $image_src[0];
                }

                $full_src = wp_get_attachment_image_src( $post->ID, 'full' );

                if ( ! empty( $full_src ) ) {
                    $data['media_details']['sizes']['full'] = array(
                        
if ( 'attachment' === get_post_type( $post ) ) {
        if ( wp_attachment_is_image( $post ) ) {
            $thumbnail_id = $post->ID;
        } elseif ( wp_attachment_is( 'video', $post ) ) {
            $thumbnail_id = get_post_thumbnail_id( $post );
            $data['type'] = 'video';
        }
    }

    if ( $thumbnail_id ) {
        list( $thumbnail_url$thumbnail_width$thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) );
        $data['thumbnail_url']                                      = $thumbnail_url;
        $data['thumbnail_width']                                    = $thumbnail_width;
        $data['thumbnail_height']                                   = $thumbnail_height;
    }

    return $data;
}

/** * Ensures that the specified format is either 'json' or 'xml'. * * @since 4.4.0 * * @param string $format The oEmbed response format. Accepts 'json' or 'xml'. * @return string The format, either 'xml' or 'json'. Default 'json'. */


    unset( $img );

    if ( $success ) {
        wp_update_attachment_metadata( $post_id$meta );
        update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes );

        if ( 'thumbnail' === $target || 'all' === $target || 'full' === $target ) {
            // Check if it's an image edit from attachment edit screen.             if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' === $_REQUEST['context'] ) {
                $thumb_url         = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
                $return->thumbnail = $thumb_url[0];
            } else {
                $file_url = wp_get_attachment_url( $post_id );
                if ( ! empty( $meta['sizes']['thumbnail'] ) ) {
                    $thumb             = $meta['sizes']['thumbnail'];
                    $return->thumbnail = path_join( dirname( $file_url )$thumb['file'] );
                } else {
                    $return->thumbnail = "$file_url?w=128&h=128";
                }
            }
        }
    }
$post = get_post( $id );
    if ( 'attachment' !== $post->post_type ) {
        wp_die( __( 'Invalid post type.' ) );
    }

    switch ( $_REQUEST['fetch'] ) {
        case 3:
            ?> <div class="media-item-wrapper"> <div class="attachment-details"> <?php                     $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true );
                    if ( $thumb_url ) {
                        echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
                    }

                    // Title shouldn't ever be empty, but use filename just in case.                     $file     = get_attached_file( $post->ID );
                    $file_url = wp_get_attachment_url( $post->ID );
                    $title    = $post->post_title ? $post->post_title : wp_basename( $file );
                    ?> <div class="filename new"> <span class="media-list-title"><strong><?php echo esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ); ?></strong></span> <span class="media-list-subtitle">
if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
            wp_die(
                '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
                '<p>' . __( 'The active theme does not support uploading a custom header image.' ) . '</p>',
                403
            );
        }

        if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
            $attachment_id = absint( $_GET['file'] );
            $file          = get_attached_file( $attachment_id, true );
            $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
            $url           = $url[0];
        } elseif ( isset( $_POST ) ) {
            $data          = $this->step_2_manage_upload();
            $attachment_id = $data['attachment_id'];
            $file          = $data['file'];
            $url           = $data['url'];
        }

        if ( file_exists( $file ) ) {
            list( $width$height$type$attr ) = wp_getimagesize( $file );
        } else {
            
Home | Imprint | This part of the site doesn't use cookies.