Using WordPress ‘atom_enclosure’ PHP filter

The atom_enclosure WordPress PHP filter modifies the atom enclosure HTML link tag for the current post.

Usage

add_filter('atom_enclosure', 'your_function_name');
function your_function_name($html_link_tag) {
    // your custom code here
    return $html_link_tag;
}

Parameters

  • $html_link_tag (string): The HTML link tag with a URI and other attributes.

More information

See WordPress Developer Resources: atom_enclosure

Examples

Remove Atom Enclosure

To remove the atom enclosure for the current post:

add_filter('atom_enclosure', 'remove_atom_enclosure');
function remove_atom_enclosure($html_link_tag) {
    // Set the enclosure to an empty string
    $html_link_tag = '';
    return $html_link_tag;
}

Change Enclosure Type

To change the enclosure type for the current post:

add_filter('atom_enclosure', 'change_enclosure_type');
function change_enclosure_type($html_link_tag) {
    // Replace video/mp4 with audio/mpeg
    $html_link_tag = str_replace('video/mp4', 'audio/mpeg', $html_link_tag);
    return $html_link_tag;
}

Add Custom Attribute

To add a custom attribute to the atom enclosure:

add_filter('atom_enclosure', 'add_custom_attribute');
function add_custom_attribute($html_link_tag) {
    // Add a custom data attribute to the tag
    $html_link_tag = str_replace('<link', '<link data-custom="my_value"', $html_link_tag);
    return $html_link_tag;
}

Modify Enclosure URL

To modify the enclosure URL for the current post:

add_filter('atom_enclosure', 'modify_enclosure_url');
function modify_enclosure_url($html_link_tag) {
    // Replace the original URL with a new one
    $html_link_tag = preg_replace('/href="[^"]+"/', 'href="https://example.com/new-url.mp4"', $html_link_tag);
    return $html_link_tag;
}

Add Enclosure to Specific Post

To add an enclosure only to a specific post:

add_filter('atom_enclosure', 'add_enclosure_to_specific_post');
function add_enclosure_to_specific_post($html_link_tag) {
    // Add enclosure only for post ID 123
    if (get_the_ID() === 123) {
        $html_link_tag = '<link href="https://example.com/enclosure.mp4" rel="enclosure" type="video/mp4" />';
    }
    return $html_link_tag;
}