1

I use filter for change link for attachments in a page

function custom_attachment_link($markup, $id, $size, $permalink, $icon, $text) {
    // Get attachment meta data
    $attachment_meta = wp_get_attachment_metadata($id);

    if ($attachment_meta) {
        // Get the file size
        $file_size = size_format(filesize(get_attached_file($id)));

        // Get the file type
        $file_type = $attachment_meta['mime_type'];

        // Debugging: Output file size and type
        error_log("File size: $file_size, File type: $file_type");

        // Add size and type to the link markup
        $markup .= '<span class="attachment-meta"> (' . $file_size . ', ' . $file_type . ')</span>';
    } else {
        // Debugging: Output if metadata is not available
        error_log("Attachment metadata not available for attachment ID: $id");
    }

    return $markup;
}

add_filter('wp_get_attachment_link', 'custom_attachment_link', 10, 6);

but this code not modified links in page content. What is wrong with this code?

2
  • just to confirm, this is in functions.php or a plugin?
    – Tom J Nowell
    Commented Mar 25 at 14:43
  • in functions.php
    – mardon
    Commented Mar 25 at 19:31

1 Answer 1

2

There has been a misunderstanding of how page content works here:

but this code not modified links in page content. What is wrong with this code?

Once the links are saved in the post content you have to reopen the post and resave/reinsert for new code to take effect. Filters only modify the links when they are created, they can't change already saved post content.

What About Widgets/Blocks/Shortcodes?

Places such as shortcodes or server rendered blocks will immediatley update as they create new fresh HTML when viewed, but anything that stored those values in the database/post meta/post content will need to be manually updated.

Is There a Different Filter For This?

No, that's not how this works, you would still need to open those pages in the admin to change the links.

So What Should I Do?

You have several options:

  • Find all the places that have your old custom HTML via search, edit and reinsert the links then save
  • when you reinsert the links consider using a block or shortcode instead so that you don't have to do this again in the future
  • if you are using a page builder you should ask in their communities as they can use different methods that require unique answers specific to that page builder
4
  • And how I change the attachment link to have info about size and type?
    – mardon
    Commented Mar 25 at 12:33
  • Is better to use this filetr developer.wordpress.org/reference/hooks/attachment_link ?
    – mardon
    Commented Mar 25 at 13:06
  • No filter can be used to update links already saved to post content. You have to resave the content, you might have to re-insert the attachments too. If you have a second/follow up question you should ask that as a new question, this isn't a discussion thread so I'm only trying to answer the question you wrote originally.
    – Tom J Nowell
    Commented Mar 25 at 14:34
  • I've made my answer clearer with edits, if that answers your question consider marking it as the correct answer. If you want to ask how to insert information about the size and type of an attachment into your posts then you'll need to create a new question.
    – Tom J Nowell
    Commented Mar 25 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.