繁体   English   中英

WordPress-尝试在注册表单中添加“上传图片”部分

[英]WordPress - Attempting to add an “Upload Image” section to the registration form

我正在尝试建立一个wordpress网站。 我需要用户在注册时上传身份证明,我不确定该怎么做。

我已经通过我正在使用的主题上的functions.php将“上传”按钮添加到站点。 我还设法让网站验证了图像是否已上传。 例如:如果用户未上传任何内容并点击“注册”,它将告诉他们该字段是必填字段。

这是我在主题functions.php中使用的代码

function text_domain_woo_reg_form_fields() {
    ?>
    <p class="form-row form-row-first">
        <label for="billing_first_name"><?php _e('First name', 'text_domain'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" />
    </p>
    <p class="form-row form-row-last">
        <label for="billing_last_name"><?php _e('Last name', 'text_domain'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" />
    </p>
    <p class="form-row form-row-upload">
        <label for="image_verification"><?php _e('Upload ID', 'text_domain'); ?><span class="required">*</span></label>
        <input type="file" name="image_verification" id="image_verification" enctype="multipart/form-data" accept="image/*" value="<?php if (!empty($_POST['image_verification'])) esc_attr_e($_POST['image_verification']); ?>" />
    </p>
    <div class="clear"></div>
    <?php
}
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');

//validate fields
function text_domain_woo_validate_reg_form_fields($username, $email, $validation_errors) {
    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) {
        $validation_errors->add('billing_first_name_error', __('<strong>Error</strong>: First name is required!', 'text_domain'));
    }

    if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) {
        $validation_errors->add('billing_last_name_error', __('<strong>Error</strong>: Last name is required!.', 'text_domain'));
    }

    if (isset($_POST['image_verification']) && empty($_POST['image_verification'])) {
        $validation_errors->add('image_verification', __('<strong>Error</strong>: ID is mandatory to register. Please upload your ID', 'text_domain'));
    }
}
add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3);
//save fields & store image
function text_domain_woo_save_reg_form_fields($customer_id) {
    //First name field
    if (isset($_POST['billing_first_name'])) {
        update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
        update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
    }
    //Last name field
    if (isset($_POST['billing_last_name'])) {
        update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
        update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
    }
            $target_path = "c:/"; 
        $target_path = $target_path.basename( $_FILES['image_verification']['name']); 
        echo " <script>console.log('it hit!!'); </script>";
        echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
        if(move_uploaded_file($_FILES['image_verification']['tmp_name'], $target_path)) { 
            echo "File uploaded successfully!";
            echo " <script>console.log('image upload - success!'); </script>";
            echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
        } else { 
            echo "Sorry, file not uploaded, please try again!"; 
            echo " <script>console.log('image upload - fail!'); </script>";
            echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
        } 
}

add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');
<?php media_handle_upload( $file_id, $post_id, $post_data, $overrides ); ?>

<?php

// Check that the nonce is valid, and the user can edit this post.
if ( 
    isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
    && wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
    && current_user_can( 'edit_post', $_POST['post_id'] )
) {
    // The nonce was valid and the user has the capabilities, it is safe to continue.

    // These files need to be included as dependencies when on the front end.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    // Let WordPress handle the upload.
    // Remember, 'my_image_upload' is the name of our file input in our form above.
    $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

} else {

    // The security check failed, maybe show the user an error.
}

上传表单可能如下所示:

<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
    <input type="file" name="my_image_upload" id="my_image_upload"  multiple="false" />
    <input type="hidden" name="post_id" id="post_id" value="55" />
    <?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
    <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM