从前端上传文件

标题前:

<?php 
    $post_id = $post->ID;
    if ( isset( $_POST['html-upload'] ) && ! empty( $_FILES ) ) {
        require_once( ABSPATH . 'wp-admin/includes/admin.php' );
        $id = media_handle_upload( 'async-upload', $post_id ); //post id of Client Files page
        unset( $_FILES );
        if( is_wp_error( $id ) ) {
            $errors['upload_error'] = $id;
            $id = false;
        }

        if( $errors ) {
            echo "<p>There was an error uploading your file.</p>";
        } else {
            echo "<p>Your file has been uploaded.</p>";
        }
    }
?>

之后,将其添加到您想要表单的位置:

<?php if ( is_user_logged_in() ) {      ?>

    <form id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
        <p id="async-upload-wrap"><label for="async-upload">File Upload:</label>
        <input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload"></p>

        <p><input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>" />
        <?php wp_nonce_field( 'client-file-upload' ); ?>
        <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /></p>

        <p><input type="submit" value="Save all changes" name="save" style="display: none;"></p>
    </form>

<?php } ?>
温馨提示 : 非特殊注明,否则均为©李联华的博客网原创文章,本站文章未经授权禁止任何形式转载;IP地址:18.191.103.144,归属地:俄亥俄州Dublin ,欢迎您的访问!
文章链接:https://www.lilianhua.com/file-upload-from-frontend.html
Popup Image

通知

本站原则上是免费提供技术支持,但是服务器维护和运营成本高,可以实行自由赞助:赞助

Loading...