How to add image id class?

Question

Is there a way to append the image id class to existing attached images in the post content automagically with a php function? That would be incredibly awesome.

Here is what I have so far. It seems like it should work, in theory. What am I doing wrong?

function be_attachment_id_on_images( $attr, $attachment ) {
    if( !strpos( $attr['class'], 'wp-image-' . $attachment->ID ) )
        $attr['class'] .= ' wp-image-' . $attachment->ID;
    return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'be_attachment_id_on_images', 10, 2 );

if ( current_user_can( 'manage_options' ) && is_admin()){
function my_update_posts() {

        $args = array(
        'post_type' => 'post',
        'numberposts' => -1,
        'category' => 1,
        'ID' => 1,
    );

    $myposts = get_posts($args);
    foreach ($myposts as $mypost){
        $content = $mypost->post_content;
        apply_filters('be_attachment_id_on_images', $content);
        $mypost->post_content = $content;
        wp_update_post( $mypost );
    }

}

add_action( 'admin_init', 'my_update_posts' );

}
0
alice 2 months 0 Answers 12 views 0

Leave an answer