Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript WordPress-正在尝试添加一个;上传图像“;注册表格的第节_Javascript_Php_Html_Wordpress - Fatal编程技术网

Javascript WordPress-正在尝试添加一个;上传图像“;注册表格的第节

Javascript WordPress-正在尝试添加一个;上传图像“;注册表格的第节,javascript,php,html,wordpress,Javascript,Php,Html,Wordpress,我正在尝试建立一个wordpress网站。我需要用户在注册时上传身份证明,我不知道该怎么做 我已经设法通过functions.php在我使用的主题上添加了“upload”按钮。我还设法让网站验证图像是否已上传。例如:如果用户不上传任何内容并点击“注册”,它将告诉他们该字段是必填字段 下面是我在theme functions.php中使用的代码 function text_domain_woo_reg_form_fields() { ?> <p class="form-

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

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

下面是我在theme 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');
function text\u domain\u woo\u reg\u form\u fields(){
?>

*


Hey Shanu!非常感谢您的回复。我真的很感谢您的帮助。我应该把这个放在function.php中的哪个函数中。html部分放在函数text\u domain\u woo\u reg\u form\u fields()下,但是处理图像上传的php部分呢。我应该把它放在哪里?放在text\u domain\u woo\u save\u reg\u form\u 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>