• Hi Everybody,

    I’m currently working on my first big plugin. The plugin makes a meta box on a ‘post’ or ‘page’ type page. You can make the settings in the plugins config panel. That’s all working. But… when i get the options from the database the values are right, but i can’t make it work. This is my script:

    function image_attachments_define_image_sizes() {
      add_theme_support('post-thumbnails');
      $image_size_slug = get_option('image-size-slug');
      $image_size_width = get_option('image-size-width');
      $image_size_height = get_option('image-size-height');
      $image_size_crop = get_option('image-size-crop');
    
      foreach(get_post_types(array('public' => true), 'object') as $label => $keys) {
        for($i = 0; $i <= count($image_size_slug[$label]) - 1; $i++) {
          if($image_size_slug[$label][$i]) {
            $crop = ($image_size_crop[$label][$i] ? true : 0);
            //echo 'add_image_size('. $image_size_slug[$label][$i] .', '. $image_size_width[$label][$i] .', '. $image_size_height[$label][$i] .', '.$crop.')';
            //Gives
            //add_image_size(test, 201, 101, 1);
            //add_image_size(haha, 201, 201, 0);
            //add_image_size(bla, 201, 301, 0);
            add_image_size($image_size_slug[$label][$i], $image_size_width[$label][$i], $image_size_height[$label][$i], $crop);
          }
        }
      }
    
      //var_dump(get_intermediate_image_sizes());
      /*
        GIVES:
        array(10) {
          [0]=>
          string(9) "thumbnail"
          [1]=>
          string(6) "medium"
          [2]=>
          string(5) "large"
          [3]=>
          string(23) "image-attachments-image"
          [4]=>
          string(16) "homepage-preview"
          [5]=>
          string(16) "portfolio-header"
          [6]=>
          string(15) "actueel-preview"
          [7]=>
          string(4) "test"
          [8]=>
          string(4) "haha"
          [9]=>
          string(3) "bla"
        }
      */
    
    }  
    
    add_action('admin_head', 'image_attachments_define_image_sizes');

    The image sizes are being recognized, but i cant find the images with 201*101, 201*201 AND 201*301. Did i choose the wrong add_action?

    add_action('admin_head', 'image_attachments_define_image_sizes');

    Can you please help me?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Same question here…
    It works when putting add_image_size(…) on functions.php of my theme but I need to do it on a plugin.

    Could someone help us?

    Thanks !

    admin_head is way too late. Do it at init or even better outside of a hook.

    EDIT: dannydehaan, your code specifically should be run inside a hook though (I recommend init) to allow plugins, etc. to register their post types.

    For a generic add_image_size() call though, it’s fine to do it outside of a hooked function.

    EDIT #2: admin_head wouldn’t work anyway as your custom sizes wouldn’t be registered on the front end which would defeat the purpose of registering custom sizes.

    It works for me with admin_init
    Thanks Alex M. !

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Add_image_size() in plugin.’ is closed to new replies.