• Resolved jan212

    (@jan212)


    Hello everbody,

    my name is Jan and i am new in WP multisite & BuddyPress. I am a designer&developer located in Solingen/Germany. Actually i am standing at a point where some help would be motivating and all documentation is readed :(.

    I am coding a function which should push a custom post type(recipe) on hook save_post from any network blog where the hook is triggered to a main blog acting as a container to save all custom posts from the network as a draft, moderate them and publish or delete them.

    This is working well at this point except “transfering” the posts thumbnail from blog1_post1 to global_blog_post1.

    I am working with(static filename for testing):

    $filename = '2011/05/myimg.png';
              $wp_filetype = wp_check_filetype(basename($filename), null );
    
              $attachment = array(
                 'post_mime_type' => $wp_filetype['type'],
                 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
                 'post_content' => '',
                 'post_status' => 'inherit'
              );

    and save it after a switch_to_blog($global_blog_id) with

    /* finaly insert */
              $p = wp_insert_post( $my_post );
    
              $attach_id = wp_insert_attachment( $attachment, $filename, $p );
              // you must first include the image.php file
              // for the function wp_generate_attachment_metadata() to work
              require_once(ABSPATH . "wp-admin" . '/includes/image.php');
              $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
              wp_update_attachment_metadata( $attach_id,  $attach_data );

    As i read in the docu wp_insert_attachment is only excepting files under the local upload dir, but the file is located in origins blogs.dir in this case. This and others facts causing malfunction… The post container and _wp_attached_file & _wp_attachment_metadata are generated in the DB but _wp_attachment_metadata holds no/wrong serialized values a:0:{} and the _thumbnail_id meta key isn’t generated at all.

    I will need a function/method which copies the file from

    blogs.dir
    -ID (actual post)
    --YYYY
    ---MM
    ----filename

    to the same dir and the ID of the global blog, the correct _wp_attachment_metadata and a small function to add the _thumbnail_id…

    Am i right or another approach…? A smarter way? Any hints would be more than nice!

    Thanks in advance & many regards
    Jan

    Attachments:
    The _wp_attachment_metadata meta_value(serialized) should look like:

    [Code moderated as per the Forum Rules. Please use the pastebin]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter jan212

    (@jan212)

    Hi Andrea,

    thanks for this first hint – i am not using Sitewide tags plugin but i will look into the code, is that what u mean?

    Edit:
    add_theme_support( 'post-thumbnails' ); is still enabled by default, all client blogs are using twentyten theme in this early state ??

    Thread Starter jan212

    (@jan212)

    to be honest, i am confused atm ??

    Update:
    I took a look in the sitewide tags plugin code, it stores the thumb in a new custom field. If there is no other way or solution to use built in functions and architecture i will do it the same way and modify the template i think… A disadvantage of this solution is that i wouldn’t keep the posts thumbnail in the global blog if origin post/blog deleted. An advantage is that i don’t have to double store it.
    In conclusion i think there’s no other way than to copy it with php copy()

    Thread Starter jan212

    (@jan212)

    I am getting deeper into it, i think the right function would be
    media_handle_sideload()… sleep mode for now.

    Thread Starter jan212

    (@jan212)

    Hi everbody,

    actual state:
    after a switch_to_blog and a $p = wp_insert_post( $my_post ); comes

    /* here goes thumbnail part #2 */
        		  $filepath = $filename;
        		  //$filepath = 'wp-content/blogs.dir/2/files/'. $filename ;
        		  $wp_filetype = wp_check_filetype( basename( $filepath ), null );
        		  $aFile["name"] = basename( $filepath );
        		  $aFile["type"] = $wp_filetype;
        		  $afile["tmp_name"] = $filepath;
    
        		  $attach_id = media_handle_sideload( $aFile, $p, 'This is optional file title' );
              /**/

    while $filename is 2011/05/myimg.png.

    Doesn’t do anything atm… Tables staying empty. Any ideas?

    Regards
    Jan

    Well… the problem in a nutshell is that the sitewide tags plugin pretty much does what you want, and I gave my answer based on using that plugin.

    Thread Starter jan212

    (@jan212)

    The difference is in between “pretty much” and “exactly” and the deep focus and the nutshell… My scenario is different even if i wish i wouldn’t be ??

    I need somebody who is familar with media_handle_sideload ??

    Thread Starter jan212

    (@jan212)

    Got the function running but it’s ignoring switch_to_blog and copies the files in the origin blogs.dir. It doesn’t create the _thumnail_id entry in wp_ID_postmeta ?? Confusing shit.

    Thread Starter jan212

    (@jan212)

    i’ve fuckin got it.

    Hi Jan212 – Congrats on getting it to work.

    I’m doing an import from a really old Joomla into WP. Having copied all the posts (7000+) over, now I need to also copy their related images.

    Basically I just have a url to each image.

    I’ve just started looking at using media_handle_sideload but haven’t gotten it to work yet.

    Can you offer any tips on how you got it work?

    Many thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Transfer thumbnail from any network blog to global blog’ is closed to new replies.