面向用户的表单,用于在WordPress上上传图像

时间:2012-04-11 作者:JasonDavis

我有一个客户,他购买了一个wordpress主题,该主题是为发布工作列表而构建的,用户可以创建一个帐户,然后发布工作列表,但我的客户希望将其转换为发布车轮列表(汽车车轮)。

到目前为止,我已经对它进行了很好的转换,但我需要允许用户上传一个列表的图像。列表只是一个帖子,但它是从面向用户的表单发布的,而不是在管理面板中发布的。

我想利用the_post_thumbnail() 是否有可能,我是否可以允许用户通过此上传,而无需进入管理面板?

2 个回复
最合适的回答,由SO网友:Adam 整理而成

您可能想查看此资源,它提供了一个相对简单且易于理解的代码段来实现这一点!

Link to the original source from @Matthew Price.

我粘贴了下面的代码以延长使用寿命,但请注意,这不是我的代码,也不是我的作者。

<?php
// if you have this in a file you will need to load "wp-load.php" to get access to WP functions.  If you post to "self" with this code then WordPress is by default loaded
require $_SERVER[\'DOCUMENT_ROOT\'] . "/wp-load.php";
// require two files that are included in the wp-admin but not on the front end.  These give you access to some special functions below.
require $_SERVER[\'DOCUMENT_ROOT\'] . "/wp-admin/includes/file.php";
require $_SERVER[\'DOCUMENT_ROOT\'] . "/wp-admin/includes/image.php";

// required for wp_handle_upload() to upload the file
$upload_overrides = array( \'test_form\' => FALSE );

global $current_user;
get_currentuserinfo();
$logged_in_user = $current_user->ID;

// count how many files were uploaded
$count_files = count( $_FILES[\'files\'] );

// load up a variable with the upload direcotry
$uploads = wp_upload_dir();

// foreach file uploaded do the upload
foreach ( range( 0, $count_files ) as $i ) {

    // create an array of the $_FILES for each file
    $file_array = array(
        \'name\'      => $_FILES[\'files\'][\'name\'][$i],
        \'type\'      => $_FILES[\'files\'][\'type\'][$i],
        \'tmp_name\'  => $_FILES[\'files\'][\'tmp_name\'][$i],
        \'error\'     => $_FILES[\'files\'][\'error\'][$i],
        \'size\'      => $_FILES[\'files\'][\'size\'][$i],
    );

    // check to see if the file name is not empty
    if ( !empty( $file_array[\'name\'] ) ) {

        // upload the file to the server
        $uploaded_file = wp_handle_upload( $file_array, $upload_overrides );

        // checks the file type and stores in in a variable
        $wp_filetype = wp_check_filetype( basename( $uploaded_file[\'file\'] ), null );  

        // set up the array of arguments for "wp_insert_post();"
        $attachment = array(
            \'post_mime_type\' => $wp_filetype[\'type\'],
            \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename( $uploaded_file[\'file\'] ) ),
            \'post_content\' => \'\',
            \'post_author\' => $logged_in_user,
            \'post_status\' => \'inherit\',
            \'post_type\' => \'attachment\',
            \'post_parent\' => $_POST[\'post_id\'],
            \'guid\' => $uploads[\'baseurl\'] . \'/\' . $file_array[\'name\']
        );

        // insert the attachment post type and get the ID
        $attachment_id = wp_insert_post( $attachment );

        // generate the attachment metadata
        $attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file[\'file\'] );

        // update the attachment metadata
        wp_update_attachment_metadata( $attachment_id,  $attach_data );

                // this is optional and only if you want to.  it is here for reference only.
                // you could set up a separate form to give a specific user the ability to change the post thumbnail
                // set_post_thumbnail( $_POST[\'post_id\', $attachment_id );

    }
}
// setup redirect.  i used the referer so that i can say "file uploaded" in a div if the files query string variable is set.
header("Location: " . $_SERVER[\'HTTP_REFERER\'] . "/?files=uploaded");
?>
前端形式的一个例子是;

<!-- you can place the upload script into your theme folder or in a plugin -->
<form action="/path/to/upload/script/upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple>
    <!-- The following hidden field contains the post id for which you will be uploading images to -->
    <input type="hidden" name="post_id" value="100">
    <input type="submit" class="btn" value="Start Upload »">
</form>
您可以调整它以满足您的需要。

SO网友:TomHarrigan

你需要给用户上传文件的能力,我用过User Role Editer 允许在目录站点上上载图像。您可以选择普通用户的用户角色,并选中upload\\u files(上载文件)框以授予他们上载权限。

结束

相关推荐