upload files user front-end error 400 admin-ajax.php

  • Avatar de Inconnu

    Hello,

    I’m throwing a message in a bottle here. Everything I’ve searched for on the internet or what ChatGPT has told me was in vain.

    I have a 400 error in my console on admin-ajax.php when I try to upload files from my “dropbox” that I placed on my website (front-end).

    In the media library, I only get the error “An error occurred during the upload. Please try again later.”

    Also, I have no errors generated by Apache or PHP in my logs, which is very strange.

    Behavior after this message: the user is logged out when they refresh the page and redirected to the homepage (login page).

    I forgot to mention: Users are created by a script:

    if (isset($_POST['activer_utilisateur'])) {
        $email = sanitize_email($_POST['activation_email']);
        $nom = sanitize_text_field($_POST['activation_nom']);
        $id = sanitize_text_field($_POST['activation_id']);
        $main_contact_id = sanitize_text_field($_POST['main_contact_id']);
    
        if (!email_exists($email)) {
            $password = wp_generate_password();
    
            $user_id = wp_insert_user(
                array(
                    'user_login' => $email,
                    'user_pass'  => $password,
                    'user_email' => $email,
                    'role'       => 'author',
                    'rich_editing' => 'true',
                    'show_admin_bar_front' => 'false',
                )
            );
    
            if (!is_wp_error($user_id)) {
                $user = new WP_User($user_id);
                $user->set_role('author');
    
                wp_update_user(array(
                    'ID' => $user_id,
                    'display_name' => $nom
                ));
    
                update_user_meta($user_id, 'sellsy_id', $id);
                update_user_meta($user_id, 'sellsy_main_contact_id', $main_contact_id);
    
                if (isset($main_contact_id) && !empty($main_contact_id)) {
                    $sellsy_api = new SellsyAPI();
                    $main_contact = $sellsy_api->get_contact($main_contact_id);
    
                    $civility = $main_contact['civility'];
                    $first_name = $main_contact['first_name'];
                    $last_name = $main_contact['last_name'];
    
                    update_user_meta($user_id, 'sellsy_main_contact_civiliy', $civility);
                    update_user_meta($user_id, 'sellsy_main_contact_first_name', $first_name);
                    update_user_meta($user_id, 'first_name', $first_name);
                    update_user_meta($user_id, 'sellsy_main_contact_last_name', $last_name);
                    update_user_meta($user_id, 'last_name', $last_name);
    
                    if (!user_can($user_id, 'upload_files')) {
                        $user = new WP_User($user_id);
                        $user->add_cap('upload_files');
                    }
                }
    
                //wp_mail($email, 'Your account has been created', 'Here are your credentials: Email: ' . $email . ' | Password: ' . $password);
    
                set_transient('client_clients_success', '<b style="color:green">User created successfully!</b>.', 60);
            } else {
                set_transient('client_clients_error', '<b style="color:red">Error while creating the user.</b>', 60);
            }
        } else {
            set_transient('client_clients_error', '<b style="color:red">This email is already in use.</b>', 60);
        }
    }
    

    I have indeed loaded the media library on the front-end:

    function enqueue_sortable_scripts() {
        wp_enqueue_editor(); // Load scripts for the classic editor
        wp_enqueue_script('wp-util'); // Necessary for wp.editor
        wp_enqueue_script('jquery');
        wp_enqueue_media();
    }
    add_action('wp_enqueue_scripts', 'enqueue_sortable_scripts');

    And finally, my script that displays my wp.media modal:

    jQuery(document).ready(function($) {
        $(".boite_depot").click(function(e){
            e.preventDefault();
            var project_id = $(this).data('project-id');
            var frame = wp.media({
                title: "Select or upload media for project " + project_id,
                library: {
                    uploadedTo: project_id
                },
                multiple: true,
                button: {
                    text: "Add to project"
                }
            });
    
            frame.on('select', function() {
                var selection = frame.state().get('selection');
                var attachment_ids = [];
                selection.each(function(attachment) {
                    attachment_ids.push(attachment.id);
                });
                // $.post processing
            });
    
            frame.open();
        });
    });

    Thanks a lot for your help

  • Le sujet ‘upload files user front-end error 400 admin-ajax.php’ est fermé aux nouvelles réponses.