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' );
}
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' );
}
Share
Improve this question
edited Mar 7, 2019 at 18:18
SHA3
asked Mar 5, 2019 at 15:44
SHA3SHA3
1199 bronze badges
1
- Did you find a working solution for this? I'm running into the same issue after importing some posts from another platform into WordPress. – Mark B Commented Aug 12, 2021 at 17:56
2 Answers
Reset to default 2Shamelessly copied from here: https://letswp.io/add-attachment-id-to-the-class-of-wordpress-images/
add_filter( 'wp_get_attachment_image_attributes', 'add_image_id_class', 10, 2 );
function add_image_id_class( $attr, $attachment ) {
$class_attr = isset( $attr['class'] ) ? $attr['class'] : '';
$has_class = preg_match( '/wpimage\_[0-9]+/', $class_attr, $matches );
if ( !$has_class ) {
$class_attr .= sprintf( ' wpimage_%d', $attachment->ID );
$attr['class'] = ltrim( $class_attr );
}
return $attr;
}
This add a class like wpimage_{ATTACHMENT_ID}
for example wpimage_22
.
According to this earlier answer, the wp_get_attachment_image_attributes
filter only works for the Featured Image/Post Thumbnail, although I'm not sure if the assertion is correct. Other places claim it doesn't work with the block editor. You might try the get_image_tag_class()
filter. Or, depending on your goals, it might suffice to use a slightly different approach: add a class to the body (using the body_class()
filter), then use a css selector like body.special img
(that doesn't give the ID, though).