2013-10-12 2 views
2

프론트 엔드의 게시물과 로그인하지 않고 사진을 업로드하려고합니다. 먼저 플러그인으로 프론트 엔드에서 사진을 업로드하지 않고 게시물을 추가하는 기능을 추가했습니다. 이제이 플러그인에 몇 가지 코드를 추가하여 사진 업로드 기능을 추가하려고합니다. 그러나 코드를 추가 한 후에는 사진을 업로드 할 수 없지만 성공적으로 게시됩니다. 나는 내 잘못이 어디에 있는지 모른다. 도와주세요. 내 코드는 다음과 같습니다Wordpress에서 프로그래밍 방식으로 게시물로 사진 업로드

function guestposts_shortcode($atts) { 
    extract (shortcode_atts (array(
    'cat' => '1', 
    'author' => '1', 
    'thanks' => get_bloginfo('home'), 
), $atts)); 

return '<form enctype="multipart/form-data" class="guests-post" action="'. plugin_dir_url("guest-posts.php") .'guest-posts/guest-posts-submit.php" method="post">  




<strong>' . __('Post Title:', 'guest-posts') . '</strong><br> 
<input type="text" name="title" size="60" required="required" placeholder="' .__('Post title here', 'guest-posts') . '"><br> 

<input type="file" name="upload" id="file"><br> 

    <strong>' . __('Story', 'guest-posts') . '</strong> 
    '. wp_nonce_field() .' 
     <textarea rows="15" cols="72" required="required" name="story" placeholder="' . __('Start writing your post here', 'guest-posts') . '"></textarea><br> 
    <strong>' . __('Tags', 'guest-posts') . '</strong><br> 
     <input type="text" name="tags" size="60" placeholder="' . __('Comma Separated Tags', 'guest-posts') . '"><br><br> 
    <strong>' . __('Your Name', 'guest-posts') . '</strong><br> 
     <input type="text" name="author" size="60" required="required" placeholder="' . __('Your name here', 'guest-posts') . '"><br> 
    <strong>' . __('Your Email', 'guest-posts') . '</strong><br> 
     <input type="email" name="email" size="60" required="required" placeholder="' . __('Your Email Here', 'guest-posts') . '"><br> 
    <strong>' . __('Your Website', 'guest-posts') . '</strong><br> 
     <input type="text" name="site" size="60" placeholder="' . __('Your Website Here', 'guest-posts') . '"><br><br><br> 
    <input type="hidden" value="'. $cat .'" name="category"><input type="hidden" value="'. $author .'" name="authorid"> 
    <input type="hidden" value="'. $thanks .'" name="thanks"> 
    <input type="hidden" value="'. str_replace('/wp-content/themes', '', get_theme_root()) .'/wp-blog-header.php" name="rootpath"> 
    <input type="submit" value="' . __('Submit The Post', 'guest-posts') . '"> <input type="reset" value="' . __('Reset', 'guest-posts') . '"><br> 
    </form> 
    '; 
} 
add_shortcode('guest-posts', 'guestposts_shortcode'); 

그리고이 코드 과정 위의 코드에서 전송 된 데이터는

ob_start(); 
require_once($_POST["rootpath"]); 

     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'); 
     } 

$title = $_POST["title"]; 
$story = $_POST["story"]; 
$tags = $_POST["tags"]; 
$author = $_POST["author"]; 
$email = $_POST["email"]; 
$site = $_POST["site"]; 
$authorid = $_POST["authorid"]; 
$category = $_POST["category"]; 
$thankyou = $_POST["thanks"]; 
$path = $_POST["rootpath"]; 
$nonce=$_POST["_wpnonce"]; 
$filename=$_FILES["file"]["name"]; 
//$file=$_FILES["file"]; 
//Load WordPress 
//require($path); 

//Verify the form fields 
if (! wp_verify_nonce($nonce)) die('Security check'); 

$new_post = array(
     'post_title' => $title, 
     'post_content' => $story, 
     'post_category' => $category, // Usable for custom taxonomies too 
     'tags_input' => $tags, 
     'post_status' => 'publish',   // Choose: publish, preview, future,  draft, etc. 
     'post_type' => 'post', //'post',page' or use a custom post type if you want to 
     'post_author' => $authorid //Author ID 
); 
//save the new post 
    $post_id = wp_insert_post($new_post); 




if ($_FILES) { 

      foreach ($_FILES as $file => $array) { 
       if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) { 
        return "upload error : " . $_FILES[$file]['error']; 

       } 

    $wp_filetype = wp_check_filetype(basename($file), null); 
    $wp_upload_dir = wp_upload_dir(); 

$attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename($file), 
    'post_mime_type' => $wp_filetype['type'], 
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($file)), 
    'post_content' => '', 
    'post_status' => 'publish' 
); 


      $attach_id = wp_insert_attachment($attachment, $file, $post_id); 
    $attach_data = wp_generate_attachment_metadata($attach_id, $file); 
     wp_update_attachment_metadata($attach_id, $attach_data); 


      } 
     } 
/* Insert Form data into Custom Fields */ 
add_post_meta($post_id, 'author', $author, true); 
add_post_meta($post_id, 'author-email', $email, true); 
add_post_meta($post_id, 'author-website', $site, true); 

header("Location: $thankyou"); 

?> 

답변

0

media_handle_upload 기능을 사용하는 것이 훨씬 낫다. 당신이 양식을 처리 할 때 ...

기능을 실행하고 업로드 된 파일에게 기능을 갖춘 이미지를 만들기 위해이 코드를 사용할 수 있습니다 그리고 ...

function guestposts_handle_attachment($file_handler, $post_id) { 

    require_once(ABSPATH . 'wp-admin/includes/image.php'); 
    require_once(ABSPATH . 'wp-admin/includes/file.php'); 
    require_once(ABSPATH . 'wp-admin/includes/media.php'); 

    $attachment_id = media_handle_upload($file_handler, $post_id); 

    if(!is_wp_error($attachment_id)) { 

     return $attachment_id; 

    } 

    return false; 

} 

을 파일을 업로드하고 첨부 파일 ID를 반환하는 다음과 같은 기능을 사용하여

if(!empty($_FILES)) { 

    $attachment_id = guestposts_handle_attachment('upload', $post_id); 

    if($attachment_id) { 

     set_post_thumbnail($post_id, $attachment_id); 

    } 

} 

희망이 있습니다.

감사

관련 문제