Title: Using RICG programmatically
Last modified: August 30, 2016

---

# Using RICG programmatically

 *  [BenRacicot](https://wordpress.org/support/users/benracicot/)
 * (@benracicot)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/using-ricg-programmatically/)
 * Hello,
 * I see that the plugin will be embedded into WP core 4.4! Awesome, really looking
   forward to that. For now, is there a way to use the plugin’s functionality programmatically
   to output a srcset formatted img tag? Or should I still do my own dirty work 
   in my markup?
 * thanks.
 * [https://wordpress.org/plugins/ricg-responsive-images/](https://wordpress.org/plugins/ricg-responsive-images/)

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

 *  Plugin Contributor [Joe McGill](https://wordpress.org/support/users/joemcgill/)
 * (@joemcgill)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/using-ricg-programmatically/#post-6735626)
 * Hi Ben,
 * The latest version of the plugin matches the functionality that will be included
   in WP 4.4, so you can use the new template functions `wp_get_attachment_image_srcset()`
   and `wp_get_attachment_image_sizes()` to help you build custom markup. Additionally,
   any image that is created using `wp_get_attachment_image()` will automatically
   include `srcset` and `sizes` attributes. For more, see [https://make.wordpress.org/core/2015/11/10/responsive-images-in-wordpress-4-4/](https://make.wordpress.org/core/2015/11/10/responsive-images-in-wordpress-4-4/).
 * Joe
 *  Thread Starter [BenRacicot](https://wordpress.org/support/users/benracicot/)
 * (@benracicot)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/using-ricg-programmatically/#post-6735638)
 * Hey Joe,
 * Thanks for replying. This is so close!
    Although I cannot get anything out of
   these two functions in WP 4.3 with RICG plugin activated:
 *     ```
       $img_src = wp_get_attachment_image_url( 380, 'medium' );
       $img_srcset = wp_get_attachment_image_srcset( 380, 'medium' );
       ```
   
 *  Plugin Contributor [Joe McGill](https://wordpress.org/support/users/joemcgill/)
 * (@joemcgill)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/using-ricg-programmatically/#post-6735653)
 * Hi Ben,
 * A couple of questions:
 * 1. How are you outputting those values once you’ve set them to the two variables?
   
   2. Have you verified that 380 is, in fact, an ID for an image in your media library
   and not a post?
 * Joe
 *  Thread Starter [BenRacicot](https://wordpress.org/support/users/benracicot/)
 * (@benracicot)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/using-ricg-programmatically/#post-6735664)
 * Joe, thanks for checking up on me.
 * Here is what I’ve got:
 *     ```
       // :(
       // $original = wp_get_attachment_image_url( 381);
       // $mobile   = wp_get_attachment_image_srcset( 381, 'mobile' );
       // $tablet   = wp_get_attachment_image_srcset( 381, 'tablet' );			
   
       // :)
       $original 	= wp_get_attachment_url( 381 );
       $mobile 	= wp_get_attachment_image_src( 381, 'mobile');
       $tablet   	= wp_get_attachment_image_src( 381, 'tablet' );
       ```
   
 * Just a side note. I wasn’t able to get srcset to load the (hardcoded or outputted)
   image per screensize so I resorted to `<picture>` to be explicit. Any input would
   be appreciated.
 * PS. I’m outputting the smiley face code successfully like this:
 *     ```
       echo $original;
       echo $tablet[0];
       echo $mobile[0];
       ```
   
 *  Plugin Contributor [Jasper de Groot](https://wordpress.org/support/users/jaspermdegroot/)
 * (@jaspermdegroot)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/using-ricg-programmatically/#post-6735722)
 * Hi Ben,
 * Can you tell us more about your custom image sizes?
 * Do your custom image sizes ‘mobile’ and ‘tablet’ have the same aspect ratio or
   did you add other custom image sizes with the same aspect ratio for both ‘mobile’
   and ‘tablet’?
    If there are no other image sizes available with the same aspect
   ratio `wp_get_attachment_image_srcset()` will return false, because a `srcset`
   with one source is the same as `src` and has no use.
 * If you have one image size for mobile and one for tablet and they have different
   aspect ratios then you would indeed need to use a `<picture>` element. But this
   is outside the scope of this plugin and what will be included in WP core 4.4.
 * Earlier you said that `$img_srcset = wp_get_attachment_image_srcset( 380, 'medium');`
   is returning `false` too. This should normally result in a `srcset` value because‘
   medium’ and ‘large’ have the same aspect ratio. Is the original image with ID
   380 big enough to create a ‘large’ image?
    Do you use the defaults for these 
   image sizes (medium: 300, large: 1024) or did you change them in the media settings?
 *  Thread Starter [BenRacicot](https://wordpress.org/support/users/benracicot/)
 * (@benracicot)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/using-ricg-programmatically/#post-6735725)
 * Hey Jasper,
 * I did end up using my old friend `<picture>`
 * Yes I do have several custom add_image_sizes for my images most of which have
   no hard crop. Really good info regarding ratios.
 * I have not changed any media settings only added those custom sizes via code.

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

The topic ‘Using RICG programmatically’ is closed to new replies.

 * ![](https://ps.w.org/ricg-responsive-images/assets/icon.svg?rev=1119324)
 * [RICG Responsive Images](https://wordpress.org/plugins/ricg-responsive-images/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ricg-responsive-images/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ricg-responsive-images/)
 * [Active Topics](https://wordpress.org/support/plugin/ricg-responsive-images/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ricg-responsive-images/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ricg-responsive-images/reviews/)

## Tags

 * [breakpoints](https://wordpress.org/support/topic-tag/breakpoints/)
 * [default](https://wordpress.org/support/topic-tag/default/)
 * [featured images](https://wordpress.org/support/topic-tag/featured-images/)
 * [media settings](https://wordpress.org/support/topic-tag/media-settings/)
 * [picturefill](https://wordpress.org/support/topic-tag/picturefill/)
 * [RICG](https://wordpress.org/support/topic-tag/ricg/)
 * [sizes](https://wordpress.org/support/topic-tag/sizes/)
 * [srcset](https://wordpress.org/support/topic-tag/srcset/)
 * [W3C](https://wordpress.org/support/topic-tag/w3c/)

 * 6 replies
 * 3 participants
 * Last reply from: [BenRacicot](https://wordpress.org/support/users/benracicot/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/using-ricg-programmatically/#post-6735725)
 * Status: not resolved