wp_get_attachment_url example

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";
                }
            }
        }
    } else {
        $delete = true;
    }

    

    protected function _prepare_media_item( $media_item$thumbnail_size = 'thumbnail' ) {
        $_media_item = array(
            'attachment_id'    => (string) $media_item->ID,
            'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
            'parent'           => $media_item->post_parent,
            'link'             => wp_get_attachment_url( $media_item->ID ),
            'title'            => $media_item->post_title,
            'caption'          => $media_item->post_excerpt,
            'description'      => $media_item->post_content,
            'metadata'         => wp_get_attachment_metadata( $media_item->ID ),
            'type'             => $media_item->post_mime_type,
        );

        $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
        if ( $thumbnail_src ) {
            $_media_item['thumbnail'] = $thumbnail_src[0];
        } else {
            
            $value = maybe_unserialize( $value );

            // Handle header image as special case since setting has a legacy format.             if ( 'header_image' === $name ) {
                $name     = 'header_image_data';
                $metadata = wp_get_attachment_metadata( $value );
                if ( empty( $metadata ) ) {
                    continue;
                }
                $value = array(
                    'attachment_id' => $value,
                    'url'           => wp_get_attachment_url( $value ),
                    'height'        => $metadata['height'],
                    'width'         => $metadata['width'],
                );
            } elseif ( 'background_image' === $name ) {
                $value = wp_get_attachment_url( $value );
            }

            if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) {
                $this->set_post_value( $name$value );
                $this->pending_starter_content_settings_ids[] = $name;
            }
        }

function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false, $permalink = false) {
    _deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_link()' );
    $id = (int) $id;
    $_post = get_post($id);

    if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
        return __('Missing Attachment');

    if ( $permalink )
        $url = get_attachment_link($_post->ID);

    $post_title = esc_attr($_post->post_title);

    $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize$max_dims);
    return "<a href='$url' title='$post_title'>$innerHTML</a>";
}


        $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id )$attachment_id$size );
    }

    /** * Filters the returned path or URL of the current image. * * @since 2.9.0 * * @param string|false $filepath File path or URL to current image, or false. * @param int $attachment_id Attachment ID. * @param string|int[] $size Requested image size. Can be any registered image size name, or * an array of width and height values in pixels (in that order). */

    $out = apply_filters( 'image_downsize', false, $id$size );

    if ( $out ) {
        return $out;
    }

    $img_url          = wp_get_attachment_url( $id );
    $meta             = wp_get_attachment_metadata( $id );
    $width            = 0;
    $height           = 0;
    $is_intermediate  = false;
    $img_url_basename = wp_basename( $img_url );

    /* * If the file isn't an image, attempt to replace its URL with a rendered image from its meta. * Otherwise, a non-image type could be returned. */
    if ( ! $is_image ) {
        
/** * Creates an attachment 'object'. * * @since 4.3.0 * * @param string $cropped Cropped image URL. * @param int $parent_attachment_id Attachment ID of parent image. * @return array An array with attachment object data. */
    public function create_attachment_object( $cropped$parent_attachment_id ) {
        $parent     = get_post( $parent_attachment_id );
        $parent_url = wp_get_attachment_url( $parent->ID );
        $url        = str_replace( wp_basename( $parent_url )wp_basename( $cropped )$parent_url );

        $size       = wp_getimagesize( $cropped );
        $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';

        $attachment = array(
            'ID'             => $parent_attachment_id,
            'post_title'     => wp_basename( $cropped ),
            'post_content'   => $url,
            'post_mime_type' => $image_type,
            'guid'           => $url,
            

    public function render_media( $instance ) {
        $instance   = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' )$instance );
        $attachment = null;

        if ( $this->is_attachment_with_mime_type( $instance['attachment_id']$this->widget_options['mime_type'] ) ) {
            $attachment = get_post( $instance['attachment_id'] );
        }

        $src = $instance['url'];
        if ( $attachment ) {
            $src = wp_get_attachment_url( $attachment->ID );
        }

        if ( empty( $src ) ) {
            return;
        }

        $youtube_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
        $vimeo_pattern   = '#^https?://(.+\.)?vimeo\.com/.*#';

        if ( $attachment || preg_match( $youtube_pattern$src ) || preg_match( $vimeo_pattern$src ) ) {
            add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );

            
$actions['delete'] = sprintf(
                    '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
                    esc_url( wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ) ),
                    $show_confirmation,
                    /* translators: %s: Attachment title. */
                    esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' )$att_title ) ),
                    __( 'Delete Permanently' )
                );
            }
        }

        $attachment_url = wp_get_attachment_url( $post->ID );

        if ( ! $this->is_trash ) {
            $permalink = get_permalink( $post->ID );

            if ( $permalink ) {
                $actions['view'] = sprintf(
                    '<a href="%s" aria-label="%s" rel="bookmark">%s</a>',
                    esc_url( $permalink ),
                    /* translators: %s: Attachment title. */
                    esc_attr( sprintf( __( 'View &#8220;%s&#8221;' )$att_title ) ),
                    __( 'View' )
                );

function wp_get_attachment_link( $post = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
    $_post = get_post( $post );

    if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! wp_get_attachment_url( $_post->ID ) ) {
        return __( 'Missing Attachment' );
    }

    $url = wp_get_attachment_url( $_post->ID );

    if ( $permalink ) {
        $url = get_attachment_link( $_post->ID );
    }

    if ( $text ) {
        $link_text = $text;
    }
$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped$attachment_id ); // For replication.
        $attachment = $this->create_attachment_object( $cropped$attachment_id );

        if ( ! empty( $_POST['create-new-attachment'] ) ) {
            unset( $attachment['ID'] );
        }

        // Update the attachment.         $attachment_id = $this->insert_attachment( $attachment$cropped );

        $url = wp_get_attachment_url( $attachment_id );
        $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );

        // Cleanup.         $medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original )$original );
        if ( file_exists( $medium ) ) {
            wp_delete_file( $medium );
        }

        if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
            wp_delete_file( $original );
        }

        

            } else {
                $data['media_details']['sizes'] = new stdClass();
            }
        }

        if ( in_array( 'post', $fields, true ) ) {
            $data['post'] = ! empty( $post->post_parent ) ? (int) $post->post_parent : null;
        }

        if ( in_array( 'source_url', $fields, true ) ) {
            $data['source_url'] = wp_get_attachment_url( $post->ID );
        }

        if ( in_array( 'missing_image_sizes', $fields, true ) ) {
            require_once ABSPATH . 'wp-admin/includes/image.php';
            $data['missing_image_sizes'] = array_keys( wp_get_missing_image_subsizes( $post->ID ) );
        }

        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';

        $data = $this->filter_response_by_context( $data$context );

        

function wp_get_original_image_url( $attachment_id ) {
    if ( ! wp_attachment_is_image( $attachment_id ) ) {
        return false;
    }

    $image_url = wp_get_attachment_url( $attachment_id );

    if ( ! $image_url ) {
        return false;
    }

    $image_meta = wp_get_attachment_metadata( $attachment_id );

    if ( empty( $image_meta['original_image'] ) ) {
        $original_image_url = $image_url;
    } else {
        $original_image_url = path_join( dirname( $image_url )$image_meta['original_image'] );
    }
$image = '<img';

            foreach ( $attr as $name => $value ) {
                $image .= ' ' . $name . '="' . $value . '"';
            }

            $image .= ' />';
        } // End if().
        $url = '';
        if ( 'file' === $instance['link_type'] ) {
            $url = $attachment ? wp_get_attachment_url( $attachment->ID ) : $instance['url'];
        } elseif ( $attachment && 'post' === $instance['link_type'] ) {
            $url = get_attachment_link( $attachment->ID );
        } elseif ( 'custom' === $instance['link_type'] && ! empty( $instance['link_url'] ) ) {
            $url = $instance['link_url'];
        }

        if ( $url ) {
            $link = sprintf( '<a href="%s"', esc_url( $url ) );
            if ( ! empty( $instance['link_classes'] ) ) {
                $link .= sprintf( ' class="%s"', esc_attr( $instance['link_classes'] ) );
            }
            
'meta_value' => get_option( 'stylesheet' ),
            'orderby'    => 'none',
            'nopaging'   => true,
        )
    );

    if ( empty( $headers ) ) {
        return array();
    }

    foreach ( (array) $headers as $header ) {
        $url          = sanitize_url( wp_get_attachment_url( $header->ID ) );
        $header_data  = wp_get_attachment_metadata( $header->ID );
        $header_index = $header->ID;

        $header_images[ $header_index ]                  = array();
        $header_images[ $header_index ]['attachment_id'] = $header->ID;
        $header_images[ $header_index ]['url']           = $url;
        $header_images[ $header_index ]['thumbnail_url'] = $url;
        $header_images[ $header_index ]['alt_text']      = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );

        if ( isset( $header_data['attachment_parent'] ) ) {
            $header_images[ $header_index ]['attachment_parent'] = $header_data['attachment_parent'];
        }
Home | Imprint | This part of the site doesn't use cookies.