When I attached images, it nested in this hierarchy of html tags :
<div id="attachment_ID" class="alignnone">
<a class="image-ID" href="">
<img class="yada yada" src="" alt="" srcset="more-image-links.jpg" sizes="yada ya da">
</a>
</div>
So I want to manipulate the anchor into <a href="post-link"
I know it is required to filter something like :
function replace_a_link( $url ) {
// Some codes here.
return $url;
}
add_filter('the_content', 'replace_a_link');
But, I don't know the exact story :/
When I attached images, it nested in this hierarchy of html tags :
<div id="attachment_ID" class="alignnone">
<a class="image-ID" href="https://image-link.jpg">
<img class="yada yada" src="https://image-link.jpg" alt="" srcset="more-image-links.jpg" sizes="yada ya da">
</a>
</div>
So I want to manipulate the anchor into <a href="post-link"
I know it is required to filter something like :
function replace_a_link( $url ) {
// Some codes here.
return $url;
}
add_filter('the_content', 'replace_a_link');
But, I don't know the exact story :/
Share Improve this question edited Apr 10, 2020 at 5:35 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Apr 10, 2020 at 5:30 Hsu Pyae NaungHsu Pyae Naung 11 Answer
Reset to default 0You may try the filter wp_get_attachment_link
// definition: apply_filters( 'wp_get_attachment_link', "<a href='" . esc_url( $url ) . "'>$link_text</a>", $id, $size, $permalink, $icon, $text, $attr );
// this filter provide a max. of 7 arguments, I use 2 in the example
add_filter( 'wp_get_attachment_link', 'q363693_update_attachment_link', 10, 2 );
function q363693_update_attachment_link( $url, $id ) {
// some codes
$new_link = "<a href='" . get_permalink( $id ) . "'>your own text or anything you need or add more arguments to use the original text...</a>";
return $new_link;
}