0

I am currently working on a function that will do the following:

  • Checks if the parameter holds an already created image size and if this size does not exists, create a new one

  • Loop through all the image sizes to add these as a source element in the code

This is currently what I have, but the only thing that's bugging me is that when I want to display an image with the newly created size it doesn't recognize the size. I get the following notice: Notice undefined index test.

private function checkImageSizes() {

    $exists = false;

    foreach ($this->getSizes() as $size) {

        $exists = false;

        foreach ($this->get_image_sizes() as $currentSizes) {
            if($size['width'] == $currentSizes['width'] && $size['height'] == $currentSizes['height']) {
                $exists = true;
                return false;
            } else {
                $exists = false;
            }
        }

        if($exists == false) {
            if(function_exists('add_image_size')) {
            add_image_size($size['name'], $size['width'], $size['height'], true);
            }
        }

    }
}

I know this question is not new and there are answered, but I can't seem to find the answer to my problem. Thanks for helping.

5
  • Hmm... Your code doesn't look right. You loop through all sizes, but if you find first one that already exists, your function returns false - so it wont process any other sizes... Commented May 2, 2018 at 9:13
  • Is this a method from a class ? why is there private?? Did you hook the it to correct hook?? Commented May 2, 2018 at 9:14
  • This is indeed a method from a Class. I am working like this because I worked with php all the time at school. WordPress is new to me so I am learning some stuff and trying to do it the right way. If you want I can show the rest of the class Commented May 2, 2018 at 9:17
  • You know that when you run add_image_size() it doesn't apply retroactively, right? It will only apply for newly uploaded images. For exisiting images you need to regenerate them. There are several plugins available for this. Commented May 2, 2018 at 9:21
  • Okay. I looked up a plugin and used it, but I didn't see one of the newly created custom sizes. Commented May 2, 2018 at 9:26

1 Answer 1

0

I figured out that the onyl way of having the permanent image_size is to add them into the functions.php. After adding these sizes I made use of a plugin to regenarte the size of the images.

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.