upload images on front by user

2019-08-18 15:52发布

I'm using a form on my website using wordpress to allow users to create posts. It works perfectly fine, I'm abble to assign the posts to a custom post type, add Advanced Custom Fields, content, tags...

What I'm trying to do is to allow the user to also upload images, using the same coding architecture, as it works fine this way since.

User don't have to be registred, so when uploading an image, He'll be abble to browse is computer and then upload the image.

the image should then be saved in my media library and database, and assign to the post.

I want to be abble to assign images to post_tumbnails for example, or to custom fields.

8 new inputs for 8 images in my form.

image_1 image_2, and so on...

like this : , and then being abble to use the image object inside my insert array.

here is my php code :

<?php
if(isset($_POST['submit'])){

    $err = array();
    $err['status'] = true;
    $output = "";

    if(empty($_POST['nom'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Nom" ne peut être vide.';
    }

    if(empty($_POST['prenom'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Nom" ne peut être vide.';
    }

    if(empty($_POST['telephone'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Nom" ne peut être vide.';
    }

    if(empty($_POST['@mail']) || !preg_match('/[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}/',$_POST['@mail'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champs "Mail" est mal renseigné';
    }

    if($err['status']){

        $insert = array(
            'post_status'   => 'publish',
            'post_title'    => $_POST ['nom'] . ' ' . $_POST ['prenom'],
            'post_category' => array(11),
            'post_author'   => 999,
            'tags_input' => htmlentities($_POST['tags']),
            'post_type' => 'vendre',        
            );

        $post_id = wp_insert_post($insert);

        if($post_id != 0){

/* CUSTOM FIELDS */

update_field('field_546f2983c57ae',htmlentities($_POST['civilite']),$post_id);
update_field('field_546f22db11f3e',htmlentities($_POST['nom']),$post_id);
update_field('field_546f22e011f3f',htmlentities($_POST['prenom']),$post_id);
update_field('field_546f22e611f40',htmlentities($_POST['adresse']),$post_id);
update_field('field_546f22f311f41',htmlentities($_POST['code_postal']),$post_id);
update_field('field_546f230211f42',htmlentities($_POST['ville']),$post_id);
update_field('field_546f230811f43',htmlentities($_POST['pays']),$post_id);
update_field('field_546f230d11f44',htmlentities($_POST['telephone']),$post_id);
update_field('field_546f231611f45',htmlentities($_POST['@mail']),$post_id);
update_field('field_546f232011f46',htmlentities($_POST['localisation']),$post_id);
update_field('field_546f232611f47',htmlentities($_POST['prix_souhaite']),$post_id);
update_field('field_546f233211f48',htmlentities($_POST['description_']),$post_id);



            $output = add_post_meta($post_id, "user_meta", json_encode($user_meta_values)) ? 'Article inséré avec succès.' : 'Une erreur est survenue lors de l\enregistrement.' ;
        }

    }

    else{
        foreach($err['msg'] as $k=>$v)
            $output .= $v . '<br />';
    }
}
?>

and my Html :

<form method="post" action="<?php echo site_url().'/vendre'; ?>">    
        <label class="civilite">M.</label><input type="radio" name="civilite" id="civilite" value="monsieur">
        <label class="civilite">Mme</label><input type="radio" name="civilite" id="civilite" value="madame"> <br>
        <label for="nom">Nom</label><input type="text" name="nom" id="nom" value="" /><br>
        <label for="prenom">Prénom</label><input type="text" name="prenom" id="prenom" value="" /><br>
        <label for="adresse">Adresse</label><input type="text" name="adresse" id="adresse" value="" /><br>
        <label for="code_postal">Code Postal</label><input type="text" name="code_postal" id="code_postal" value="" /><br>
        <label for="ville">Ville</label><input type="text" name="ville" id="ville" value="" /><br>
        <label for="pays">Pays</label><input type="text" name="pays" id="pays" value="" /><br>
        <label for="telephone">Téléphone</label><input type="text" name="telephone" id="telephone" value="" /><br>
        <label for="@mail">@mail</label><input type="text" name="@mail" id="@mail" value="" /><br>
        <label for="localisation">Localisation</label><input type="text" name="localisation" id="localisation" value="" /><br>
        <label for="prix_souhaite">Prix souhaité</label><input type="text" name="prix_souhaite" id="prix_souhaite" value="" /><br>
        <label for="description_">Description rapide de votre bien et commentaires</label><textarea name="description_" id="description_" rows="10" cols="15"></textarea><br>





    <p><input type="submit" name="submit" value="envoyer" /></p>
</form>


<div class="ok"><?php echo isset($output) ? $output : '' ; ?></div>

hope someone can help me with this,

thanks a lot

0条回答
登录 后发表回答