When inserting an image in a WordPress post, I would like that it automatically add the alt text of the image above the image as h2.

Example, when inserting an image, WordPress ads this into the post:

<img src="" alt="Hello" />

What I would like is inserting:

<img src="" alt="Hello" />

Thank you for your help!

Best regards

1 Answer 1


I find the solution using this How to change the markup Wordpress inserts for post images

My final code is:

add_filter( 'image_send_to_editor', 'add_custom_data_attribute_send_to_editor', 10, 8 );
function add_custom_data_attribute_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt ){  
    if( $id > 0 ){
        $post = get_post( $id );
              $html = str_replace( "<img src", "<h2>$alt</h2><img src", $html );
    return $html;

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.