get_attachment_link example


            }
        }
    } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {

        // Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101.         if ( is_attachment()
            && ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) )
            && ! $redirect_url
        ) {
            if ( ! empty( $_GET['attachment_id'] ) ) {
                $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
                $redirect_obj = get_post( get_query_var( 'attachment_id' ) );

                if ( $redirect_url ) {
                    $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
                }
            } else {
                $redirect_url = get_attachment_link();
                $redirect_obj = get_post();
            }
        } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
            $redirect_url = get_permalink( get_query_var( 'p' ) );
            

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;
    } elseif ( $size && 'none' !== $size ) {
        $link_text = wp_get_attachment_image( $_post->ID, $size$icon$attr );
    } else {
        $link_text = '';
    }

    if ( '' === trim( $link_text ) ) {
        
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'] ) );
            }
            if ( ! empty( $instance['link_rel'] ) ) {
                $link .= sprintf( ' rel="%s"', esc_attr( $instance['link_rel'] ) );
            }


        $quote  = $url_match[1]; // The quote (single or double).         $url_id = (int) $url_match[2];
        $rel_id = (int) $rel_match[1];

        if ( ! $url_id || ! $rel_id || $url_id != $rel_id || ! str_contains( $url_match[0]$site_url ) ) {
            continue;
        }

        $link    = $link_matches[0][ $key ];
        $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote$link );

        $content = str_replace( $link$replace$content );
    }

    if ( $replace ) {
        $post['post_content'] = $content;
        // Escape data pulled from DB.         $post = add_magic_quotes( $post );

        return wp_update_post( $post );
    }
}

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

/** * Retrieve icon URL and Path. * * @since 2.1.0 * @deprecated 2.5.0 Use wp_get_attachment_image_src() * @see wp_get_attachment_image_src() * * @param int $id Optional. Post ID. * @param bool $fullsize Optional. Whether to have full image. Default false. * @return array Icon URL and full path to file, respectively. */
$post   = get_post( $post );
        $sample = false;
    }

    if ( empty( $post->ID ) ) {
        return false;
    }

    if ( 'page' === $post->post_type ) {
        return get_page_link( $post$leavename$sample );
    } elseif ( 'attachment' === $post->post_type ) {
        return get_attachment_link( $post$leavename );
    } elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) {
        return get_post_permalink( $post$leavename$sample );
    }

    $permalink = get_option( 'permalink_structure' );

    /** * Filters the permalink structure for a post before token replacement occurs. * * Only applies to posts with post_type of 'post'. * * @since 3.0.0 * * @param string $permalink The site's permalink structure. * @param WP_Post $post The post in question. * @param bool $leavename Whether to keep the post name. */
list( $type$subtype ) = array( $attachment->post_mime_type, '' );
    }

    $attachment_url = wp_get_attachment_url( $attachment->ID );
    $base_url       = str_replace( wp_basename( $attachment_url ), '', $attachment_url );

    $response = array(
        'id'            => $attachment->ID,
        'title'         => $attachment->post_title,
        'filename'      => wp_basename( get_attached_file( $attachment->ID ) ),
        'url'           => $attachment_url,
        'link'          => get_attachment_link( $attachment->ID ),
        'alt'           => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'author'        => $attachment->post_author,
        'description'   => $attachment->post_content,
        'caption'       => $attachment->post_excerpt,
        'name'          => $attachment->post_name,
        'status'        => $attachment->post_status,
        'uploadedTo'    => $attachment->post_parent,
        'date'          => strtotime( $attachment->post_date_gmt ) * 1000,
        'modified'      => strtotime( $attachment->post_modified_gmt ) * 1000,
        'menuOrder'     => $attachment->menu_order,
        'mime'          => $attachment->post_mime_type,
        
if ( 0 == $post->post_parent && $insert_into_post_id ) {
            wp_update_post(
                array(
                    'ID'          => $id,
                    'post_parent' => $insert_into_post_id,
                )
            );
        }
    }

    $url = empty( $attachment['url'] ) ? '' : $attachment['url'];
    $rel = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $id ) === $url );

    remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );

    if ( str_starts_with( $post->post_mime_type, 'image' ) ) {
        $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none';
        $size  = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
        $alt   = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';

        // No whitespace-only captions.         $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
        if ( '' === trim( $caption ) ) {
            

        exit;
    }

    if ( isset( $send_id ) ) {
        $attachment = wp_unslash( $_POST['attachments'][ $send_id ] );
        $html       = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';

        if ( ! empty( $attachment['url'] ) ) {
            $rel = '';

            if ( str_contains( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) === $attachment['url'] ) {
                $rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'";
            }

            $html = "<a href='{$attachment['url']}'$rel>$html</a>";
        }

        /** * Filters the HTML markup for a media item sent to the editor. * * @since 2.5.0 * * @see wp_get_attachment_metadata() * * @param string $html HTML markup for a media item sent to the editor. * @param int $send_id The first key from the $_POST['send'] data. * @param array $attachment Array of attachment metadata. */
Home | Imprint | This part of the site doesn't use cookies.