dtkyayvldeaqhl7151 2013-10-12 10:53
浏览 41

在wordpress上以编程方式上传照片

I am trying to upload photo to wordpress with post from frontend and without logging in. Firstly I have added the functionality to add post without uploading photo from frontend in wordpress with a plugin. Now I am trying to add photo uploading functionality by adding some code on this plugin. But after adding the codes it doesnt upload photo, but it posts successfully. I dont know where is my fault. Please help me. My code is below,

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

And this code process the data sent from the code of above,

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");

?>
  • 写回答

1条回答 默认 最新

  • duanmaduan1848 2015-02-16 20:55
    关注

    It is much better to use the media_handle_upload function. Use the following function to upload the file and return the attachment ID...

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

    Then when you process the form you can use this code to execute the function and make the uploaded file the featured image...

    if( !empty( $_FILES ) ) {
    
        $attachment_id = guestposts_handle_attachment( 'upload', $post_id );
    
        if( $attachment_id ) {
    
            set_post_thumbnail($post_id, $attachment_id);
    
        }
    
    }
    

    Hope that helps.

    Regards

    Dan

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端