Using WordPress ‘prepend_attachment()’ PHP function

The prepend_attachment WordPress PHP function wraps an attachment in a paragraph tag before the content.

Usage

To use the prepend_attachment function, pass the content string as an argument:

$content = 'This is a sample content.';
echo prepend_attachment($content);

This will output:

<p>Attachment</p>
This is a sample content.

Parameters

  • $content (string) – The content to which the attachment will be prepended.

More information

See WordPress Developer Resources: prepend_attachment

Examples

Add an Image Attachment

Add an image attachment to the content:

$content = 'This is a post with an image attachment.';
$image = '<img src="image.jpg" alt="Image Attachment">';
echo prepend_attachment($image) . $content;

Add a Video Attachment

Add a video attachment to the content:

$content = 'This is a post with a video attachment.';
$video = '<video src="video.mp4" controls></video>';
echo prepend_attachment($video) . $content;

Add an Audio Attachment

Add an audio attachment to the content:

$content = 'This is a post with an audio attachment.';
$audio = '<audio src="audio.mp3" controls></audio>';
echo prepend_attachment($audio) . $content;

Add a PDF Attachment

Add a PDF attachment to the content:

$content = 'This is a post with a PDF attachment.';
$pdf = '<a href="document.pdf">Download PDF</a>';
echo prepend_attachment($pdf) . $content;

Add a Custom Attachment

Add a custom attachment to the content:

$content = 'This is a post with a custom attachment.';
$custom_attachment = '<div class="custom-attachment">Custom Attachment</div>';
echo prepend_attachment($custom_attachment) . $content;