Category: Media

  • Add Custom Image Sizes Into WordPress Media Library

    How to Add Custom Image Sizes Into WordPress Media Library

    In a previous article we have posted about how you can define new custom image sizes to use on your WordPress theme and how to remove default image sizes. You can define and add custom image sizes with the help of add_image_size() function in WordPress. In this article we will show you how you can add these pre-defined custom image sizes to media library upload panel so that you can choose from all available image sizes instead of only standard thumbnail, medium, large and full sizes.

    We want the content editor to be able to select these new sizes when adding an image into the post, by default when using add_image_size() these custom image sizes won’t appear when inserting into the post. For this we need to use the filter image_size_name_choose and make sure we add the custom image sizes into the size array. Adding the new custom image sizes into this array will make sure that the editor can pick these new sizes and that the images will still fit the design of the theme.

    Simply add the following WordPress code snippet into your theme’s functions.php file to allow the editors to choose these new sizes.

    https://web.archive.org/web/20220701042026if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=3881983570&adf=1219422936&pi=t.ma~as.3463695748&w=336&lmt=1775836305&psa=0&format=336×280&url=http%3A%2F%2Fwpcodesnippet.com%2Fadd-custom-image-sizes-wordpress-media-library%2F&wgl=1&dt=1656649229768&bpp=1&bdt=3800&idt=1266&shv=r20220613&mjsv=m202206090101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280&correlator=4294693926340&frm=20&pv=1&ga_vid=998311421.1656649232&ga_sid=1656649232&ga_hid=2106914544&ga_fc=0&u_tz=0&u_his=8&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1792&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759837%2C44766068&oid=2&pvsid=2260101201887277&tmod=4619812&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=1&fsb=1&xpc=2wcIN6S18X&p=https%3A//web.archive.org&dtd=2255

    // add custom image sizes into wordpress media library
    function display_custom_image_sizes( $sizes ) {
        global $_wp_additional_image_sizes;
        if ( empty( $_wp_additional_image_sizes ) )
            return $sizes;
    
        foreach ( $_wp_additional_image_sizes as $id => $data ) {
            if ( !isset($sizes[$id]) )
                $sizes[$id] = ucfirst( str_replace( '-', ' ', $id ) );
        }
    
        return $sizes;
    }
    add_filter( 'image_size_names_choose', 'display_custom_image_sizes' );
  • Force WordPress to Select Thumbnail Image Size by Default in WordPress Media Uploader

    How to Force WordPress to Select Thumbnail Image Size by Default in WordPress Media Uploader

    With WordPress 3.5 release, WordPress core team introduced many significant changes in the core and Media Uploader was one of the most important one. This new WordPress Media Uploader has a number of advantages over the previous version. For instance it has drag and drop support, easier management of galleries, cleaner interface and much more.

    https://web.archive.org/web/20220521003726if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=4940428946&adk=2809832793&adf=801880389&pi=t.ma~as.4940428946&w=336&lmt=1775834046&psa=0&format=336×280&url=http%3A%2F%2Fwpcodesnippet.com%2Fselect-thumbnail-image-size-in-wordpress-media-uploader%2F&wgl=1&dt=1653093449295&bpp=5&bdt=3332&idt=941&shv=r20220613&mjsv=m202206140101&ptt=9&saldr=aa&abxe=1&correlator=1419750045290&frm=20&pv=2&ga_vid=839127661.1653093451&ga_sid=1653093451&ga_hid=461633498&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=1155&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759842%2C44761043%2C31067768%2C31068039%2C21066430&oid=2&pvsid=2699379776652316&tmod=1118653441&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=1&uci=a!1&btvi=1&fsb=1&xpc=6KxjIWs3TV&p=https%3A//web.archive.org&dtd=1979

    The new WordPress Media Uploader is just great and provides tons of features for uploading and editing images but sometimes that is not enough for us. We always look for ways to improve the user experience.

    On a project, my client asked me to implement a feature to select a custom thumbnail image size automatically in WordPress Media Uploader when he uploads new images to a post, so he can insert them into content without making few extra clicks.

    Lucky for me, WordPress has tons of filters and hooks to help developers alter the default functionality. I used pre_option_(option name) filter to force WordPress to select a custom thumbnail image size by default. This filter is used to temporarily alter a WordPress option during the rendering of a page without changing it permanently in the database.

    If you are also looking for a way to auto select a specific custom thumbnail image size in WordPress Media Uploader then you will need to follow these steps.

    First make sure your theme has support for post thumbnails and you have already defined the custom thumbnail image size in your functions.php file.

    https://web.archive.org/web/20220521003726if_/https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-7443478584383873&output=html&h=280&slotname=3463695748&adk=3568412579&adf=337198732&pi=t.ma~as.3463695748&w=336&lmt=1775834046&psa=0&format=336×280&url=http%3A%2F%2Fwpcodesnippet.com%2Fselect-thumbnail-image-size-in-wordpress-media-uploader%2F&wgl=1&dt=1653093449295&bpp=2&bdt=3332&idt=952&shv=r20220613&mjsv=m202206140101&ptt=9&saldr=aa&abxe=1&prev_fmts=336×280&correlator=1419750045290&frm=20&pv=1&ga_vid=839127661.1653093451&ga_sid=1653093451&ga_hid=461633498&ga_fc=0&u_tz=0&u_his=7&u_h=926&u_w=428&u_ah=926&u_aw=428&u_cd=24&u_sd=1&adx=30&ady=2106&biw=428&bih=781&scr_x=0&scr_y=0&eid=44759875%2C44759926%2C44759842%2C44761043%2C31067768%2C31068039%2C21066430&oid=2&pvsid=2699379776652316&tmod=1118653441&uas=0&nvt=1&eae=0&fc=896&brdim=0%2C0%2C0%2C0%2C428%2C0%2C428%2C926%2C428%2C781&vis=1&rsz=%7C%7CoeEbr%7C&abl=CS&pfx=0&fu=0&bc=31&ifi=2&uci=a!2&btvi=2&fsb=1&xpc=L8A0vA1tEe&p=https%3A//web.archive.org&dtd=1985

    // define custom thumbnail image size
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'my-image-size', 600, 300, true );

    You can change the custom thumbnail image identifierand dimension based on your theme requirement. Now add this following code snippet in your theme’s functions.phpfile before the closing PHP tag at the bottom.

    // force WordPress to select thumbnail image size by default in WordPress media uploader
    function wpcs_default_image_size() {
        return 'my-image-size';
    }
    add_filter( 'pre_option_image_default_size', 'wpcs_default_image_size' );

    That’s it, there is nothing more to do. It’s important to note before posting above code snippet that you use same thumbnail image identifier in above code which you defined earlier in your theme, or it will not work.

  • Add Custom Image Sizes Into WordPress Media Library

    How to Add Custom Image Sizes Into WordPress Media Library

    In a previous article we have posted about how you can define new custom image sizes to use on your WordPress theme and how to remove default image sizes. You can define and add custom image sizes with the help of add_image_size() function in WordPress. In this article we will show you how you can add these pre-defined custom image sizes to media library upload panel so that you can choose from all available image sizes instead of only standard thumbnail, medium, large and full sizes.

    We want the content editor to be able to select these new sizes when adding an image into the post, by default when using add_image_size() these custom image sizes won’t appear when inserting into the post. For this we need to use the filter image_size_name_choose and make sure we add the custom image sizes into the size array. Adding the new custom image sizes into this array will make sure that the editor can pick these new sizes and that the images will still fit the design of the theme.

    Simply add the following WordPress code snippet into your theme’s functions.php file to allow the editors to choose these new sizes.

    // add custom image sizes into wordpress media library
    function display_custom_image_sizes( $sizes ) {
        global $_wp_additional_image_sizes;
        if ( empty( $_wp_additional_image_sizes ) )
            return $sizes;
    
        foreach ( $_wp_additional_image_sizes as $id => $data ) {
            if ( !isset($sizes[$id]) )
                $sizes[$id] = ucfirst( str_replace( '-', ' ', $id ) );
        }
    
        return $sizes;
    }
    add_filter( 'image_size_names_choose', 'display_custom_image_sizes' );