WordPress.org

Ready to get started?Download WordPress

Forums

ThreeWP Broadcast
Patch: Convert Custom Fields pointing to attachments (6 posts)

  1. STRML
    Member
    Posted 1 year ago #

    Hey Edward,

    I have a number of custom post formats that contain custom fields pointing to attachments. So for instance I have a "Photos" post format that contains three thumbnails, which are attached by custom fields (creatively named "image1", "image2", and "image3").

    While the Broadcast plugin correctly finds these attachments and moves them over to the correct blog, the post IDs are not updated in the custom fields. This creates some more work for my users who then have to go into the gallery, figure out which images were originally attached, and re-attach them.

    Rather than deal with all of that junk, I fixed the plugin. Essentially it will search the post's custom fields for IDs that were pointing to attachments, and convert them. I'm not sure of the best way to submit this, so I'm just going to attach some code:

    At line 1541: (just past wp_delete_attachment( $attachment_to_remove->ID );

    $converted_attachment_IDs = array();
    foreach( $attachment_data as $key=>$attached_file)
    {
    	if ( $key != 'thumbnail' )
    		$converted_attachment_IDs[$key] = $this->copy_attachment( $attached_file, $new_post_id );
    }

    At line 1567: (just past $single_meta_value = maybe_unserialize( $single_meta_value );)

    // If this was a custom field pointing to an attachment, convert the ID
    if(array_key_exists($single_meta_value, $converted_attachment_IDs)){
    	$single_meta_value = $converted_attachment_IDs[$single_meta_value];
    }

    At line 1574: (just past $meta_value = maybe_unserialize( $meta_value ); )

    // If this was a custom field pointing to an attachment, convert the ID
    if(array_key_exists($meta_value, $converted_attachment_IDs)){
    	$meta_value = $converted_attachment_IDs[$meta_value];
    }

    Let me know if this makes sense - I have subbed to email notifications on this thread so I will get the messages & can send the full file / a patch if needed.

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

  2. Jorijn
    Member
    Posted 9 months ago #

    For the Googlers that enter this page searching for the same problem;

    I was facing issues with the Advanced Custom Fields (ACF) plug-in. It copied ACF images, but it did not copy the attachment. I added this to OP's code;

    After $bcd->post_custom_fields = get_post_custom( $bcd->post->ID ); add:

    $acf_image_IDs = array();
    foreach ($bcd->post_custom_fields as $k => $pcf)
    {
    	if (substr($k, -6) === '_image' && $k{0} !== '_')
    	{
    		$a_id = $pcf[0];
    
    		$fake_attachment_object = new \stdClass;
    		$fake_attachment_object->attachment_id   = $a_id;
    		$fake_attachment_object->attachment_data = attachment_data::from_attachment_id( $a_id, $bcd->upload_dir);
    
    		$acf_image_IDs[$a_id] = $fake_attachment_object;
    	}
    }

    Below $converted_attachment_IDs = array(); add:

    foreach ($acf_image_IDs as $acf_image_ID => $data)
    {
    	$converted_attachment_IDs[$acf_image_ID] = $this->copy_attachment( $data, $new_post_id );
    }

    Since ACF also requires some post meta fields prefixes with an underscore, you need to whitelist the fields you wish to copy. For example, I had a field called carrousel-for-content which has the internal name of _carrousel-for-content. Once I whitelisted the key, everything worked smoothly. This setup does assume all ACF settings are the same for every site in the MU environment by XML import.

  3. edward_plainview
    Member
    Plugin Author

    Posted 9 months ago #

    This sounds like it should be converted to a separate ACF-compatability plugin...

  4. Jorijn
    Member
    Posted 9 months ago #

    I agree. :-)

  5. edward_plainview
    Member
    Plugin Author

    Posted 9 months ago #

    If I'm to write an ACF-compatability plugin then somebody is going to have to get in touch with me and help with configuration and testing.

    edward@plainview.se

  6. edward_plainview
    Member
    Plugin Author

    Posted 9 months ago #

    There is now an ACF-compatability plugin in the premium pack.

    See: http://plainview.se/wordpress/threewp-broadcast-premium-pack/#acf

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic