WordPress.org

Ready to get started?Download WordPress

Forums

ThreeWP Broadcast
Broadcast CDN attachments (2 posts)

  1. flynsarmy
    Member
    Posted 1 year ago #

    We use Amazon S3 with CloudFront plugin. As a result the copy_attachment() method doesn't work as expected - no attachments are copied. I whipped up a minor patch to get this happening correctly however then ran into issues with the Scissors Continued plugin which has a wp_insert_attachment() hook. As a result we're just going to use the handy "Override child post permalinks" option however this patch might help others so here it is:

    In ThreeWP_Broadcast.php find the copy_attachment() method.
    Change

    if ( ! file_exists( $attachment_data->filename_path() ) )
    	return false;
    
    copy( $attachment_data->filename_path(), $upload_dir['path'] . '/' . $attachment_data->filename_base() );

    to

    //File won't exist on disk for Amazon S3 for WordPress with CloudFront uploads
    if ( !isset($attachment_data->post_custom['amazonS3_info']) )
    {
    	if ( ! file_exists( $attachment_data->filename_path() ) )
    		return false;
    
    	copy( $attachment_data->filename_path(), $upload_dir['path'] . '/' . $attachment_data->filename_base() );
    }

    and change

    $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $attachment_data->filename_base(), $post_id );

    to

    //File won't exist on disk for Amazon S3 for WordPress with CloudFront uploads
    if ( !isset($attachment_data->post_custom['amazonS3_info']) )
    	$attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $attachment_data->filename_base(), $post_id );
    else
    	$attach_id = wp_insert_attachment( $attachment, false, $post_id );

    For that second code block I wasn't sure if passing the CDN file URL to wp_insert_attachment() is helpful or not - it didn't seem to make a difference. Incase it does and I'm just not seeing it, here's the version where it does:

    //File won't exist on disk for Amazon S3 for WordPress with CloudFront uploads
    if ( !isset($attachment_data->post_custom['amazonS3_info']) )
    	$attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $attachment_data->filename_base(), $post_id );
    else
    {
    	$s3_info = maybe_unserialize( reset($attachment_data->post_custom['amazonS3_info']) );
    	$attach_id = wp_insert_attachment( $attachment, 'http://'.$s3_info['bucket'].'/'.$s3_info['key'], $post_id );
    }

    http://wordpress.org/extend/plugins/threewp-broadcast/

  2. isay
    Member
    Posted 1 year ago #

    Hi there flynsarmy!

    This is nearly exactly what I need to do, only I use the w3-total-cache plugin. I described my problem here.

    Any ideas how I do the equivalent of $attachment_data->post_custom['amazonS3_info'] in your code above?

    Thank you very much for your help!

    //Mike

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.