prepend_attachment

The timeline below displays how wordpress function prepend_attachment has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Wraps attachment in paragraph tag before content.
 *
 * @since 2.0.0
 *
 * @param string $content
 * @return string
 */
function prepend_attachment($content)
{
    $post = get_post();
    if (empty($post->post_type) || 'attachment' !== $post->post_type) {
        return $content;
    }
    if (wp_attachment_is('video', $post)) {
        $meta = wp_get_attachment_metadata(get_the_ID());
        $atts = array('src' => wp_get_attachment_url());
        if (!empty($meta['width']) && !empty($meta['height'])) {
            $atts['width'] = (int) $meta['width'];
            $atts['height'] = (int) $meta['height'];
        }
        if (has_post_thumbnail()) {
            $atts['poster'] = wp_get_attachment_url(get_post_thumbnail_id());
        }
        $p = wp_video_shortcode($atts);
    } elseif (wp_attachment_is('audio', $post)) {
        $p = wp_audio_shortcode(array('src' => wp_get_attachment_url()));
    } else {
        $p = '<p class="attachment">';
        // Show the medium sized image representation of the attachment if available, and link to the raw file.
        $p .= wp_get_attachment_link(0, 'medium', false);
        $p .= '</p>';
    }
    /**
     * Filters the attachment markup to be prepended to the post content.
     *
     * @since 2.0.0
     *
     * @see prepend_attachment()
     *
     * @param string $p The attachment HTML output.
     */
    $p = apply_filters('prepend_attachment', $p);
    return "{$p}\n{$content}";
}

WordPress Version: 5.4

/**
 * Wrap attachment in paragraph tag before content.
 *
 * @since 2.0.0
 *
 * @param string $content
 * @return string
 */
function prepend_attachment($content)
{
    $post = get_post();
    if (empty($post->post_type) || 'attachment' !== $post->post_type) {
        return $content;
    }
    if (wp_attachment_is('video', $post)) {
        $meta = wp_get_attachment_metadata(get_the_ID());
        $atts = array('src' => wp_get_attachment_url());
        if (!empty($meta['width']) && !empty($meta['height'])) {
            $atts['width'] = (int) $meta['width'];
            $atts['height'] = (int) $meta['height'];
        }
        if (has_post_thumbnail()) {
            $atts['poster'] = wp_get_attachment_url(get_post_thumbnail_id());
        }
        $p = wp_video_shortcode($atts);
    } elseif (wp_attachment_is('audio', $post)) {
        $p = wp_audio_shortcode(array('src' => wp_get_attachment_url()));
    } else {
        $p = '<p class="attachment">';
        // Show the medium sized image representation of the attachment if available, and link to the raw file.
        $p .= wp_get_attachment_link(0, 'medium', false);
        $p .= '</p>';
    }
    /**
     * Filters the attachment markup to be prepended to the post content.
     *
     * @since 2.0.0
     *
     * @see prepend_attachment()
     *
     * @param string $p The attachment HTML output.
     */
    $p = apply_filters('prepend_attachment', $p);
    return "{$p}\n{$content}";
}

WordPress Version: 4.6

/**
 * Wrap attachment in paragraph tag before content.
 *
 * @since 2.0.0
 *
 * @param string $content
 * @return string
 */
function prepend_attachment($content)
{
    $post = get_post();
    if (empty($post->post_type) || $post->post_type != 'attachment') {
        return $content;
    }
    if (wp_attachment_is('video', $post)) {
        $meta = wp_get_attachment_metadata(get_the_ID());
        $atts = array('src' => wp_get_attachment_url());
        if (!empty($meta['width']) && !empty($meta['height'])) {
            $atts['width'] = (int) $meta['width'];
            $atts['height'] = (int) $meta['height'];
        }
        if (has_post_thumbnail()) {
            $atts['poster'] = wp_get_attachment_url(get_post_thumbnail_id());
        }
        $p = wp_video_shortcode($atts);
    } elseif (wp_attachment_is('audio', $post)) {
        $p = wp_audio_shortcode(array('src' => wp_get_attachment_url()));
    } else {
        $p = '<p class="attachment">';
        // show the medium sized image representation of the attachment if available, and link to the raw file
        $p .= wp_get_attachment_link(0, 'medium', false);
        $p .= '</p>';
    }
    /**
     * Filters the attachment markup to be prepended to the post content.
     *
     * @since 2.0.0
     *
     * @see prepend_attachment()
     *
     * @param string $p The attachment HTML output.
     */
    $p = apply_filters('prepend_attachment', $p);
    return "{$p}\n{$content}";
}

WordPress Version: 4.2

/**
 * Wrap attachment in paragraph tag before content.
 *
 * @since 2.0.0
 *
 * @param string $content
 * @return string
 */
function prepend_attachment($content)
{
    $post = get_post();
    if (empty($post->post_type) || $post->post_type != 'attachment') {
        return $content;
    }
    if (wp_attachment_is('video', $post)) {
        $meta = wp_get_attachment_metadata(get_the_ID());
        $atts = array('src' => wp_get_attachment_url());
        if (!empty($meta['width']) && !empty($meta['height'])) {
            $atts['width'] = (int) $meta['width'];
            $atts['height'] = (int) $meta['height'];
        }
        if (has_post_thumbnail()) {
            $atts['poster'] = wp_get_attachment_url(get_post_thumbnail_id());
        }
        $p = wp_video_shortcode($atts);
    } elseif (wp_attachment_is('audio', $post)) {
        $p = wp_audio_shortcode(array('src' => wp_get_attachment_url()));
    } else {
        $p = '<p class="attachment">';
        // show the medium sized image representation of the attachment if available, and link to the raw file
        $p .= wp_get_attachment_link(0, 'medium', false);
        $p .= '</p>';
    }
    /**
     * Filter the attachment markup to be prepended to the post content.
     *
     * @since 2.0.0
     *
     * @see prepend_attachment()
     *
     * @param string $p The attachment HTML output.
     */
    $p = apply_filters('prepend_attachment', $p);
    return "{$p}\n{$content}";
}

WordPress Version: 4.1

/**
 * Wrap attachment in paragraph tag before content.
 *
 * @since 2.0.0
 *
 * @param string $content
 * @return string
 */
function prepend_attachment($content)
{
    $post = get_post();
    if (empty($post->post_type) || $post->post_type != 'attachment') {
        return $content;
    }
    if (0 === strpos($post->post_mime_type, 'video')) {
        $meta = wp_get_attachment_metadata(get_the_ID());
        $atts = array('src' => wp_get_attachment_url());
        if (!empty($meta['width']) && !empty($meta['height'])) {
            $atts['width'] = (int) $meta['width'];
            $atts['height'] = (int) $meta['height'];
        }
        if (has_post_thumbnail()) {
            $atts['poster'] = wp_get_attachment_url(get_post_thumbnail_id());
        }
        $p = wp_video_shortcode($atts);
    } elseif (0 === strpos($post->post_mime_type, 'audio')) {
        $p = wp_audio_shortcode(array('src' => wp_get_attachment_url()));
    } else {
        $p = '<p class="attachment">';
        // show the medium sized image representation of the attachment if available, and link to the raw file
        $p .= wp_get_attachment_link(0, 'medium', false);
        $p .= '</p>';
    }
    /**
     * Filter the attachment markup to be prepended to the post content.
     *
     * @since 2.0.0
     *
     * @see prepend_attachment()
     *
     * @param string $p The attachment HTML output.
     */
    $p = apply_filters('prepend_attachment', $p);
    return "{$p}\n{$content}";
}

WordPress Version: 4.0

/**
 * Wrap attachment in <<p>> element before content.
 *
 * @since 2.0.0
 *
 * @param string $content
 * @return string
 */
function prepend_attachment($content)
{
    $post = get_post();
    if (empty($post->post_type) || $post->post_type != 'attachment') {
        return $content;
    }
    if (0 === strpos($post->post_mime_type, 'video')) {
        $meta = wp_get_attachment_metadata(get_the_ID());
        $atts = array('src' => wp_get_attachment_url());
        if (!empty($meta['width']) && !empty($meta['height'])) {
            $atts['width'] = (int) $meta['width'];
            $atts['height'] = (int) $meta['height'];
        }
        if (has_post_thumbnail()) {
            $atts['poster'] = wp_get_attachment_url(get_post_thumbnail_id());
        }
        $p = wp_video_shortcode($atts);
    } elseif (0 === strpos($post->post_mime_type, 'audio')) {
        $p = wp_audio_shortcode(array('src' => wp_get_attachment_url()));
    } else {
        $p = '<p class="attachment">';
        // show the medium sized image representation of the attachment if available, and link to the raw file
        $p .= wp_get_attachment_link(0, 'medium', false);
        $p .= '</p>';
    }
    /**
     * Filter the attachment markup to be prepended to the post content.
     *
     * @since 2.0.0
     *
     * @see prepend_attachment()
     *
     * @param string $p The attachment HTML output.
     */
    $p = apply_filters('prepend_attachment', $p);
    return "{$p}\n{$content}";
}

WordPress Version: 3.9

/**
 * Wrap attachment in <<p>> element before content.
 *
 * @since 2.0.0
 *
 * @param string $content
 * @return string
 */
function prepend_attachment($content)
{
    $post = get_post();
    if (empty($post->post_type) || $post->post_type != 'attachment') {
        return $content;
    }
    if (0 === strpos($post->post_mime_type, 'video')) {
        $meta = wp_get_attachment_metadata(get_the_ID());
        $atts = array('src' => wp_get_attachment_url());
        if (!empty($meta['width']) && !empty($meta['height'])) {
            $atts['width'] = (int) $meta['width'];
            $atts['height'] = (int) $meta['height'];
        }
        $p = wp_video_shortcode($atts);
    } elseif (0 === strpos($post->post_mime_type, 'audio')) {
        $p = wp_audio_shortcode(array('src' => wp_get_attachment_url()));
    } else {
        $p = '<p class="attachment">';
        // show the medium sized image representation of the attachment if available, and link to the raw file
        $p .= wp_get_attachment_link(0, 'medium', false);
        $p .= '</p>';
    }
    /**
     * Filter the attachment markup to be prepended to the post content.
     *
     * @since 2.0.0
     *
     * @see prepend_attachment()
     *
     * @param string $p The attachment HTML output.
     */
    $p = apply_filters('prepend_attachment', $p);
    return "{$p}\n{$content}";
}

WordPress Version: 3.7

/**
 * Wrap attachment in <<p>> element before content.
 *
 * @since 2.0.0
 * @uses apply_filters() Calls 'prepend_attachment' hook on HTML content.
 *
 * @param string $content
 * @return string
 */
function prepend_attachment($content)
{
    $post = get_post();
    if (empty($post->post_type) || $post->post_type != 'attachment') {
        return $content;
    }
    $p = '<p class="attachment">';
    // show the medium sized image representation of the attachment if available, and link to the raw file
    $p .= wp_get_attachment_link(0, 'medium', false);
    $p .= '</p>';
    $p = apply_filters('prepend_attachment', $p);
    return "{$p}\n{$content}";
}