skip to Main Content

I’m trying to get post id of image and then get specific size of image.
If I give hardcode single image url then it works fine but if I give url through variable then it returns 0.
It is strange behaviour

here is function

function get_image_id_and_size($image_url) 
{
    
    //return $image_url;
    
    $image_id = attachment_url_to_postid($image_url);
    
    return $image_id;
    
    $image_large = wp_get_attachment_image_src($image_id, 'large');
    
    
    return $image_large[0];
    
}

and here is javascript code

    var picture = document.querySelectorAll(".active");
for (var i = 0; i < picture.length; i++)
    {
        var src = picture[i].src;
        if (typeof src !== 'undefined')
        {
            var res;
            if (src.includes('bedbase') && src.includes('12in_'))
            {
                        
                var imgurl = src.split('12in_');
                var imgres = imgurl[1].split('-');
                var orgurl = imgurl[0] + "12in_" + currentcolor + ".png";
                                
                //console.log(orgurl);
                res = <?php echo get_image_id_and_size(orgurl); ?>;
                console.log(res);
                //picture[i].src = res;
                        
            }
        }
    }

2

Answers


  1. Chosen as BEST ANSWER

    I checked if I return the urls it works, as you can see I commented that return line. But it don't get the post id it returns 0. But if I give hardcode single link in argument then it works and returns post id.


  2. I had a similar problem where the domain did not have a www. but the file url did, and that would return a 0.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search