How to Protect S3 file uploads to ACF custom fields

Prevent Direct Access (PDA) Gold allows you to protect files embedded into content, including file uploaded to the S3 bucket using Amazon S3 Integration extension. If you’ve already inserted files before offloading them, we also provide Search & Replace tool which finds all embedded URLs in specific content and replace them with S3 links automatically. The UI is simple to use and no coding required.

But when it comes to replacing S3 links under ACF custom files, you are required to twist some code for this function to work properly. The steps are easy to follow, and you just need basic knowledge coding to achieve it.

In this article, we will show you how to protect S3 file uploaded to ACF custom fields with PDA Gold and its S3 Integration extension.

ACF Image field

Step 1: Create an ACF Image field and select Image ID as Return Format


Step 2: Embed your files to ACF custom fields as usual

Step 3: Protect and offload these files to S3 bucket using PDA Gold and S3 Integration

Step 4: Copy our custom code snippet to your theme files in where you want to display these files:

if ( method_exists( 'PDA_S3_UTILS', 'create_presigned_url') ) {
	$image_id = get_field('your-image-field-name');
	$s3_utils = new PDA_S3_UTILS();
	// Set expirion time for S3 signed URL
	$expiry_time = 60; // seconds
	$signed_url = $s3_utils->create_presigned_url( $image_id, "{$expiry_time} seconds" );
	echo '<img src="'. $signed_url . '" />';
}

ACF File field

Step 1: Create an ACF File field and select File ID as Return Value

Step 2: Embed your files to ACF custom fields as usual

Step 3: Protect and offload these files to S3 bucket using PDA Gold and S3 Integration

Step 4: Copy our custom code snippet to your theme files in where you want to display these files:

if ( method_exists( 'PDA_S3_UTILS', 'create_presigned_url') ) {
	$file_id = get_field('your-file-field-name');
	$s3_utils = new PDA_S3_UTILS();
	// Set expirion time for S3 signed URL
	$expiry_time = 60; // seconds
	$signed_url = $s3_utils->create_presigned_url( $file_id, "{$expiry_time} seconds" );
	echo '<a href="'. $signed_url . '">' . get_the_title( $file_id ) . '</a>'; //Your link anchor text
}

Set expiration time for S3 signed URL

You can also get the expiry time set on our Amazon S3 settings page instead of defining a new expiry time.

$expiry_time = pda_s3_get_setting( PdaS3Constants::PDAS3_EXPIRED_TIME_FOR_SIGNED_URL );
if ( false === $expiry_time ) {
    $expiry_time = PdaS3Constants::PDAS3_DEFAULT_EXPIRED_TIME; // which is 60 seconds
}

ACF Wysiwyg Editor field

Step 1: Create an ACF Wysiwyg Editor field

Step 2: Embed your files to ACF custom fields as usual

Step 3: Protect and offload these files to S3 bucket using PDA Gold and S3 Integration

Step 4: Copy our custom code snippet to your theme files in where you want to display these files:

$value = get_field('your-field-name');
if ( function_exists('pda_s3_handle_the_content') ) {
	$value = pda_s3_handle_the_content($value);
}
echo $value;