0

I'm inserting posts by wp_insert_post function. It works like first a post is created to get its id then an image is attached to that post id. I attach images by media sideload function. Here is the whole code module on which I'm working on :HERE . Initially post content is left blank.

$new_post = array(
    'post_title' => $title,   
    'comment_status' => 'open',
    'post_name' => $slug,
    'post_content' => '',
    'post_status' => 'draft',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => 2,
    'post_type' => 'post',
    'post_category' => $cat_ids
    );
    $post_id = wp_insert_post($new_post);

When images is uploaded and attached after that I add a variable $content as below and update the post.

$content = $link[$i].'<br>'.'<img src="'.$src.'" alt="'.$img_title.'" title="'.$img_title.'" class="alignnone size-full wp-image-'.$img_id.'" width="500"/>';

$link[$i] contains the original url of that image. 
$src = wp_get_attachment_url( $img_id );

Everything works fine but problem is when I see the same post in the editor, it only shows as url of image. Rest is not displayed. I tried these:

  1. Changed theme if css posing that problem. Result was same content was displayed as above url and no image tags in post editor.

  2. I changed the code after <br>, as '1234 qwe asd zdc'. Resulted as url1234 qwe asd zdc <br> doesn't seem to work. There was no break.

  3. Echoed direct $content variable without changing anything, on a blank file.php, displayed url and image correctly.

How to resolve this thing? I have been trying this for whole day but no success.

UPDATE: What I have found is probably, the post is updating but not taking new content in line 52 :here

0

If you do $content = "something"; you are replacing the $content variable's value. If you want to append something to the current content, you'd need to do something like:

$content = 'initial content';
$content .= 'more content (notice the dot)';

In this case, if you want to append the image to some existing content you'd need to do:

$content = get_the_content(); //or wherever the existing content is
$content .= $link[$i].'<br><img src="'.$src.'" alt="'.$img_title.'" title="'.$img_title.'" class="alignnone size-full wp-image-'.$img_id.'" width="500"/>';

Or the reverse if you want to put the image first, before any content.

  • Guido, thanks for replying. But I used the same method for creating gallery posts and its working fine.It displays everything. Here is that code: $gallery_content = $url.'<br>'.'[gallery link="post" columns="1" ids="'.$gallery_ids_list.'"]'; – 10wtaylor Apr 30 '13 at 7:07
  • Now that I re-read, I understand that the post is fine on the frontend but doesn't show the image on the backend editor, right? Did you check the source code of the editor view? Does the img tag not appear at all?Perhaps the image is trying to be loaded but the src path is not correct or is relative to the front-end and not the wp-admin folder, thus the browser doesn't render the image. – guidod Apr 30 '13 at 12:28
  • Problem is code contains $content variable which has img tag inside. Post is created. But when I check the post in post editor there is no img tag and as a result no image in post preview.I checked $src, it is also working fine. – 10wtaylor May 1 '13 at 10:40
  • But if the image is trying to be loaded but the src path is not correct or is relative to the front-end, it should at least display alternate text alt for that image but there is none. – 10wtaylor May 1 '13 at 10:41
  • My whole code is there in pastebin: pastebin.com/2CccxLnj – 10wtaylor May 1 '13 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.