Using WordPress ‘atom_enclosure()’ PHP function

The atom_enclosure() WordPress PHP function displays the atom enclosure for the current post. It uses the global $post to check whether the post requires a password and if the user has the password for the post. If not, it returns before displaying. The function also uses get_post_custom() to get the post’s ‘enclosure’ metadata field and parses the value to display the enclosure(s). The enclosure(s) consist of link HTML tags with a URI and other attributes.

Usage

atom_enclosure();

This function does not return any value. It directly outputs the atom enclosure of the current post.

Parameters

This function does not accept any parameters.

More information

See WordPress Developer Resources: atom_enclosure()

This function is part of WordPress core and is not deprecated as of the current version.

Examples

Basic Usage

To display the atom enclosure for a post, simply call the function. This will output the atom enclosure for the current post if it exists.

atom_enclosure();

Using in The Loop

You can use atom_enclosure() in The Loop to display the atom enclosure for each post.

while ( have_posts() ) {
    the_post();
    // Display the atom enclosure for the current post
    atom_enclosure();
}

This will output the atom enclosure for each post in The Loop.

Checking Post Password

If the post has a password, the atom enclosure will only be displayed if the user has the password. If the user does not have the password, the function will return before displaying the enclosure.

if ( post_password_required() ) {
    echo 'This post is protected by a password.';
} else {
    atom_enclosure();
}

Display Enclosures in Custom Query Loop

You can also use atom_enclosure() in a custom WP_Query loop.

$query = new WP_Query( array( 'post_type' => 'post' ) );
while ( $query->have_posts() ) {
    $query->the_post();
    atom_enclosure();
}
wp_reset_postdata(); // Reset post data after custom query

Checking for Enclosure Before Displaying

Before displaying the atom enclosure, you can check if it exists using get_post_custom_values(). If it exists, display it using atom_enclosure()

$enclosure = get_post_custom_values( 'enclosure' );
if ( ! empty( $enclosure ) ) {
    atom_enclosure();
}

This code checks if the current post has an enclosure. If it does, it displays the enclosure. If not, it does nothing.