PHP code example of aysnc / wordpress-dynamic-media
1. Go to this page and download the library: Download aysnc/wordpress-dynamic-media library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
add_filter( 'aysnc_wordpress_dynamic_media_rest_enabled', function ( bool $enabled, WP_REST_Request $request, WP_Post $attachment ): bool {
// Skip transformation for specific attachments
if ( get_post_meta( $attachment->ID, '_skip_dynamic_media', true ) ) {
return false;
}
return $enabled;
}, 10, 3 );
add_filter( 'aysnc_wordpress_cloudinary_args', function ( array $args, int $attachment_id ): array {
// Apply auto quality and format to all images
$args['transform']['quality'] = 'auto';
$args['transform']['fetch_format'] = 'auto';
return $args;
}, 10, 2 );
add_filter( 'aysnc_wordpress_dynamic_media_url', function ( string $url, int $attachment_id, array $args ): string {
// Log all generated URLs
error_log( "Dynamic URL for {$attachment_id}: {$url}" );
return $url;
}, 10, 3 );
add_filter( 'aysnc_wordpress_dynamic_media_srcset_dimensions', function ( array $dimensions, int $attachment_id, array $image_meta, string $image_src ): array {
// Force soft crop for all srcset images
$dimensions['hard_crop'] = false;
return $dimensions;
}, 10, 4 );
add_filter( 'aysnc_wordpress_dynamic_media_content_image_src', function ( ?string $src, int $attachment_id, ?string $original_src, $width, $height, string $size ): ?string {
// Skip transformation for full-size images
if ( $size === 'full' ) {
return $original_src;
}
return null; // Let the plugin handle it
}, 10, 6 );
add_filter( 'aysnc_wordpress_cloudinary_upload_url', function ( string $upload_url ): string {
// Use a consistent URL for multisite
return 'https://example.com/wp-content/uploads';
} );
use Aysnc\WordPress\DynamicMedia\Adapter;
use Aysnc\WordPress\DynamicMedia\Adapters\MediaAdapter;
class ImgixAdapter implements MediaAdapter {
public static function get_dynamic_url( int $id, array $args ): string {
$original_url = wp_get_attachment_url( $id );
// Build your Imgix URL here
$imgix_url = 'https://your-source.imgix.net/' . basename( $original_url );
if ( ! empty( $args['width'] ) ) {
$imgix_url .= '?w=' . $args['width'];
}
// Handle $args['transform'] for Imgix-specific params
return $imgix_url;
}
}
// Register and activate your adapter
add_action( 'after_setup_theme', function () {
Adapter::register( 'imgix', new ImgixAdapter() );
Adapter::set( 'imgix' );
}, 20 ); // Priority 20 to run after default registration
use Aysnc\WordPress\DynamicMedia\Plugin;
Plugin::pause(); // Disable transformations
// ... do something with original URLs ...
Plugin::pause( false ); // Re-enable