• Hi Im using Multisite and trying to replicate posts from main blog to child, and from child to main blog using the Multiposu-MU plugin.

    Everything goes weel except with the Featured Image, I needed to add some code lines to transfer the featured images here is the code

    function multiPost( $postID ) {
    
    			global $switched, $blog_id, $current_user;
    			get_currentuserinfo();
    			// ensure multipost is only triggered from source blog to prevent massive cascade of posts
    			if( $blog_id != $_POST['HMMPMU_source_blog_id'] ){
    				return false;
    			}
    			// get existing child posts, if any
    			$childPosts = unserialize( get_post_meta( $postID, 'HMMultipostMU_children', true ) );
    			if( empty( $childPosts ) ) {
    				$childPosts = array(); // key = blog_id, val = post_id
    			}
    			// get post
    			$thisPost = get_post( $postID );
    			$postCustomFields = get_post_custom( $postID );
    
    			// ******HERES WHAT ADDED********
    			$image_url = wp_get_attachment_image_src( get_post_thumbnail_id($postID),full );
    			$filetype = wp_check_filetype( $image_url[0] );
    			$wp_upload_dir = wp_upload_dir();
    			$argsFile = array(
    				'guid'           => $wp_upload_dir['url'] . '/' . basename( $image_url[0] ),
    	            'post_mime_type' => $filetype['type'],
    	            'post_title'     => esc_attr( $thisPost->post_title), // you may want something different here
    	            'post_content'   => '',
    	            'post_status'    => 'inherit'
            	);		
    
    	    /**************************************/
    
    			unset( $postCustomFields['HMMultipostMU_children'] ); // we don't want to copy this one to sub-blogs... rippled chaos will ensue!
    			unset( $postCustomFields['_edit_lock'] );
    			unset( $postCustomFields['_edit_last'] );
    			$thisPostTags = wp_get_post_tags( $postID );
    			// get array of categories (need ->name parameter)
    			$thisPostCategories = wp_get_object_terms( $postID, 'category' );
    			$masterPostCats = array();
    			// pull category id/name into array for easier searching
    			foreach( $thisPostCategories as $thisPostCategory ) {
    				$masterPostCats[$thisPostCategory->term_id] = $thisPostCategory->name;
    			}
    			$thisPostTags_string = '';
    			foreach( $thisPostTags as $thisPostTag ) {
    				$thisPostTags_string .= $thisPostTag->name .',';
    			}
    			$thisPostTags_string = trim( $thisPostTags_string, ',' );
    			// create post object with this post's data
    			$dupePost = array(
    				'post_title' => $thisPost->post_title,
    				'post_content' => $thisPost->post_content,
    				'post_status' => $thisPost->post_status,
    				'post_author' => $thisPost->post_author,
    				'post_excerpt' => $thisPost->post_excerpt,
    				'post_date' => $thisPost->post_date,
    				'post_date_gmt' => $thisPost->post_date_gmt,
    				'post_modified' => $thisPost->post_modified,
    				'post_modified_gmt' => $thisPost->post_modified_gmt,
    				'tags_input' => $thisPostTags_string
    			);
    			//check if post is sticky
    			$sticky = is_sticky($postID);
    
    			// get list of blogs
    			//$subBlogs = get_blog_list( 0, 'all' );
    			$subBlogs = get_blogs_of_user( $current_user->ID );
    			// get the subBlogs in chronological order as get_blog_list() pulls in reverse cron order
    
    			foreach( $subBlogs as $subBlog ) {
    				// if user selected specific blogs in which to post and this blog isn't among them, skip to next
    				if( !empty( $_POST['HMMPMU_selectedSubBlogs'] ) && !in_array( $subBlog->userblog_id, $_POST['HMMPMU_selectedSubBlogs'] ) ) {
    					// if a previous post exists on this blog, but isnt now needed, delete it
    					if( in_array( $subBlog->userblog_id, array_keys( $childPosts ) ) ) {
    						if( switch_to_blog( $subBlog->userblog_id ) === true ) {
    							wp_delete_post( $childPosts[$subBlog->userblog_id] );
    							// jump back to master blog
    							restore_current_blog();
    							unset( $childPosts[$subBlog->userblog_id] );
    						}
    					}
    					continue;
    				}
    
    				if( $blog_id != $subBlog->userblog_id ) { // skip the current blog
    					$childPostID = 0;	// used to hold new/updated post for each sub-blog
    					// switch each sub-blog
    					if( switch_to_blog( $subBlog->userblog_id ) === true ) {
    							if( isset( $childPosts[$subBlog->userblog_id] ) ) {
    								// there is already an existing post for this blog
    								$dupePost['ID'] = $childPosts[$subBlog->userblog_id];	// set post ID
    								$childPostID = wp_update_post( $dupePost );
    
    								unset( $dupePost['ID'] );	// remove post ID from duped post object
    							} else {
    								// no existing post for this blog, and was checked, create a new post
    								if( !empty( $_POST['HMMPMU_selectedSubBlogs'] ) && in_array( $subBlog->userblog_id, $_POST['HMMPMU_selectedSubBlogs'] ) ) {
    									$childPostID = wp_insert_post( $dupePost );	
    
    								}
    							}
    							if( $childPostID > 0 ) {
    								// get the new post's object
    								$childPost = get_post( $childPostID );
    								// get existing categories for this blog
    								$childBlogCats = get_terms( 'category' );
    								// if matching category found, add post to it
    								$matchingCatID = 0;
    								$childCatsToAdd = array();
    								foreach( $masterPostCats as $masterPostCats_key=>$masterPostCats_value ) {
    									$matchingTerm = get_term_by( 'name', $masterPostCats_value, 'category' );
    									if( $matchingTerm === false ) {
    										// create new term/category
    										$newCatID = wp_create_category( $masterPostCats_value );
    										$matchingTerm = get_term( $newCatID, 'category' );
    									}
    									array_push( $childCatsToAdd, $matchingTerm->term_id );
    								}
    								// add terms/categories to post
    								wp_set_post_categories( $childPostID, $childCatsToAdd );
    								// update or set custom fields
    								foreach( $postCustomFields as $postCustomFieldKey=>$postCustomFieldValue ) {
    									//update existing custom field (this adds first if fields does not yet exist)
    									foreach( $postCustomFieldValue as $postCustomFieldValueItem ){
    										update_post_meta( $childPostID, $postCustomFieldKey, $postCustomFieldValueItem );
    									}
    								}
    								// if the update/new post was successful, add it to the array of child posts
    								$childPosts[$subBlog->userblog_id] = $childPostID;
    
    								// *********MOREE THAT I ADDED********
            						$thumb_id = wp_insert_attachment( $argsFile, $image_url[0],  $childPostID );
            						require_once(ABSPATH . 'wp-admin/includes/image.php');
    						        $metadata = wp_generate_attachment_metadata( $thumb_id, $image_url[0] );
    						        wp_update_attachment_metadata( $thumb_id, $metadata );
    
    						        // Finally! set our post thumbnail
    						        update_post_meta( $childPostID, '_thumbnail_id', $thumb_id );
    
    /*********************************/
    
    								// if the original post was sticky, set the new one sticky. otherwise remove sticky.
    								if($sticky === true){
    									stick_post($childPostID);
    								} elseif(is_sticky($childPostID)===true) {
    									unstick_post($childPostID);
    								}
    							}
    						// jump back to master blog
    						restore_current_blog();
    					}
    				}
    			}

    Well, when i post from child blog to master blog everything is ok, the featured image works, but when i post from master to child the featured image shows broken, because de url “uplodas/sites/2/…”.

    Anyone can help me please?

    Sorry about my english, i from Brazil!

  • The topic ‘Featured Images wrong path using Multipost-MU plugin on multisite’ is closed to new replies.