Title: prepend_attachment
Published: April 25, 2014
Last modified: February 24, 2026

---

# prepend_attachment( string $content ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#wp--skip-link--target)

Wraps attachment in paragraph tag before content.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#parameters)󠁿

 `$content`stringrequired

## 󠀁[Return](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#return)󠁿

 string

## 󠀁[Source](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post-template.php#L1723)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post-template.php#L1723-L1762)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#hooks)󠁿

 [apply_filters( ‘prepend_attachment’, string $p )](https://developer.wordpress.org/reference/hooks/prepend_attachment/)

Filters the attachment markup to be prepended to the post content.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_attachment_is()](https://developer.wordpress.org/reference/functions/wp_attachment_is/)`wp-includes/post.php` |

Verifies an attachment is of a given type.

  | 
| [has_post_thumbnail()](https://developer.wordpress.org/reference/functions/has_post_thumbnail/)`wp-includes/post-thumbnail-template.php` |

Determines whether a post has an image attached.

  | 
| [get_post_thumbnail_id()](https://developer.wordpress.org/reference/functions/get_post_thumbnail_id/)`wp-includes/post-thumbnail-template.php` |

Retrieves the post thumbnail ID.

  | 
| [wp_get_attachment_link()](https://developer.wordpress.org/reference/functions/wp_get_attachment_link/)`wp-includes/post-template.php` |

Retrieves an attachment page link using an image or icon, if possible.

  | 
| [get_the_ID()](https://developer.wordpress.org/reference/functions/get_the_id/)`wp-includes/post-template.php` |

Retrieves the ID of the current item in the WordPress Loop.

  | 
| [wp_video_shortcode()](https://developer.wordpress.org/reference/functions/wp_video_shortcode/)`wp-includes/media.php` |

Builds the Video shortcode output.

  | 
| [wp_audio_shortcode()](https://developer.wordpress.org/reference/functions/wp_audio_shortcode/)`wp-includes/media.php` |

Builds the Audio shortcode output.

  | 
| [wp_get_attachment_metadata()](https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/)`wp-includes/post.php` |

Retrieves attachment metadata for attachment ID.

  | 
| [wp_get_attachment_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/)`wp-includes/post.php` |

Retrieves the URL for an attachment.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

[Show 6 more](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#)

| Used by | Description | 
| [wp_embed_excerpt_attachment()](https://developer.wordpress.org/reference/functions/wp_embed_excerpt_attachment/)`wp-includes/embed.php` |

Filters the post excerpt for the embed template.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/prepend_attachment/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fprepend_attachment%2F)
before being able to contribute a note or feedback.