Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I found this wonderdul script that @Bainternet here at Wordpress Answers created, that makes it possible to attach images into posts from a html form..

I tried to combine in into my "html form to post" script.. I does create a post as draft in the backend.. but the image does not show up, and it doesn't show in "media" ..

This is @Bainternet script/answer, Submit post and upload image from front-end

This is mine,

<?php 


if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = 'fashion';
$post_content = $_POST['post_content'];

$new_post = array(
      'ID' => '',
      'post_author' => $user->ID, 
      'post_category' => array($post_category),
      'post_content' => $post_content,
      'post_title' => $post_title,
      'post_status' => 'draft'
    );

$post_id = wp_insert_post($new_post);


        if (!function_exists('wp_generate_attachment_metadata')){
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file]['error'];
                }
                $attach_id = media_handle_upload( $file, $post_id );
            }   
        }
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($post_id,'_thumbnail_id',$attach_id);
        }


// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( 'http://domain.com' );
}      
?>      

<form method="post" action=""> 
<input type="text" name="post_title" size="45" id="input-title" value="heading"/><br>
<input type="text" name="post_url" size="45" id="input-url" value="Url"/><br>
<input type="file" name="thumbnail" id="thumbnail"><br>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> <br>
<input type="hidden" name="new_post" value="1"/> <br>
<input class="subput round" type="submit" name="submit" value="Post"/><br>
</form>
share|improve this question

3 Answers 3

up vote 0 down vote accepted

I think the problem is with your form tag missing enctype="multipart/form-data" so try changing:

<form method="post" action=""> 

to:

<form method="post" action="" enctype="multipart/form-data"> 

and as a side note, if you want to redirect to the newly created post change:

wp_redirect( 'http://domain.com' );

to:

wp_redirect( get_permalink($post_id));
exit();

Update: From the comments i understand that want the image to show inside the content, well that is a different thing but not that hard,

change:

if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($post_id,'_thumbnail_id',$attach_id);
        }

to:

if ($attach_id > 0){
    $post = get_post($post_id,'ARRAY_A');
    $image = wp_get_attachment_image_src( $attach_id );
    $image_tag = '<img src="'.$image[0].'" width="'.$image[1].'" height="'.$image[2].'" />';

    //add image above the content
    $post['post_content'] = $image_tag . $post['post_content'];

    //add image under the content
    //$post['post_content'] = $post['post_content'] . $image_tag;

    $post_id =  wp_update_post( $post );
}
share|improve this answer
    
saw that i missed out on the enctype.. when i tried it afterwards the form just keeps loading and loading.. and i'm just uploading a 500kb image.. get's stuck in just loading.. thanks for answering by the way! :) –  DennisT Jul 12 '11 at 22:26
    
does the post get generated? –  Bainternet Jul 12 '11 at 22:41
    
Works now.. but the image doesn't attach in the post.. but i can see the image in the upload folder and media gallery.. is there any easy way to attach it as "large"? has it anything to do with _thumbnail_id tag? –  DennisT Jul 12 '11 at 22:43
    
you want to insert the image to the post's content? –  Bainternet Jul 12 '11 at 22:46
    
yes! is that a total different thing? –  DennisT Jul 12 '11 at 22:50

For everyone that needs the same script.

What it does, A simple html form uploads a image and some text to wordpress and media gallery and creates a post as draft from the content posted. And redirects you to the created post for preview.

Big thanks to @Bainternet

<?php 


if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = 'todays_post';
$post_content = $_POST['post_content'];

$new_post = array(
      'ID' => '',
      'post_author' => $user->ID, 
      'post_category' => array($post_category),
      'post_content' => $post_content,
      'post_title' => $post_title,
      'post_status' => 'draft'
    );

$post_id = wp_insert_post($new_post);


        if (!function_exists('wp_generate_attachment_metadata')){
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file]['error'];
                }
                $attach_id = media_handle_upload( $file, $post_id );
            }   
        }
if ($attach_id > 0){
    $post = get_post($post_id,'ARRAY_A');
    $image = wp_get_attachment_image_src( $attach_id, 'large' );
    $image_tag = '<p><img src="'.$image[0].'" width="'.$image[1].'" height="'.$image[2].'" /></p>';

    //add image under the content
    $post['post_content'] = $image_tag . $post['post_content'];

    //add image above the content
    //$post['post_content'] = $post['post_content'] . $image_tag;

$post_id =  wp_update_post( $post );
}


// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( get_permalink($post_id));
exit();
}      
?>
<html>
<head>
<title>Form</title>

</head>
<body>

<form method="post" action="" enctype="multipart/form-data">
<input type="text" name="post_title" size="45" id="input-title" value="heading"/><br>
<input type="text" name="post_url" size="45" id="input-url" value="Url"/><br>
<input type="file" name="thumbnail" id="thumbnail"><br>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> <br>
<input type="hidden" name="new_post" value="1"/> <br>
<input class="subput round" type="submit" name="submit" value="Post"/><br>
</form>

</body>
</html>
share|improve this answer

I have tried this code and when I try, nothing happens. I am not using the image yet, I have only been trying to get the form to make a new post. It seems that the form variables are empty though.

I used a var_dump to check and there is nothing there from my form. The code above does not work for me in the latest version of wordpress. I had to modify it to this

$my_post = array();
    $my_post['post_title'] = "My Post"
    $my_post['post_content']  = 'This is my post.';
    $my_post['post_status']   = 'publish';
    $my_post['post_author']   = 1;
    $my_post['post_category'] = array(9);

This is how I did my testing and it does work this way, only post category doesn't work with a slug which could be a problem. Anytime I try to replace post_title with $_POST['name'] the code fails. Here is my form

<form action=" " method="post" name="reservations" id="reservations" enctype="multipart/form-data"> 

    <table width="474" border="0" cellspacing="0" cellpadding="0" id="reserve">
        <tr>
                            <td class="formInput"><input type="text" id="name" name="name" /></td>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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