Skip to content

OG Perfect Images Retina 2x: developer's guide

This page lists the hooks, functions, REST routes and data that OG Perfect Images Retina 2x (shown in WordPress as OG Picture Perfect) uses. For settings and everyday use, see the user guide.

Items marked Pro exist only when the licence is active and the Pro features are installed.

The plugin has no shortcodes, blocks, AJAX actions, MCP tools or WP-CLI commands.

Actions

Action Arguments When it fires
ogretina_before_generate_retina $id Before retina copies are built for an attachment.
ogretina_retina_file_added $id, $retina_file, $size_name After each @2x file is written.
ogretina_generate_retina $id After all retina copies for an attachment are built.
ogretina_before_generate_webp $id Before WebP or AVIF copies are built.
ogretina_webp_file_added $id, $new_file, $size_name After each WebP or AVIF file is written.
ogretina_generate_webp $id After all WebP or AVIF copies are built.
ogretina_before_generate_webp_retina $id Before @2x WebP or AVIF copies are built.
ogretina_webp_retina_file_added $id, $file, $size_name After each @2x WebP or AVIF file is written.
ogretina_generate_webp_retina $id After all @2x WebP or AVIF copies are built.
ogretina_retina_file_removed $id, $retina_file After a retina file is deleted.
ogretina_webp_file_removed $id, $webp_file After a WebP or AVIF file is deleted.
ogretina_webp_retina_file_removed $id, $file After a @2x WebP or AVIF file is deleted.
ogretina_before_generate_thumbnails $media_id Before thumbnails are regenerated (standard or AI).
ogretina_generate_thumbnails $media_id After thumbnails are regenerated.
ogretina_before_regenerate $media_id Before a Build Retina or Build WebP run from Image Tools.
ogretina_regenerate $media_id After that run.
ogretina_before_replace $media_id, $tmp_file Before Replace Image swaps the file.
ogretina_replace $media_id After the file is replaced and thumbnails rebuilt.
ogretina_cdn_settings_changed $changed (array of key => [ 'old', 'new' ]), $new_options When CDN Domain, the Easy IO domain or All Image URLs changes. Use it to clear caches that store image URLs.
ogretina_before_upload_retina $media_id, $retina_file Pro. Before an uploaded full-size retina file is saved.
ogretina_upload_retina $media_id, $retina_file Pro. After it is saved.
ogretina_before_upload_webp $media_id, $webp_file Pro. Before an uploaded full-size WebP file is saved.
ogretina_upload_webp $media_id, $webp_file Pro. After it is saved.

Example: purge your own cache when the CDN settings change.

add_action( 'ogretina_cdn_settings_changed', function ( $changed, $options ) {
    delete_transient( 'my_theme_image_urls' );
}, 10, 2 );

Filters

Filter Arguments Use
ogretina_allow_setup $allowed (default: manage_options) Who can read and change the settings through the REST API.
ogretina_allow_usage $allowed (default: administrator role) Who can use Image Tools and the media routes.
ogretina_ignore_generate_retina $is_ignored, $id Return true to skip retina building for an attachment.
ogretina_ignore_generate_webp $is_ignored, $id Return true to skip WebP or AVIF building.
ogretina_esrgan_timeout $seconds (default 600) How long This server: Real-ESRGAN may run for one image.
ogretina_sdapi_timeout $seconds (default 300) How long to wait for Your own AI server.
ogretina_ignore_generate_webp_retina $is_ignored, $id Return true to skip @2x WebP or AVIF building.
ogretina_custom_crop null, $id, $size_name Return an array with x, y, w, h and scale to crop a retina or WebP copy your own way.
ogretina_last_chance_generate false, $id, $retina_file, $width, $height Runs when a retina file could not be made (original too small). Create the file yourself and return true to avoid an issue being recorded.
ogretina_should_delete_attachment true, $attach_id, $post Return false to keep the retina and WebP files when a media item is deleted.
ogretina_replace_ext_in_srcset $extensions (default jpg, jpeg, png) File extensions whose srcset entries are swapped for WebP or AVIF.
ogretina_cdn_url_allowed $allowed, $url, $attachment_id, $mime Whether a URL is rewritten to the CDN domain. By default only image/* files are.
ogretina_validate_src $src Picturefill method. Return an empty value to leave an <img> untouched.
ogretina_img_retina_url $retina_url Picturefill method. The retina URL added to an <img> or CSS background.
ogretina_img_url $img_url Picturefill method. The normal URL in the new srcset.
ogretina_img_src $src Picturefill method, with Keep IMG SRC on. The src that is kept.
ogretina_preserve_uploaded_retina $exact_match, $media_id, $tmp_file, $width, $height Pro. Return true to store an uploaded full-size retina file as it is, without re-encoding. By default this happens only when it is exactly twice the full size.

Retina images are resized with the jpeg_quality WordPress filter (default 75).

Example: never build retina copies for PNG files.

add_filter( 'ogretina_ignore_generate_retina', function ( $ignored, $id ) {
    return $ignored || get_post_mime_type( $id ) === 'image/png';
}, 10, 2 );

Example: let editors use Image Tools.

add_filter( 'ogretina_allow_usage', function ( $allowed ) {
    return $allowed || current_user_can( 'edit_others_posts' );
} );

PHP functions

Global helper functions you can call from a theme or plugin.

Function Returns
ogretina_get_retina_from_url( $url ) The retina URL for an image URL, or null.
ogretina_get_retina( $file ) The retina file path for an image path, or null if it does not exist.
ogretina_generate_images( $meta ) Builds the retina copies for attachment metadata.
ogretina_delete_attachment( $attach_id, $deleteFullSize = true ) Deletes the retina copies of an attachment.
ogretina_picture_rewrite( $buffer ) Runs the Picturefill rewrite on an HTML string. The delivery method already does this for you.
ogretina_get_pathinfo_from_image_src( $image_src ) Path info for an image URL, relative to the uploads folder.
ogretina_retina_extension() The retina suffix, @2x.
ogretina_is_registered() Whether the Pro licence is active. Call it in wp-admin only.

File naming:

File Name
Retina photo@2x.jpg
WebP or AVIF photo.jpg.webp or photo.jpg.avif
Retina WebP or AVIF photo@2x.jpg.webp or photo@2x.jpg.avif

REST API

Namespace og-retina-images/v1. Routes are registered only for users who can upload_files. Settings routes check ogretina_allow_setup; the others check ogretina_allow_usage. Send a wp_rest nonce in X-WP-Nonce. POST bodies are JSON, except the upload routes, which take a multipart form.

Settings

Route Method Use
/all_settings/ GET All settings.
/update_option/ POST Save settings: { "options": { ... } }. Returns the saved options.
/easy_io_link/ POST Link the site to Easy IO.
/easy_io_unlink/ POST Unlink Easy IO.
/easy_io_stats/ POST Easy IO usage figures.
/check_optimizers/ GET Which image optimisation tools the server has.
/ai_image_sizes/ POST AI Assistant suggestions for image sizes. Needs OG AI Core.
/ai_retina_sizes/ POST AI Assistant suggestions for retina sizes. Needs OG AI Core.

Media

Route Method Parameters Use
/media GET skip, limit, filterBy (all, optimizeIssues, ignored), orderBy, order, search The Image Tools list.
/stats GET search Counts for the filters.
/get_all_ids POST issuesOnly Attachment IDs for a bulk run (JPEG, PNG and GIF).
/refresh POST none Recalculates the issue lists.
/details POST mediaId Retina status per size.
/webp_details POST mediaId WebP or AVIF status per size.
/build_retina POST mediaId Rebuilds retina and @2x WebP copies.
/build_webp POST mediaId Rebuilds WebP or AVIF and @2x WebP copies.
/regenerate POST mediaId, optimized (bool), ai_generate (bool) Regenerates thumbnails. optimized builds only missing ones; ai_generate uses AI upscaling.
/optimize POST mediaId Compresses the image with the server's tools.
/delete_retina POST mediaId Deletes retina copies.
/delete_webp POST mediaId Deletes WebP or AVIF copies.
/ignore POST mediaIds (ID or array) Toggles Ignore.
/replace POST multipart form: mediaId plus the uploaded file Replaces the image file.
/get_logs GET none Reads the log.
/clear_logs GET none Empties the log.
/upload_retina POST mediaId plus file Pro. Uploads a retina file.
/upload_retina_fullsize POST mediaId plus file Pro. Uploads a full-size retina file.
/delete_retina_fullsize POST mediaId Pro. Deletes the full-size retina file.
/upload_webp POST mediaId plus file Pro. Uploads a WebP file.
/upload_webp_fullsize POST mediaId plus file Pro. Uploads a full-size WebP file.
/delete_webp_fullsize POST mediaId Pro. Deletes the full-size WebP file.
/generate_upscale_retina POST mediaId Pro. Creates the full-size retina image by AI upscaling.

Example: rebuild the retina copies of attachment 123.

curl -X POST "https://example.com/wp-json/og-retina-images/v1/build_retina" \
  -H "X-WP-Nonce: <nonce>" -H "Content-Type: application/json" \
  --cookie "<logged-in cookies>" \
  -d '{"mediaId":123}'

Cron events

Event When
ogretina_pack_fetch A single event that downloads the Pro features after the licence is saved or the plugin is updated.

Data stored

Name Type What it holds
ogretina_options option All settings in one array (sizes, modules, delivery methods, CDN, AI service and its settings: ai_service is none, local, esrgan, sdapi, ogaie, replicate, stability or claid; esrgan_path, esrgan_model, esrgan_gpu, sdapi_url, sdapi_upscaler, sdapi_auth, replicate_token, stability_key, claid_*; logs path).
ogretina_license option The licence record.
ogretina_prefix_moved option Marks a one-time data move from older installs as done.
ogretina_issues transient IDs of attachments with missing retina or WebP files.
ogretina_optimize_issues transient IDs of attachments flagged by the optimiser.
ogretina_flush_rules transient Asks for the rewrite rules to be rebuilt.
_ogretina_ignored post meta 1 when an attachment is ignored.
_ogretina_optimize post meta The result of Optimize for an attachment.
_media_version post meta Goes up each time an image is replaced. Added to CDN URLs to bust caches.

The plugin adds no database tables. Other files it creates:

  • The @2x, WebP and AVIF copies, next to the originals in wp-content/uploads.
  • The log file wp-content/uploads/ogretina_<random>.log, when Logs is on.
  • The Pro features in wp-content/og-pro/og-retina-images/ (Pro).

Nothing is removed when the plugin is deleted.

Constants

Constant Value
OGRETINA_VERSION The plugin version.
OGRETINA_PREFIX ogretina
OGRETINA_PATH The plugin folder path.
OGRETINA_URL The plugin folder URL.
OGRETINA_ESRGAN_PATH Optional, set in wp-config.php: the full path to realesrgan-ncnn-vulkan. It overrides Program Path, so the path cannot be changed from wp-admin.

JavaScript

On its own settings screen, the Media Library and Media > Add New, the plugin loads its admin app with the global ogretina_retina. It holds api_url, rest_nonce, is_pro, is_registered, options and image_size_names.

On the front end it loads a script only for some delivery methods:

Delivery method Script
Picturefill picturefill.min.js (unless Polyfill Script is set to Do not load)
Retina.js retina.min.js
HTML Rewrite retina-cookie.js, which sets a devicePixelRatio cookie
Responsive Images none

With Debug Mode on, debug.js also loads to force retina rendering.