• Resolved fdavidsen

    (@fdavidsen)


    Hello everyone, I’m developing a theme in WP 6.0.2. I tried to upload image by using wp_insert_attachment(). Then I want it to generate all the sub sizes.

    
    $attachment_id = wp_insert_attachment([  
      'guid' => $upload['url'],
      'post_mime_type' => $upload['type']
    ], $upload['file'] );
    
    // Generate sub-sizes of the image.
    wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) );
    

    It works fine unless it doesn’t generate thumbnail with the custom size I defined, it only generates the thumbnail for WP default sizes.

    
    function wing_theme_setup() {
      add_theme_support( 'post-thumbnails' );
      add_image_size( 'other', 8, 8 );
    }
    add_action( 'after_setup_theme', 'wing_theme_setup' );
    

    What should I do? Thanks in advance.

    • This topic was modified 3 years, 8 months ago by fdavidsen.
    • This topic was modified 3 years, 8 months ago by fdavidsen.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter fdavidsen

    (@fdavidsen)

    I checked the available sizes with wp_get_registered_image_subsizes(), it returns without my custom size.

    Moderator bcworkz

    (@bcworkz)

    I tried you code in my theme and it works as expected. An appropriately sized image file is generated. The new size is only generated for newly uploaded images, older images will not have the new size unless you use a plugin to regenerate sizes for all images.

    Even though the size becomes available, it does not appear in editor dialogs unless you explicitly add it via the ‘image_size_names_choose’ filter.

    Thread Starter fdavidsen

    (@fdavidsen)

    Hi bcworkz, thanks for replying. I put the add_image_size() on function.php and the wp_generate_attachment_metadata() somewhere in a static method (causing me need to require “wp-admin/includes/image.php”). Is that causing mine to not work? If it does, how to solve it?

    Moderator bcworkz

    (@bcworkz)

    The need to require a particular core file to gain certain functionality can be difficult to do successfully because it’s often hard to resolve dependencies. So, yes, that could be the cause.

    Adding the image size and uploading images the normal way through the media library will result in the generation of all appropriate image size files. I’m not sure why or how you’re attempting to bypass this, so I’m unable to make a specific suggestion. Are you trying to allow uploads through a front end process? Uploads really need to be an admin process so the right resources are brought on board. A front end process can invoke an admin process via Ajax or REST API requests.

    Thread Starter fdavidsen

    (@fdavidsen)

    Yes, uploading images through the media library works as expected. However, this behavior doesn’t match my need. I tried to upload images in this way because I’m doing starter content importing programmatically. I just don’t want the user to uploading the XML files. I’m not sure this is good or bad, but it match my users’ need.

    I ended up loading my importing script that contain wp_generate_attachment_metadata() using the after_theme_setup hook.

    
    function wing_file_loader() {
      require_once WING_THEME_PATH . 'inc/functions.php';
    }
    add_action( 'after_setup_theme', 'wing_file_loader', 15 );
    

    Again, I’m not sure if this is good or bad. What do you think?

    Moderator bcworkz

    (@bcworkz)

    I’m unclear what your inc/functions.php file does, so it’s difficult to comment about it. If it does what you need, it’s probably fine.

    I’ve never tried programatically importing theme images into the media library. I just pull default images from an /images/ theme sub-folder. They are only used as a fallback when the user hasn’t selected their own preference.

    Thread Starter fdavidsen

    (@fdavidsen)

    inc/functions.php file simply where the wp_generate_attachment_metadata() located, I just put the importing code there.

    Could you please provide the code to pull default images from an /images/ theme sub-folder? Just for the reference of how to pull images.

    Moderator bcworkz

    (@bcworkz)

    Assuming a user selected image is saved as a theme mod, I’d use a theme /images/ file when the user hasn’t specified their own selection like this (template code):

    $img_id = get_theme_mod('my_theme_img_setting');
    if ( ! $img_id ) {
      $url = get_stylesheet_directory() . '/images/default-theme-image.png';
    } else {
      $url = wp_get_attachment_url( $img_id );
    }
    echo "<img src=\"$url\" class=\"my-theme-image\" />";
    Thread Starter fdavidsen

    (@fdavidsen)

    Got it, thanks!

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

The topic ‘Custom Size Not Being Generated’ is closed to new replies.