Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * In WordPress Multisite, when an image is added to a blog post in edit mode, I need to add it to the DB as a post.
- * For some reason, it adds the post as an attachment to wp_1_posts instead of the correct blog's table.
- */
- /*
- * all of these variables are set, and correct
- */
- $imgurl = $_POST['imgurl'];
- $imgh = $_POST['imgh'];
- $imgw = $_POST['imgw'];
- $blog_id = $_POST['blog_id']; // on my test box, this value is 37
- $post_id = $_POST['post_id'];
- // Switching to the blog that I want to add the image to
- //switch_to_blog($blog_id); // <--- Verified blog_id is 37
- //$wpdb->set_blog_id(37); // <---- This actually switches to blod id 37
- // Array of data about the image. These are the four required keys
- $attachment = array(
- 'post_mime_type' => 'image/jpeg',
- 'post_title' => 'image title',
- 'post_content' => '',
- 'post_status' => 'attachment'
- );
- // Insert the attachment into the DB. Returns the post_id of the post
- $attach_id = wp_insert_attachment($attachment, '', $_POST['post_id'] );
- if ($attach_id == 0)
- {
- // TODO: ADD ERROR HANDLING
- }
- // Build the HTML that will go into the draft post
- $ret_url = '<a href="' . $imgurl . '"><img class="alignnone size-medium wp-image-' . $attach_id . '" src="' . $imgurl .'" alt="" width="' . $imgw . '" height="' . $imgh . '" /></a>';
- // Return the HTML & die
- echo $ret_url;
- restore_current_blog();
- die();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement