• Hello,

    I would like to generate the ID for any image I add into post or widget…

    For example when I create new post wordpress automatically generates ID for it, something like “post-879” and the when you go to check page source code you can see that it added <article id="post-879" class="blablabla".... That way you can use CSS for particular post.

    What happens with image it only generates <img src="..." alt="">, who no ID??

    Thank you in advance!

    Regards,
    Darius

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    When you add an image into a post it should have a class “wp-image-###” where ### is the image ID. Check the entire tag’s attributes, the class is not where one often expects it, it’s last on my installation.

    Or maybe this occurs when adding to a widget? What widget are you adding images to?

    Thread Starter Dariusmit

    (@dariusmit)

    The image is here https://spa.figa.lt/, it’s inside the post. I would like to set a css style for that particular image.

    Moderator bcworkz

    (@bcworkz)

    You used the media library to insert that image? Besides the usual ID class missing, the alignment class is missing as well. Either your theme or a plugin appears to be suppressing the class attribute output.

    In any case, you can use the ‘get_image_tag_class’ filter to add/create/replace class attributes. The second parameter passed is the image ID, so this should be straight forward.

    Since something appears to be suppressing class attributes, there’s a chance it could also suppress your effort as well. Adding your filter with a later priority (higher number) will hopefully bypass whatever else is going on.

    Thread Starter Dariusmit

    (@dariusmit)

    When I press add media to post it doesn’t work. I found that this script is causing the error somehow, when I remove it the add media button works again:

    var image_field;
    jQuery(function($){
     //file upload
      $(document).on('click', 'input.select-img', function(evt){
        image_field = $(this).siblings('.img');
        tb_show('', 'media-upload.php?type=image&TB_iframe=true');
        return false;
      });
       window.send_to_editor = function(html) {
        imgurl = $('img', html).attr('src');
        image_field.val(imgurl);
        if(textarea.textarea=='') {
            textarea.textarea = '<img src="'+ imgurl + '"/>';
            $('.addhtml').val(textarea.textarea);
        }
        else {
            textarea.textarea=textarea.textarea + '<img src="'+ imgurl + '"/>';
    	$('.addhtml').val(textarea.textarea);
        }
        tb_remove()
      }
    });

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to generate ID for image’ is closed to new replies.